URAL 1036 Lucky Tickets (高精度,数位dp)

题意:

求2*N位的数,前N位数和等于后N位数和并且这个和等于S对应的数的个数。

题解:

高精度存dp,dp[i][j]表示前i位和为j的个数,状态转移很容易。注意和为奇数时无解所以结果是0.

#include<iostream>
#include<math.h>
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<vector>
#include<queue>
#include<map>
#include<set>
using namespace std;
#define B(x) (1<<(x))
typedef long long ll;
void cmax(int& a,int b){ if(b>a)a=b; }
void cmin(int& a,int b){ if(b<a)a=b; }
const int oo=0x3f3f3f3f;
const int MOD=1000000007;
const int maxn=105;
struct BigInt
{
    const static int mod = 10;
    int a[maxn],len;
    BigInt()
    {
        memset(a,0,sizeof(a));
        len = 1;
    }
    BigInt(int v)
    {
        memset(a,0,sizeof(a));
        len = 0;
        do
        {
            a[len++] = v%mod;
            v /= mod;
        }while(v);
    }
    BigInt operator +(const BigInt &b)const
    {
        BigInt res;
        res.len = max(len,b.len);
        for(int i = 0;i <= res.len;i++)
            res.a[i] = 0;
        for(int i = 0;i < res.len;i++)
        {
            res.a[i] += ((i < len)?a[i]:0)+((i < b.len)?b.a[i]:0);
            res.a[i+1] += res.a[i]/mod;
            res.a[i] %= mod;
        }
        if(res.a[res.len] > 0)res.len++;
        return res;
    }

    BigInt operator *(const BigInt &b)const
    {
        BigInt res;
        for(int i = 0; i < len;i++)
        {
            int up = 0;
            for(int j = 0;j < b.len;j++)
            {
                int temp = a[i]*b.a[j] + res.a[i+j] + up;
                res.a[i+j] = temp%mod;
                up = temp/mod;
            }
            if(up != 0)
                res.a[i + b.len] = up;
        }
        res.len = len + b.len;
        while(res.a[res.len - 1] == 0 &&res.len > 1)res.len--;
        return res;
    }

    void output()
    {
        printf("%d",a[len-1]);
        for(int i = len-2;i >=0 ;i--)
            printf("%d",a[i]);
        printf("\n");
    }

    void init(){
        memset(a,0,sizeof a);
        len=1;
    }

}dp[55][505];

void CL(){
    for(int i=0;i<55;i++){
        for(int j=0;j<505;j++){
            dp[i][j].init();
        }
    }
}


int main(){
    //freopen("E:\\read.txt","r",stdin);
    int n,s;
    while(scanf("%d %d",&n,&s)!=EOF){
        if(s&1){
            printf("0\n");
            continue;
        }
        CL();
        s/=2;
        dp[0][0]=1;
        for(int i=1;i<=n;i++){
            for(int j=0;j<=s;j++){
                for(int k=0;k<=9;k++){
                    if(j-k>=0)
                        dp[i][j]=dp[i][j]+dp[i-1][j-k];
                }
            }
        }
        BigInt ans=dp[n][s]*dp[n][s];
        ans.output();
    }
	return 0;
}







你可能感兴趣的:(URAL 1036 Lucky Tickets (高精度,数位dp))