2018 Multi-University Training Contest 1 hdu 6304 Chiaki Sequence Revisited(规律?数学?)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6304

题意:看一眼就懂了

做法:找规律,每个数出现的次数k,满足以2^k差为2^k+1的等差数列,然后就可以了,然后正解是差分,阿贝尔变换,本人不会

代码

#include<bits/stdc++.h>
#define N 100005
#define P pair<int,int>
using namespace std;
typedef long long ll;
const int M=1e9+7;
const int inf=1e9+7;
ll pre[N],f[N];
int main()
{
    int t;
    ll n;
    pre[0]=f[0]=1;
    for(int i=1;i<63;i++){
        pre[i]=pre[i-1]*2+1;
        f[i]=f[i-1]*2;
    }
    for(scanf("%d",&t);t;t--)
    {
        scanf("%lld",&n);
        ll m=n,p=0;
        for(int i=62;i>=0;i--)
            if(pre[i]<=m){
                m-=pre[i];
                p|=f[i];
            }
        p+=(m>0);
        ll sum=1,ans=1;
        ll ni=M+1>>1;
        for(int i=1;i<63&&f[i-1]<p;i++){
            ll k=(p-f[i-1]-1)/f[i];
            sum+=(k+1)*i;
            ll x=f[i-1]+k*f[i];
            (ans+=(f[i-1]+x)%M*((k+1)%M)%M*ni%M*i%M)%=M;
        }
        (ans+=(n-sum)%M*(p%M)%M)%M;
        printf("%lld\n",(ans+M)%M);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/abutoto/article/details/81174428