HDU 5439 Aggregated Counting 找规律+二分

题意:

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;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值