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.
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 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的更多相关文章
- 2018 杭电多校1 - Chiaki Sequence Revisited
题目链接 Problem Description Chiaki is interested in an infinite sequence $$$a_1,a_2,a_3,...,$$$ which i ...
- 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 ...
- Chiaki Sequence Revisited HDU - 6304 lowbit找规律法
Problem Description Chiaki is interested in an infinite sequence a1,a2,a3,..., which is defined as f ...
- 【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 ...
- [HDU6304][数学] Chiaki Sequence Revisited-杭电多校2018第一场G
[HDU6304][数学] Chiaki Sequence Revisited -杭电多校2018第一场G 题目描述 现在抛给你一个数列\(A\) \[ a_n=\begin{cases}1 & ...
- HDU 5860 Death Sequence(死亡序列)
p.MsoNormal { margin: 0pt; margin-bottom: .0001pt; text-align: justify; font-family: Calibri; font-s ...
- HDU 1711 Number Sequence(数列)
HDU 1711 Number Sequence(数列) Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Ja ...
- HDU 1005 Number Sequence(数列)
HDU 1005 Number Sequence(数列) Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Jav ...
- HDU 5860 Death Sequence(递推)
HDU 5860 Death Sequence(递推) 题目链接http://acm.split.hdu.edu.cn/showproblem.php?pid=5860 Description You ...
随机推荐
- WIdo联网代码中文注释
代码如下 /*************************************************** 这是一个例子的dfrobot维多-无线集成物联网建兴传感器和控制节点 *产品页面及更 ...
- list实现大整数加法
#include<iostream> #include<list> #include<string> using namespace std; list<in ...
- 初学版本控制更新Version control
概述: 在学习计算机软件工程中,修订控制是跟踪和控制源代码更改的任何类型的实践. 对于软件开发人员有时会使用修订控制软件来维护文档和配置文件以及源代码. 当团队设计,开发和部署软件时,通常会将同一软件 ...
- 关于函数式编程(Functional Programming)
初学函数式编程,相信很多程序员兄弟们对于这个名字熟悉又陌生.函数,对于程序员来说并不陌生,编程对于程序员来说也并不陌生,但是函数式编程语言(Functional Programming languag ...
- HTML前序
HTML基本格式 <!DOCTYPE html> //文档类型声明 <html lang="zh-cn"> //表示HTML文档开始,属性lang,属性值= ...
- PythonStudy——汇编语言 Assembly Language
汇编语言 汇编语言(assembly language)是一种用于电子计算机.微处理器.微控制器或其他可编程器件的低级语言,亦称为符号语言.在汇编语言中,用助记符(Mnemonics)代替机器指令的操 ...
- [转]python中pandas库中DataFrame对行和列的操作使用方法
转自:http://blog.csdn.net/u011089523/article/details/60341016 用pandas中的DataFrame时选取行或列: import numpy a ...
- Innodb中MySQL如何快速删除2T的大表
转自:http://database.51cto.com/art/201808/582324.htm OK,这里就说了.假设,你有一个表erp,如果你直接进行下面的命令: drop table erp ...
- Azure VMSS (3) 修改VM Template并创建VMSS
<Windows Azure Platform 系列文章目录> 在开始本章内容之前,我们需要准备好Azure VM的镜像,具体可以参考:Azure VMSS (2) 对VM执行Genera ...
- [蓝桥杯]PREV-7.历届试题_连号区间数
问题描述 小明这些天一直在思考这样一个奇怪而有趣的问题: 在1~N的某个全排列中有多少个连号区间呢?这里所说的连号区间的定义是: 如果区间[L, R] 里的所有元素(即此排列的第L个到第R个元素)递增 ...