[DP] 51nod1048 integer decomposed to a power V2__DP of 2

Source: Internet
Author: User

This is an upgraded version of 51nod1383. n N is 1030 10^{30}
The approach is a fantastic DP DP. Consider under the binary system.
Fi,j F_{i,j} indicates that the maximum number of 2i 2^i is the number of scenarios when 2j 2^j.
Transfer is fi,j=fi−1,k+fi−1−k,j−k f_{i,j}=f_{i-1,k}+f_{i-1-k,j-k}
Then there is a similar merge per bit. Gi,j G_{i,j} represents the number of scenarios in which the maximum number of 2j 2^j is composed of n n binary bits. Transfer similar.
God TM does not give modulus to force high precision ... and ...

The following is an Kathang code

#include <cstdio> #include <cstring> using namespace std;
typedef long Long LL;
const int maxn=101,con=100000000;
    inline int max (int x,int y) {return x>y?x:y;} struct int{int a[205];
    inline int (int val=0) {memset (a,0,sizeof (a)); doing A[++a[0]]=val%con, Val/=con; while (Val);}
        inline void Sread () {static char st[6000]; scanf ("%s", st+1); int Len=strlen (st+1); a[0]= (len+7)/8;
    for (int i=1;i<=len;i++) a[(len+1-i+7)/8]=a[(len+1-i+7)/8]*10+st[i]-' 0 ';
    } inline void Print () {printf ("%d", a[a[0]]); for (int i=a[0]-1;i>=1;i--) printf ("%08d", A[i);}
        inline void operator + = (const Int &b) {A[0]=max (a[0],b.a[0]);
        for (int i=1;i<=a[0];i++) a[i]+=b.a[i], A[i+1]+=a[i]/con, A[i]%=con;
    if (a[a[0]+1]) a[0]++; } Inline int operator * (const int &b) {Int C; c.a[0]=a[0]+b.a[0]-1;
        LL v; for (int i=1;i<=a[0];i++) for (int j=1;j<=b.a[0];j++) v= (LL) a[i]*b.a[j], c.a[i+j]+= (v+c.a[i+j-1))/Con, c.a[i+j-1]= (v+c.a[i+j-1])%con; if (c.a[c.a[0]+1]) c.a[0]++; while (C.a[0]>1&&!c.a[c.a[0]]) c.a[0]--;
    return C; } inline void operator/= (const int val) {LL x=0; for (int i=a[0];i>=1;i--) x=x*con+a[i], A[i]=x/val, x=x%v
        Al
    while (A[0]>1&&!a[a[0]]) a[0]--;
}} N,f[maxn][maxn],g[2][maxn],ans;
    int main () {n.sread ();
    F[0][0]=1;
        for (int i=1;i<=100;i++) {f[i][i]=1;
    for (int j=0;j<=i-1;j++) for (int k=0;k<=j;k++) f[i][j]+=f[i-1][k]*f[i-1-k][j-k];
    int now=0;
        for (int i=0;i<=100;n/=2,i++) if (n.a[1]&1) {now++;
            if (now==1) {for (int j=0;j<=i;j++) g[now&1][j]=f[i][j];
        Continue
            for (int j=0;j<=i;j++) {g[now&1][j]=0;
        for (int k=0;k<=j;k++) g[now&1][j]+=g[(now&1) ^1][k]*f[i-k][j-k];
    for (int i=0;i<=100;i++) ans+=g[now&1][i]; Ans.
    Print (); REturn 0; }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.