hdu 5439 Aggregated Counting(找规律)
来源:互联网 发布:u盘数据恢复文件乱码 编辑:程序博客网 时间:2024/05/18 00:34
题目大意:
一个序列1、2、2、3、3、4、4、4、5、5、5……
第i项的数a[i]表示连续的i的个数,依次下去。
现给出n(n<=1e9),记最后出现n的位置为last[n],求last[last[n]]。
规律:last[last[n]]=a[1]+a[2]+……+a[last[n]]。比如last[3]=5,则last[last[3]]为序列的前5项的和,为11
知道了这个还不能算,因为n太大了。
再对上式进行归纳整理,可以得到:
last[last[n]]=1*1+(2+3)*2+(4+5)*3+……+(k+k+1+k+2+……+a[last[p-1]])*(p-1)+(a[last[p- 1]]+1+……+n)*p
其中每一个括号里面的数字构成等差序列,第i个括号里面的项数恰好是a[i](最后一个括号除外,最多只加到n),最后一项为a[last[i]]。
n最大可达到1e9,而p的最大值相对而言会小得多。
通过测试可以知道最大的p不会超过500000(事实上只有43万多一点)
那么,可以预处理出500000以内的序列a[i],last[i]。
以及前缀和ans[i]。
这里的ans[i]=1*1+(2+3)*2+(4+5)*3+……+(k+k+1+k+2+……+a[last[i])*i
对于每一个n,通过二分查找,找到p(其前面的括号项数刚好是a[i]的最大的p),即可马上得出答案。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<queue>using namespace std;#define maxn 500000#define mod 1000000007typedef __int64 LL;int a[maxn],last[maxn],tot;LL ans[maxn];void init(){ int i; a[1]=last[1]=1; a[2]=2; last[2]=3; a[3]=2; LL sum=3; for(i=3;sum<1000000000;++i){ sum+=a[i]; last[i]=sum; for(LL j=sum-a[i]+1;j<=min(sum,(LL)maxn);++j) a[j]=i; } tot=i-1; ans[0]=0; for(i=1;i<maxn;++i) ans[i]=(ans[i-1]+(LL(i)*(last[i]-a[i]+1+last[i])%mod)*a[i]%mod*500000004%mod)%mod;}int Find(int n){ int l=1,r=tot,ans; while(l<=r) { int m=(l+r)>>1; if(last[m]<=n) {ans=m;l=m+1;} else r=m-1; } return ans;}int main(){ init(); int T,n; scanf("%d",&T); while(T--) { scanf("%d",&n); int p=Find(n); LL res=ans[p]; if(last[p]<n) res=(res+LL(p+1)*(last[p]+1+n)%mod*(n-last[p])%mod*500000004)%mod; printf("%I64d\n",res); } return 0;}
0 0
- HDU 5439 Aggregated Counting(找规律+预处理)
- HDU 5439 Aggregated Counting 找规律+二分
- hdu 5439 Aggregated Counting(找规律)
- hdu 5439 Aggregated Counting(规律)
- hdu 5439 Aggregated Counting(长春网络赛——找规律+二分)
- HDU 5439 Aggregated Counting
- HDU 5439 Aggregated Counting
- HDU 5439 Aggregated Counting
- hdu 5439 Aggregated Counting
- HDU5439 Aggregated Counting (找规律+预处理+二分)
- HDU 5439 Aggregated Counting(数论+二分)
- HDU 1396 Counting Triangles (找规律)
- 2015长春网络赛1003 HDU 5439 Aggregated Counting
- hdu 5439(找规律)
- hdu 5439 Aggregated Counting 2015长春网络赛 分块 二分 预处理
- HDU 5439 Aggregated Counting (2015年长春赛区网络赛C题)
- hdu 1597 找规律
- HDU-1597(找规律)
- 对 Jsp及Servlet 的简单剖析
- zoj3892Available Computation Sequence(区间dp)
- LeetCode Best Time to Buy and Sell Stock IV
- .net中ImageField绑定图片路径
- java 逆置 单链表
- hdu 5439 Aggregated Counting(找规律)
- 将char[][] 赋值给char**的结果
- Xcode添加常用的代码片段 (code sinppets)
- Python 学习(2)---基本知识整理
- 在Linux命令行下运行Matlab
- python 关键词记录
- oracle 部分文件作用
- 【ionic&AngularJS】用户头像压缩上传,按比例缩小。
- jQuery提交JSON文件至php网页,保存为文档文件