杭电多校 2018 Multi-University Training Contest 1007 Chiaki Sequence Revisited(数学+递归)

Chiaki is interested in an infinite sequence a1,a2,a3,…, which is defined as follows:
an={1an−an−1+an−1−an−2n=1,2n≥3

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

Input
There are multiple test cases. The first line of input contains an integer T (1≤T≤105), indicating the number of test cases. For each test case:
The first line contains an integer n (1≤n≤1018).

Output
For each test case, output an integer denoting the answer.

Sample Input

10 1 2 3 4 5 6 7 8 9 10

Sample Output

1 2 4 6 9 13 17 21 26 32

题意:就是1,1,2,2,3,4,4,4,5,6,6,7,8,8,8,8,9,10,10,11,12,12,12,13, 14, 14, 15, 16,16, 16, 16, 16, 17, 18, 18, 19, 20, 20。。。。这样的一个序列让你求前 n项和。
题解:
第一层 只有1
第二层 1 2 2 3
第三层1 2 2 3 4 4 4 5 6 6 7
。。
也就是左面和上一层相同,但是右面是左面加上的2^(k-1),k为层数
题解我写不出来了,看代码吧,只可意会,不可言传。。

#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define mod 1000000007
#define inf 0x3f3f3f3f
using namespace std;
const int N=1e5+100;
ll a[N];
int main()
{
    #ifdef local
        freopen("D://r.txt","r",stdin);
    #endif
    a[1]=1;
    int z=2,t;;
    while(1)
    {
        a[z]=2*a[z-1]+z;
        if(a[z]>1e18)
            break;
        z++;
    }
    scanf("%d",&t);
    while(t--)
    {
        ll sum=1,cnt=0,n,j,l=1;
        scanf("%lld",&n);
        n--;
        while(n)
        {
            for(int i=1;i<=z;i++) if(a[i]>n){j=i-1;break;}
            ll b = (ll)(l<<j-1)%mod;
            sum+=(((b+cnt)%mod)*(a[j]%mod))%mod;
            cnt=(cnt+(ll)(l<<j))%mod;
            n-=a[j];
            if(n<=j+1)
                sum+=n*cnt,n=0;
            else
                sum+=(j+1)*cnt,n-=j+1;
            sum%=mod;
        }
        printf("%lld\n",sum);
    }
    return 0;
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值