URAL 1036(dp+高精度)

Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  URAL 1036

Description

You are given a number 1 ≤  N ≤ 50. Every ticket has its 2  N-digit number. We call a ticket lucky, if the sum of its first  N digits is equal to the sum of its last  N digits. You are also given the sum of ALL digits in the number. Your task is to count an amount of lucky numbers, having the specified sum of ALL digits.

Input

Two space-separated numbers:  N and  S. Here  S is the sum of all digits. Assume that 0 ≤  S ≤ 1000.

Output

The amount of lucky tickets.

Sample Input

input output
2 2

4

Hint

The tickets are 0101, 0110, 1001, 1010 in the example above
求前N位与后N位各个位和相等且总和等于S的2N位数的个数。
注意:每个位上的数字是0-9也就是十进制
状态转移方程:dp[i][j]=sum(dp[i-1][j-k],0<=k<=9)。可想而知最后的数一定会很大。所有操作都用高精度来做
#include<stdio.h>

#include<cstring>

#include<iostream>

using namespace std;

int dp[55][505][105];//第100位存的是数据的长度

int n,tot;

void add(int sum[],int num[])

{

    int i,j,temp[105];

    int len1=sum[100],len2=num[100];

    int max=len1>len2 ? len1:len2;

    int jin=0;

    for(i=0;i<max;i++){

        int m=jin+sum[i]+num[i];

        if(m>=10) {jin=1;m-=10;}

        else jin=0;

        temp[i]=m;

    }

    if(jin) temp[i++]=jin;

    for(j=0;j<i;j++)

        sum[j]=temp[j];

    sum[100]=i;

}

void muti_dan(int num[],int data[],int n,int h)

{

    int temp[105],i,j;

    memset(temp,0,sizeof(temp));

    int len=num[100];

    int jin=0;

    for(i=0;i<len;i++){

        int m=num[i]*n+jin;

        jin=m/10;

        if(jin)m-=jin*10;

        temp[i]=m;

    }

    if(jin) temp[i++]=jin;

    data[100-h]=i;

    for(j=0;j<i;j++){

        data[j]=temp[j];

    }

}

void  multi(int num[],int num1[])

{

    int temp[105],sum[105],i,j;

    memset(sum,0,sizeof(sum));

    int len1=num[100],len2=num1[100];

    for(j=0;j<len2;j++){

        memset(temp,0,sizeof(temp));

        muti_dan(num,&temp[j],num1[j],j);

        temp[100]+=j;

        add(sum,temp);

    }

    for(i=0;i<sum[100];i++){

        num[i]=sum[i];

    }

    num[100]=sum[100];

}

void display(int num[])

{

    int len=num[100];

    for(int i=len-1;i>0;i--)

        cout<<num[i];

    cout<<num[0]<<endl;

}

void solve()

{

    int i,j,k;

    if(tot%2) {printf("0\n");return;}

    int sum=tot/2;

    memset(dp,0,sizeof(dp));

    for(i=0;i<10;i++) {

        dp[0][i][0]=1;

        dp[0][i][100]=1;

    }



    for(i=0;i<n-1;i++){

        for(j=0;j<=sum;j++){

            for(k=0;k<=9&&j+k<=sum;k++){

                add(dp[i+1][j+k],dp[i][j]);

            }

        }

    }

    

    multi(dp[n-1][sum],dp[n-1][sum]);

    display(dp[n-1][sum]);

}

int main(void)

{

    while(cin>>n>>tot){

        solve();

    }

    return 0;

}

 

你可能感兴趣的:(dp)