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 ...
随机推荐
- 工作中 sql 整理(一)
这篇文章记录关于SQL的内容,有些凌乱,是工作中点滴的积累,只能按照时间顺序,逐次记录. 一.update 关联更新 1.需求 Table A TableB A表中的主键和B表中的主键相关联,关联 ...
- Redhat 6.5安装JDK和Tomcat小记
下面将今天在Linux 6.5环境安装JDK和Tomcat的过程记录下来,以备以后查用. Linux环境:Redhat 6.5 JDK版本:7u79 Tomcat版本:7.0.70 1.下载JDK文件 ...
- Qt5 中文乱码问题
在做gui界面时,使用QTdesigner设计时,控件的名称等输入中文没有问题.为了更加灵活点,直接使用代码进行布局就很有必要了.这样就会出现中文的乱码.为了解决乱码,查看资料说时修改文件保存的编码, ...
- Dockerfile之nginx(六)
一.Dokcerfile的基本指令 1)From 指定构建镜像的基础镜像 2)MAINTAINER 指定镜像的作者 3)RUN 使用前一条指令创建的镜像生产容器,并在容器中执行命令,执行结束后会自 ...
- asp.net验证码
asp.net 生成验证码问题 .添加一个.ashx文件 <%@ WebHandler Language="C#" class="CheckCode" % ...
- cookie.js插件
/*! cookiejs v1.0.23 | MIT (c) 2018 kenny wong | https://github.com/jaywcjlove/cookie.js */!function ...
- SELINUX工作原理
SELinux工作原理 1. 简介 SELinux带给Linux的主要价值是:提供了一个灵活的,可配置的MAC机制. Security-Enhanced Linux (SELinux)由以下两部分组成 ...
- WHERE 子句操作符
操作符(operator) 用来联结或改变WHERE子句中得子句的关键字,也称为逻辑操作符(logical operator): 操作符 说 明 = 等于 <> 不等于 != 不等于 & ...
- 面向的phthon2+3 的场景,Anaconda 安装+环境配置+管理
standard procedure in pyCharm for creating environment when Anaconda installed Create a conda env vi ...
- python学习笔记_week25
note Day25 - 博客 - KindEditor - beautifulsoup4对标签进行过滤 - 单例模式 - 事务操作 - from django.db import transacti ...