URAL 1035 Lucky Tickets

题意:长度为2n的数字,前N位之和和后面的一样,,,加一起是s........问有多少种不同的数字

首先s是奇数肯定就不行了。。。。

然后n*2*9<s也不行了。。。。。。

dp[i][j]+=dp[i-1][j-k];就是加的这位不同的情况·~~~

这题要用高精度,,,,

#include<stdio.h>

#include<string.h>

#include <algorithm>

#include <bits/stdc++.h>

using namespace std;

const int N = 550;

const int M = 50;

const int inf = 10000;

int dp[M+5][N][M+5];

void add(int a[M+1], int b[M+1]){

int i;

for(i = 1; i <= M; i++){

a[i] += b[i];

}

for(i = 1; i <= M; i++){

a[i+1] += a[i]/inf;

a[i] %= inf;

}

}

void mul(int a[M+1], int b[M+1]){

int c[2*M+1], i, j;

memset(c, 0, sizeof(c));

for(i = 1; i <= M; i++)

for(j = 1; j <= M; j++)

c[i+j-1] += a[i]*b[j];

for(i = 1; i <= M; i++){

c[i+1] += c[i]/inf;

c[i] %= inf;

}

for(i = 1; i <= M; i++)

a[i] = c[i];

}

void display(int a[M+1]){

int j, i = M;

while(a[i] == 0 && i > 0)    i--;

if(i == 0)    printf("0\n");

else{

printf("%d", a[i]);

for(j = i-1; j > 0; j--){

printf("%04d", a[j]);

}

}

printf("\n");

}

int main()

{

int n, s, i, j, k;

while(~scanf("%d%d", &n, &s))

{

if(s&1)

{

printf("0\n");

continue;

}

else

{

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

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

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

for(i = 1; i <= n; i++)

for(j = 0; j <= s/2; j++)

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

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

mul(dp[n][s/2], dp[n][s/2]);

display(dp[n][s/2]);

}

}

return 0;

}



版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-10-24 18:30:23

URAL 1035 Lucky Tickets的相关文章

DP+高精度 URAL 1036 Lucky Tickets

题目传送门 1 /* 2 题意:转换就是求n位数字,总和为s/2的方案数 3 DP+高精度:状态转移方程:dp[cur^1][k+j] = dp[cur^1][k+j] + dp[cur][k]; 4 高精度直接拿JayYe的:) 5 异或运算的规则: 6 0⊕0=0,0⊕1=1 7 1⊕0=1,1⊕1=0 8 口诀:相同取0,相异取1 9 */ 10 #include <cstdio> 11 #include <cstring> 12 #include <string>

Ural 1036 Lucky Tickets

Lucky Tickets Time Limit: 2000ms Memory Limit: 16384KB This problem will be judged on Ural. Original ID: 103664-bit integer IO format: %lld      Java class name: (Any) You are given a number 1 ≤ N ≤ 50. Every ticket has its 2N-digit number. We call a

URAL 1044 Lucky Tickets. Easy!

算是个动态规划,统计和 sum[位数][差值] 构建个虚拟数组写起来就顺多了 1 import java.util.Scanner; 2 3 public class P1044 4 { 5 private static int save[][] = new int[10][100]; 6 7 private static int getSum(int n, int deta) 8 { 9 return save[n][deta + 50]; 10 } 11 12 private static

ural 1217. Unlucky Tickets

1217. Unlucky Tickets Time limit: 1.0 secondMemory limit: 64 MB Strange people live in Moscow! Each time in the bus, getting a ticket with a 6-digit number, they try to sum up the first half of digits and the last half of digits. If these two sums ar

寒假集训.Lucky Tickets. Easy!

Lucky Tickets. Easy! Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description Background The public transport administration of Ekaterinburg is anxious about the fact that passengers don't like to pay for

递推DP URAL 1031 Railway Tickets

题目传送门 1 /* 2 简单递推DP:读题烦!在区间内的都更新一遍,dp[]初始化INF 3 注意:s1与s2大小不一定,坑! 4 详细解释:http://blog.csdn.net/kk303/article/details/6847948 5 */ 6 #include <cstdio> 7 #include <iostream> 8 #include <algorithm> 9 #include <cstring> 10 #include <s

Lucky Tickets URAL - 1036 dp+大数

用b[i][j]表示递推到第i位时数字和为j的方案数,那么总方案数就是b[n][s/2]的平方 1 n, s = input().split(' ') 2 3 n = int(n) 4 s = int(s) 5 6 if s % 2 == 1: 7 print(0) 8 else: 9 s = int(s // 2) 10 b = [[]] 11 a = [] 12 for i in range(0, 10): 13 a.append(1) 14 for i in range(10, s + 1

【数位DP】Codeforces Gym 100418J Lucky tickets

题意: 设性质P:一个数能够整除它二进制表示下的1的个数.求[1,N]中满足性质P的数的个数.N<=1019. 思路: 数位DP.首先这个数最多有64位,我们可以枚举1的个数x,然后求可以整除x的数的个数.设dp[i][j][k][w]表示从最高位枚举到i位,现在已经构成的数模x余多少(这里是关键,只用考虑余数),现在已经用了k个1,w=0表示现在枚举的这个数已经小于N了,w=1表示从最高位到第i位都与N相同(数位dp计算时的方法:如果当前枚举的数小于N了,那么枚举的这一位就可以任意,如果等于N

poj 2346 Lucky tickets

题目链接:http://poj.org/problem?id=2346 思路:     使用动态规划解法: 设函数 d( n, x )代表长度为n且满足左边n/2位的和减去右边n/2位的和为x的数的数目. 将一个长度为n的数看做n个数字 A1, A2....An ( 0 <= Ai <= 9  ),将字符分为两个集合{ A1, A2....An/2 } 与 { An/2+1.....An }; 问题转换为求集合中元素和相等的数目.先从每个集合中选出一个元素,求它们差为a的可能数目,即d( 2,