题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=6304
多校contest1
 
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.

 题目大意是求一个 奇怪序列的 前 n 项 和,n最坏情况达 1e18。
开始打了个表,发现在序列是从1开始的连续整数(每个整数出现的次数不同),除了数字 1是出现两次,其他数字 如x 都是出现 lowbit(x)后缀0的个数+1 次。 如2(10) 出现2次 ,3(11) 出现1次,4(100) 出现3次,5(101)出现1次,6(110) 出现2次...
可以发现这是一个十分有特点的(类似2进制,有部分对称性)的序列
接下来我们可以发现如果把1当做出现1次,出现在 2^n 上,如果占满 ,次数总和刚好是 2^(n+1) -1 次,那么多出来的数似乎又没有规律了,这时我们可以利用局部对称与这个和二进制相似的特点,找到 第 n 个数 是数字多少(落在图中的位置),
有两种方法:
第一种 可以知道N(自减一后)对应的准确数字,但不知N落在的数字差几次被填满,不便计算。
 void init(){
P[]=;P[]=;
nP[]=;nP[]=;
for(int i=;i<=;++i){
P[i]=*P[i-];
nP[i]=P[i]-;
}
} ll getbound(ll N){
ll bound=;
for(int i=;i>=;--i){
while(N>=nP[i]){
N-=nP[i];
bound+=P[i-];
}
}
return bound;
}

第二种 完全参照二进制,可知N(自减一后)所落在的数字最近的次数填满数字,后来计算时很方便。

 void init(){
P[]=;P[]=;
nP[]=;nP[]=;
for(int i=;i<=;++i){
P[i]=*P[i-];
nP[i]=P[i]-;
}
} ll getbound(ll& N){
ll bound=;
for(int i=;i>=;--i){
if(N>=nP[i]){
N-=nP[i];
bound+=P[i-];
}
}
return bound;
}

计算时可以可利用每个数字出现的次数,是1(2^0)的倍数的出现过一次,是2(2^1)的倍数的额外出现过一次,是4(2^2)的倍数的又额外出现一次,,,(这也恰恰是后缀0的意义)

在这里贴两份按照上述两种方法写的代码。

 #include <bits/stdc++.h>
using namespace std; typedef long long ll; const ll MOD=1e9+;
ll P[];
ll nP[];
ll a[];
ll arr[]; ll lowbit(ll x){
ll low=x&(-x);
ll cnt=;
while(low>>=){
cnt++;
}
return cnt;
} void init(){
P[]=;P[]=;
nP[]=;nP[]=;
for(int i=;i<=;++i){
P[i]=*P[i-];
nP[i]=P[i]-;
} //a[1]=1;a[2]=1;
//arr[1]=1;arr[2]=2;
//for(int i=3;i<101;i++){
// a[i]=a[i-a[i-1]]+a[i-1-a[i-2]];
// arr[i]=arr[i-1]+a[i];
//} //for(int i=1;i<101;++i) printf("%lld\n",arr[i]); } ll inv(ll a,ll m){
if(a==) return ;
return inv(m%a,m)*(m-m/a)%m;
} ll getbound(ll N){
ll bound=;
for(int i=;i>=;--i){
while(N>=nP[i]){
N-=nP[i];
bound+=P[i-];
}
}
return bound;
} int main(){
//freopen("data.in","r",stdin);
//freopen("data1.out","w",stdout);
init();
int T;
scanf("%d",&T);
while(T--){
ll N;
scanf("%lld",&N);
if(N==) puts("");
else{
ll ans=;
N-=1ll;
ll bound=;
bound=getbound(N);
//printf("bound = %lld\n",bound);
ll cnt=lowbit(bound)+1ll;
ll tot=N;
for(ll i=;i<=cnt;++i){
if(bound==getbound(N+i)) tot++;
else break;
}
ll _m2=inv(,MOD);
//printf("%lld\n",_m2);
for(int i=;i<=;++i){
if(P[i]<=bound){
ll M=bound/P[i];
ans=(ans+(P[i]%MOD)*(M%MOD)%MOD*((M+1ll)%MOD)%MOD*_m2%MOD)%MOD;
}
else break;
}
//printf("1:%lld\n",(ans+1)%MOD);
ans=(ans-(bound)*(tot-N)%MOD+MOD)%MOD;
printf("%lld\n",(ans+1ll)%MOD);
//printf("%I64d %I64d\n",(ans+1ll)%MOD,arr[N+1]);
//最后加一
}
}
return ;
}
 #include <bits/stdc++.h>
using namespace std; typedef long long ll; const ll MOD=1e9+;
ll P[];
ll nP[];
ll a[];
ll arr[]; ll lowbit(ll x){
ll low=x&(-x);
ll cnt=;
while(low>>=){
cnt++;
}
return cnt;
} void init(){
P[]=;P[]=;
nP[]=;nP[]=;
for(int i=;i<=;++i){
P[i]=*P[i-];
nP[i]=P[i]-;
} //a[1]=1;a[2]=1;
//arr[1]=1;arr[2]=2;
//for(int i=3;i<101;i++){
// a[i]=a[i-a[i-1]]+a[i-1-a[i-2]];
// arr[i]=arr[i-1]+a[i];
//} //for(int i=1;i<101;++i) printf("%lld\n",arr[i]); } ll inv(ll a,ll m){
if(a==) return ;
return inv(m%a,m)*(m-m/a)%m;
} ll getbound(ll& N){
ll bound=;
for(int i=;i>=;--i){
if(N>=nP[i]){
N-=nP[i];
bound+=P[i-];
}
}
return bound;
} int main(){
//freopen("data.in","r",stdin);
//freopen("data1.out","w",stdout);
init();
int T;
scanf("%d",&T);
while(T--){
ll N;
scanf("%lld",&N);
if(N==) puts("");
else{
ll ans=;
N-=1ll;
ll bound=;
bound=getbound(N);
//printf("bound = %lld\n",bound);
//ll cnt=lowbit(bound)+1ll;
//ll tot=N;
//for(ll i=1;i<=cnt;++i){
// if(bound==getbound(N+i)) tot++;
// else break;
//}
ll _m2=inv(,MOD);
for(int i=;i<=;++i){
if(P[i]<=bound){
ll M=bound/P[i];
ans=(ans+(P[i]%MOD)*(M%MOD)%MOD*((M+1ll)%MOD)%MOD*_m2%MOD)%MOD; }
else break;
}
//printf("1:%lld\n",(ans+1)%MOD);
ans=(ans+(bound+)*N%MOD)%MOD;
printf("%lld\n",(ans+1ll)%MOD);
//printf("%I64d %I64d\n",(ans+1ll)%MOD,arr[N+1]);
//最后加一
}
}
return ;
}

然后,比赛时WA了两发,其实规律找到了,但错在了计算,算总和时,第一个错误处是没用逆元,第二个错误处是P[i]在N为1e18时奇大,应该先mod在相乘。

血的教训。

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).

HDU 6304 Chiaki Sequence Revisited的更多相关文章

  1. 2018 杭电多校1 - Chiaki Sequence Revisited

    题目链接 Problem Description Chiaki is interested in an infinite sequence $$$a_1,a_2,a_3,...,$$$ which i ...

  2. HDU - 6304(2018 Multi-University Training Contest 1) Chiaki Sequence Revisited(数学+思维)

    http://acm.hdu.edu.cn/showproblem.php?pid=6304 题意 给出一个数列的定义,a[1]=a[2]=1,a[n]=a[n-a[n-1]]+a[n-1-a[n-2 ...

  3. Chiaki Sequence Revisited HDU - 6304 lowbit找规律法

    Problem Description Chiaki is interested in an infinite sequence a1,a2,a3,..., which is defined as f ...

  4. 【HDOJ6304】Chiaki Sequence Revisited(数学)

    题意:给定一个序列a,定义a[1]=a[2]=1,a[n]=a[n-a[n-1]]+a[n-1-a[n-2]](n>=3),求该序列的前n项和是多少,结果对 1e9+7 取模 n<=1e1 ...

  5. [HDU6304][数学] Chiaki Sequence Revisited-杭电多校2018第一场G

    [HDU6304][数学] Chiaki Sequence Revisited -杭电多校2018第一场G 题目描述 现在抛给你一个数列\(A\) \[ a_n=\begin{cases}1 & ...

  6. HDU 5860 Death Sequence(死亡序列)

    p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...

  7. HDU 1711 Number Sequence(数列)

    HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...

  8. HDU 1005 Number Sequence(数列)

    HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...

  9. HDU 5860 Death Sequence(递推)

    HDU 5860 Death Sequence(递推) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 Description You ...

随机推荐

  1. C++ 值传递、指针传递、引用传递详解

    C++ 值传递.指针传递.引用传递详解 最近写了几篇深层次讨论数组和指针的文章,其中提到了“C语言中,所有非数组的形式参数传递均以值传递形式” 数组和指针背后——内存角度 语义"陷阱&quo ...

  2. imp导入时 出现IMP-00017:由于 ORACLE 错误 6550, 以下语句失败: 解决方法

    cmd命令下--执行imp命令时,出现IMP-00017:由于 ORACLE 错误 6550, 以下语句失败: 解决办法:在imp命令里加入 statistics=none (不导入数据库统计信息) ...

  3. list基本代码

    #include<iostream> #include<list> //STL之list的基本用法 using namespace std; void outputList(l ...

  4. 通过用户名&密码验证访问远程共享文件夹 C#

    通过代码先在cmd中运行net use进行验证,然后就可访问共享文件了. 验证方法如下: public string connectState(string path/*要访问的文件路径*/, str ...

  5. xamarin C# 安卓实现 ListView 放大缩小

    翻译自java示例https://raw.githubusercontent.com/Xjasz/AndroidZoomableViewGroup/master/ZoomListView.java u ...

  6. 20165308 2017-2018-2 《Java程序设计》课程总结

    20165308 2017-2018-2 <Java程序设计>课程总结 一.每周作业及实验报告链接汇总 我期待的师生关系 学习基础和c语言调查 Linux 安装及学习 第一周学习总结 第二 ...

  7. vld for memory leak detector (release version)

    有没有这样的情况,无法静态的通过启动和退出来查找内存泄露,比如网络游戏,你总不能直接关游戏那玩家怎么办? 现在vld支持release,我们可以动态的找. 1.在release版本使用vld了.< ...

  8. android 使用Retrofit2 RxJava 文件上传

    private static void upload(final Context context, final int type, File logFile) { Map<String, Req ...

  9. percona-toolki安装冲突(my.cnf Percona-Server-shared与mysql-community-server)

    最近在安装percona-toolkit工具包时,提示在my.cnf文件中, Percona-Server-shared与mysql-community-server冲突.起初还以为是一定需安装Per ...

  10. [SQL]查询整个数据库中某个特定值所在的表和字段的方法

    查询整个数据库中某个特定值所在的表和字段的方法 当数据库做的太庞大的时候,难免会出现忘记哪个值会存入哪个表的情况,于是在网上找到的如下解决办法. 通过做一个存储过程,只需要传入一个想要查找的值,即可查 ...