杭电多校 2018 Multi-University Training Contest 1007 Chiaki Sequence Revisited(数学+递归)
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
题意:就是1,1,2,2,3,4,4,4,5,6,6,7,8,8,8,8,9,10,10,11,12,12,12,13, 14, 14, 15, 16,16, 16, 16, 16, 17, 18, 18, 19, 20, 20。。。。这样的一个序列让你求前 n项和。
题解:
第一层 只有1
第二层 1 2 2 3
第三层1 2 2 3 4 4 4 5 6 6 7
。。
也就是左面和上一层相同,但是右面是左面加上的2^(k-1),k为层数
题解我写不出来了,看代码吧,只可意会,不可言传。。
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007
#define inf 0x3f3f3f3f
using namespace std;
const int N=1e5+100;
ll a[N];
int main()
{
#ifdef local
freopen("D://r.txt","r",stdin);
#endif
a[1]=1;
int z=2,t;;
while(1)
{
a[z]=2*a[z-1]+z;
if(a[z]>1e18)
break;
z++;
}
scanf("%d",&t);
while(t--)
{
ll sum=1,cnt=0,n,j,l=1;
scanf("%lld",&n);
n--;
while(n)
{
for(int i=1;i<=z;i++) if(a[i]>n){j=i-1;break;}
ll b = (ll)(l<<j-1)%mod;
sum+=(((b+cnt)%mod)*(a[j]%mod))%mod;
cnt=(cnt+(ll)(l<<j))%mod;
n-=a[j];
if(n<=j+1)
sum+=n*cnt,n=0;
else
sum+=(j+1)*cnt,n-=j+1;
sum%=mod;
}
printf("%lld\n",sum);
}
return 0;
}