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

inputoutput
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;
}

 

转载于:https://www.cnblogs.com/woshijishu3/p/3888639.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值