HDU 5439 Aggregated Counting

Aggregated Counting

Time Limit: 1500/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)
Total Submission(s): 672    Accepted Submission(s): 305


Problem Description
Aggregated Counting Meetup (ACM) is a regular event hosted by Intercontinental Crazily Passionate Counters (ICPC). The ICPC people recently proposed an interesting sequence at ACM2016 and encountered a problem needed to be solved.

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, . . . .

The ICPC is widely renowned for its counting ability. At ACM2016, they came up with all sorts of intriguing problems in regard to this sequence, and here is one: Given a positive number  n , First of all, find out the position of the last  n  that appeared in the sequence. For instance, the position of the last 3 is 5, the position of the last 4 is 8. After obtaining the position, do the same again: Find out the position of the last (position number). For instance, the position of the last 3 is 5, and the position of the last 5 is 11. ICPC would like you to help them tackle such problems efficiently.

 

Input
The first line contains a positive integer  T,T2000 , indicating the number of queries to follow. Each of the following  T  lines contain a positive number  n(n109)  representing a query.
 

Output
Output the last position of the last position of each query  n . In case the answer is greater than  1000000006 , please modulo the answer with  1000000007 .
 

Sample Input
  
  
3 3 10 100000
 

Sample Output
  
  
11 217 507231491
 

Source
 

看了好久才懂的一题、、、

自己看懂了规律,英文差还是有好处的,一开始自己写的时候就认为输入的n是求序列的前n项和、、、、

结果看了别人的博客才知道、、、是求f(f(n))、、、

借鉴了一下:

若将题目中的A数组进行另一种写法  则有

A 1 2 2 3 3 4 4 4 5 5 5 6 6 6 6 7 7 7 7 .......

如果用B来记录出现i次的数有多少个  则有

B 1 2 2 3 3 4 4 4 5 5 5 6 6 6 6 7 7 7 7 ......

 A B 数组完全相同。

那么就是求A的前n项和的问题了。。。因为n太大了  打表也打不出来、、、所以就对公式化简

在B数组中,暴力跑一边发现  b的前400000多万项和已经大于1e9了,40多万不是很大,所以就拿B数组作为突破口

A与B的关系呢、、、就是 sum(Ai|1<=i<=n)=1*1+(2+3)*2+(4+5)*3+...+(Bp项)p

最后的一个不足Bp项、、、

这样看来,只要确定每一个p的序列开头,则可以用B数组确定里面有几个连续的数,等差数列前n项和就能快速的出和了。所以只要打40+万的表就能解决了、、、

#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <math.h>
#include <algorithm>
#include <iostream>
using namespace std;
typedef long long ll;

ll ans[500010],b[500010],a[500010];
int len;
const ll Mod=1e9+7;

void init()
{
    a[1]=1;
    a[2]=2;
    b[1]=1;
    b[2]=11;
    ans[1]=1;
    ans[2]=3;
    int it=2,aa=a[it]-1;
    for(int i=3;;i++)
    {
        a[i]=it;
        aa--;
        ans[i]=ans[i-1]+it;//保存第i个等差数列的其实项
        b[i]=(b[i-1]+((ans[i]+ans[i-1]+1)*it*i/2)%Mod)%Mod;
        if(aa==0)
        {
            it++;
            aa=a[it];
        }

        if(ans[i]>=1e9)
        {
            len=i;
            break;
        }
    }
}

int main()
{
    len=0;
    init();
    int t,n;
    scanf("%d",&t);
    while(t--&&scanf("%d",&n))
    {
        int low=lower_bound(ans+1,ans+len,n)-ans; //确定n是在哪一个连续的区间内
            ll sum=b[low]; 
            sum=((sum-((ans[low]-n)*(n+1+ans[low])/2)*low%Mod)%Mod+Mod)%Mod; //减掉n+1到该区间最后的数的结果
            printf("%lld\n",sum);
    }
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值