题意:
The sequence is generated by the following scheme.
1. First, write down 1, 2 on a paper.2. The 2nd number is 2, write down 2 2’s (including the one originally on the paper). The paper thus has 1, 2, 2 written on it.
3. The 3rd number is 2, write down 2 3’s. 1, 2, 2, 3, 3 is now shown on the paper.
4. The 4th number is 3, write down 3 4’s. 1, 2, 2, 3, 3, 4, 4, 4 is now shown on the paper.
5. The procedure continues indefinitely as you can imagine. 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6, 6, . . . .
规律就是上述内容。。
网赛时到处找规律,没写出来。
赛后一想:
题目只要求前1e9个数,所求答案为前n项i*f[i]的和。而我们知道,有一堆连续的数,如2有2个,3有2个,4有3个,,那么凑足1e9个数时,那个数的数值应该不会很大。通过调试,知道,那个数不超过5e5。所以,我们只要把前5e5个数暴力求出来,并且引进个辅助数组g[n],表示序列中数字(而非位置)到n时所求答案的大小。然后再引进一个数字,sum[n]表示到n时可以写出多少个数字。通过二分,我们就能在给定n的情况下,找到其具体数值了。
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <set>
#include <algorithm>
#include <math.h>
using namespace std;
typedef long long ll;
const int N=5e5;
const int inf=1e9;
const int Mod=1e9+7;
int f[N];
ll sum[N];
int g[N];
int cal(int a,int b){
int ret=(ll)(a+b)*(b-a+1)/2%Mod;
return ret;
}
void work(){
f[1]=g[1]=1;
f[2]=2;
int cur=1;
sum[1]=1;
g[1]=1;
for(int i=2;i<N;i++){
for(int j=0;j<f[i];j++){
if(cur==N-1)break;
f[++cur]=i;
}
sum[i]=sum[i-1]+f[i];
g[i]=g[i-1]+(ll)i*cal(sum[i-1]+1,sum[i])%Mod;
g[i]%=Mod;
}
int T;
scanf("%d",&T);
while(T--){
int n,pos;
scanf("%d",&n);
pos=upper_bound(sum+1,sum+N,n)-sum;
pos--;
ll left=n-sum[pos];
//printf("(%d,%I64d)\n",pos,left);
int ret=g[pos]+(ll)(pos+1)*cal(sum[pos]+1,n)%Mod;
ret%=Mod;
printf("%d\n",ret);
}
//printf("%I64d,%d\n",sum[N-1],f[N-1]);
//for(int i=1;i<100;i++)printf("%d\n",f[i]);
}
int main(){
freopen("data.in","r",stdin);
work();
return 0;
}