推了好久。。。
这个题肯定是先把an打出来。。1 1 2 2 3 4 4 4 5 6 6 7 8 8 8 8 9
发现从小到大出现,而且除了1,出现次数为二进制形式最低位的位数。。
然后是求和。。既然和二进制有关,优先考虑n=2^k的情况。。发现an都是n/2。。然后就要考虑前n/2怎么推出n了。。
1 2 2 3 4 4 4
5 6 6 7 8 8 8
贴一下 2-8和9-15(因为第一项不符合最低位的规律)可以发现他们都只差了4,由a[n/2]和a[n]的关系可以推出差值为a[n]-a[n/2]=n/4
然后就可以yy公式了。。设s为前缀和。。
s[n]={s[n/2]}+{(s[n/2]-1)+(n/2-1)*(n/4)}+{n/2}
其中第一项为前n/2的和;第二项为n+1~n-1的和,可以在2~n/2的基础上+项数*n/4得到;第三项就是n/4了。。
这样就解决了s[2^k]了。。
然后其他的呢?其实可以先把n表示成二进制。。然后设最高位为m
前2^m可以先算出来,剩下的减去a[2^m],可以发现会变成从2开始的a序列。。(和算2^k的思想类似)然后补上一个1就变成了另一个s。。
这样按位减可以依次得到答案了。。
Chiaki Sequence Revisited
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 887 Accepted Submission(s): 203
Problem Description
Chiaki is interested in an infinite sequence a1,a2,a3,..., which is defined as follows:
an={1an−an−1+an−1−an−2n=1,2n≥3
Chiaki would like to know the sum of the first n terms of the sequence, i.e. ∑i=1nai. As this number may be very large, Chiaki is only interested in its remainder modulo (109+7).
Input
There are multiple test cases. The first line of input contains an integer T (1≤T≤105), indicating the number of test cases. For each test case:
The first line contains an integer n (1≤n≤1018).
Output
For each test case, output an integer denoting the answer.
Sample Input
10 1 2 3 4 5 6 7 8 9 10
Sample Output
1 2 4 6 9 13 17 21 26 32
Source
2018 Multi-University Training Contest 1
Recommend
liuyiding
Statistic | Submit | Discuss | Note