[ZJOI2012]数列(高精)

[ZJOI2012]数列(luogu)

Solution

观察发现,对于一对A(i)和A(i+1)( i 为偶数),求它们的值需要知道的A(x)只有两个,且也是一对(A(y),A(y+1)( y 为偶数))

也就是说可以将A(n)表示为 k1*A(0)+k2*A(1)

于是可以不断将 n 除2,记录此时的系数,注意用高精

Code

#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>;
using namespace std;
string chu(string a,int b)
{
    string c="";
    int d=0;
    for(int i=0;i<a.length();i++)
        c.push_back((d*10+(a[i]-48))/b+48),d=(d*10+(a[i]-48))%b;
    for(int i=0;c[0]==48;i++) c.erase(c.begin(),c.begin()+1);
    return c;
}
string add(string a,string b)
{
    int la=a.length(),lb=b.length();
    if(la<lb) swap(a,b),swap(la,lb);
    int j=0,k=la-lb;
    string c,s;
    for(int i=1;i<=k;i++) b='0'+b;
    for(int i=la-1;i>=0;i--)
    {
        s=char((a[i]-48+b[i]-48+j)%10+48)+s;
        j=(a[i]-48+b[i]-48+j)/10;
    }
    if(j==1) s='1'+s;
    return s;
}
int T;
string n;
int main()
{
    cin>>T;
    while(T--)
    {
        string a="1",b="0";
        cin>>n;
        while(add("1",n)!="1")
        { 
            string x=chu(n,2);
            if(add(x,x)==n) a=add(a,b);
            else b=add(a,b);
            n=x;
        }
        cout<<b<<endl;
    }
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/hsez-cyx/p/12393731.html
今日推荐