HDU 6304 Chiaki Sequence Revisited
Chiaki is interested in an infinite sequence a1,a2,a3,..., which is defined as follows:
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).
The first line contains an integer n (1≤n≤1018).
For each test case, output an integer denoting the answer.
1 void init(){ 2 P[0]=1;P[1]=2; 3 nP[0]=0;nP[1]=1; 4 for(int i=2;i<=62;++i){ 5 P[i]=2*P[i-1]; 6 nP[i]=P[i]-1; 7 } 8 } 9 10 ll getbound(ll N){ 11 ll bound=0; 12 for(int i=62;i>=1;--i){ 13 while(N>=nP[i]){ 14 N-=nP[i]; 15 bound+=P[i-1]; 16 } 17 } 18 return bound; 19 }
第二种 完全参照二进制,可知N(自减一后)所落在的数字最近的次数填满数字,后来计算时很方便。
1 void init(){ 2 P[0]=1;P[1]=2; 3 nP[0]=0;nP[1]=1; 4 for(int i=2;i<=62;++i){ 5 P[i]=2*P[i-1]; 6 nP[i]=P[i]-1; 7 } 8 } 9 10 ll getbound(ll& N){ 11 ll bound=0; 12 for(int i=62;i>=1;--i){ 13 if(N>=nP[i]){ 14 N-=nP[i]; 15 bound+=P[i-1]; 16 } 17 } 18 return bound; 19 }
计算时可以可利用每个数字出现的次数,是1(2^0)的倍数的出现过一次,是2(2^1)的倍数的额外出现过一次,是4(2^2)的倍数的又额外出现一次,,,(这也恰恰是后缀0的意义)
在这里贴两份按照上述两种方法写的代码。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long ll; 5 6 const ll MOD=1e9+7; 7 ll P[66]; 8 ll nP[66]; 9 ll a[101]; 10 ll arr[101]; 11 12 ll lowbit(ll x){ 13 ll low=x&(-x); 14 ll cnt=0; 15 while(low>>=1){ 16 cnt++; 17 } 18 return cnt; 19 } 20 21 void init(){ 22 P[0]=1;P[1]=2; 23 nP[0]=0;nP[1]=1; 24 for(int i=2;i<=62;++i){ 25 P[i]=2*P[i-1]; 26 nP[i]=P[i]-1; 27 } 28 29 //a[1]=1;a[2]=1; 30 //arr[1]=1;arr[2]=2; 31 //for(int i=3;i<101;i++){ 32 // a[i]=a[i-a[i-1]]+a[i-1-a[i-2]]; 33 // arr[i]=arr[i-1]+a[i]; 34 //} 35 36 //for(int i=1;i<101;++i) printf("%lld\n",arr[i]); 37 38 } 39 40 ll inv(ll a,ll m){ 41 if(a==1) return 1; 42 return inv(m%a,m)*(m-m/a)%m; 43 } 44 45 ll getbound(ll N){ 46 ll bound=0; 47 for(int i=62;i>=1;--i){ 48 while(N>=nP[i]){ 49 N-=nP[i]; 50 bound+=P[i-1]; 51 } 52 } 53 return bound; 54 } 55 56 int main(){ 57 //freopen("data.in","r",stdin); 58 //freopen("data1.out","w",stdout); 59 init(); 60 int T; 61 scanf("%d",&T); 62 while(T--){ 63 ll N; 64 scanf("%lld",&N); 65 if(N==1) puts("1"); 66 else{ 67 ll ans=0; 68 N-=1ll; 69 ll bound=0; 70 bound=getbound(N); 71 //printf("bound = %lld\n",bound); 72 ll cnt=lowbit(bound)+1ll; 73 ll tot=N; 74 for(ll i=1;i<=cnt;++i){ 75 if(bound==getbound(N+i)) tot++; 76 else break; 77 } 78 ll _m2=inv(2,MOD); 79 //printf("%lld\n",_m2); 80 for(int i=0;i<=62;++i){ 81 if(P[i]<=bound){ 82 ll M=bound/P[i]; 83 ans=(ans+(P[i]%MOD)*(M%MOD)%MOD*((M+1ll)%MOD)%MOD*_m2%MOD)%MOD; 84 } 85 else break; 86 } 87 //printf("1:%lld\n",(ans+1)%MOD); 88 ans=(ans-(bound)*(tot-N)%MOD+MOD)%MOD; 89 printf("%lld\n",(ans+1ll)%MOD); 90 //printf("%I64d %I64d\n",(ans+1ll)%MOD,arr[N+1]); 91 //最后加一 92 } 93 } 94 return 0; 95 }
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 typedef long long ll; 5 6 const ll MOD=1e9+7; 7 ll P[66]; 8 ll nP[66]; 9 ll a[101]; 10 ll arr[101]; 11 12 ll lowbit(ll x){ 13 ll low=x&(-x); 14 ll cnt=0; 15 while(low>>=1){ 16 cnt++; 17 } 18 return cnt; 19 } 20 21 void init(){ 22 P[0]=1;P[1]=2; 23 nP[0]=0;nP[1]=1; 24 for(int i=2;i<=62;++i){ 25 P[i]=2*P[i-1]; 26 nP[i]=P[i]-1; 27 } 28 29 //a[1]=1;a[2]=1; 30 //arr[1]=1;arr[2]=2; 31 //for(int i=3;i<101;i++){ 32 // a[i]=a[i-a[i-1]]+a[i-1-a[i-2]]; 33 // arr[i]=arr[i-1]+a[i]; 34 //} 35 36 //for(int i=1;i<101;++i) printf("%lld\n",arr[i]); 37 38 } 39 40 ll inv(ll a,ll m){ 41 if(a==1) return 1; 42 return inv(m%a,m)*(m-m/a)%m; 43 } 44 45 ll getbound(ll& N){ 46 ll bound=0; 47 for(int i=62;i>=1;--i){ 48 if(N>=nP[i]){ 49 N-=nP[i]; 50 bound+=P[i-1]; 51 } 52 } 53 return bound; 54 } 55 56 int main(){ 57 //freopen("data.in","r",stdin); 58 //freopen("data1.out","w",stdout); 59 init(); 60 int T; 61 scanf("%d",&T); 62 while(T--){ 63 ll N; 64 scanf("%lld",&N); 65 if(N==1) puts("1"); 66 else{ 67 ll ans=0; 68 N-=1ll; 69 ll bound=0; 70 bound=getbound(N); 71 //printf("bound = %lld\n",bound); 72 //ll cnt=lowbit(bound)+1ll; 73 //ll tot=N; 74 //for(ll i=1;i<=cnt;++i){ 75 // if(bound==getbound(N+i)) tot++; 76 // else break; 77 //} 78 ll _m2=inv(2,MOD); 79 for(int i=0;i<=62;++i){ 80 if(P[i]<=bound){ 81 ll M=bound/P[i]; 82 ans=(ans+(P[i]%MOD)*(M%MOD)%MOD*((M+1ll)%MOD)%MOD*_m2%MOD)%MOD; 83 84 } 85 else break; 86 } 87 //printf("1:%lld\n",(ans+1)%MOD); 88 ans=(ans+(bound+1)*N%MOD)%MOD; 89 printf("%lld\n",(ans+1ll)%MOD); 90 //printf("%I64d %I64d\n",(ans+1ll)%MOD,arr[N+1]); 91 //最后加一 92 } 93 } 94 return 0; 95 }
然后,比赛时WA了两发,其实规律找到了,但错在了计算,算总和时,第一个错误处是没用逆元,第二个错误处是P[i]在N为1e18时奇大,应该先mod在相乘。
血的教训。
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).
posted on 2018-07-24 22:11 Emiya_Kiritsugu 阅读(332) 评论(0) 编辑 收藏 举报