BNU OJ 51003 BQG's Confusing Sequence

二进制++高精度取模

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;

const int maxn=100;
long long a[maxn];
long long sumA[maxn];
int aa,bb;
int len;
long long n;

int Base[maxn];
int tot;

int ans[maxn];

void init1()
{
    memset(sumA,0,sizeof sumA);
    a[0]=1;
    for(int i=1; i<=30; i++)
    {
        a[i]=2*a[i-1];
        sumA[i]=sumA[i-1]+a[i];
    }
}


int main()
{
    init1();

    int T;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%d%d%lld",&aa,&bb,&n);
        if(aa>bb) swap(aa,bb);
        if(aa&&bb)
        {
            for(int i=1; i<=30; i++)
            {
                if(sumA[i]>=n)
                {
                    int x=(int)(a[i]-1-(sumA[i]-n));
                    //printf("%d\n",x);
                    memset(Base,0,sizeof Base);
                    tot=0;
                    while(x) Base[tot++]=x%2,x=x/2;

                    len=0;
                    for(int j=i-1;j>=0;j--)
                    {
                        if(Base[j]==0) ans[len++]=aa;
                        else ans[len++]=bb;
                    }
                    break;
                }
            }
        }
        else
        {
            memset(Base,0,sizeof Base);
            tot=0;
            while(n) Base[tot++]=n%2,n=n/2;
            len=0;
            for(int i=tot-1;i>=0;i--)
            {
                if(Base[i]==0) ans[len++]=aa;
                else ans[len++]=bb;
            }
        }

        long long Ans=0;
        long long MOD=1000000007;
        for(int i=0;i<len;i++)
        {
            Ans=Ans*10+ans[i];
            Ans=Ans%MOD;
        }
        printf("%lld\n",Ans);
    }
    return 0;
}

 

posted @ 2015-12-26 11:29  Fighting_Heart  阅读(151)  评论(0编辑  收藏  举报