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>
 13 #include <iostream>
 14 #include <algorithm>
 15 using namespace std;
 16
 17 const int numlen = 1005;
 18 const int numbit = 4;
 19 const int addbit = 10000;
 20 const int maxn = numlen/numbit + 10;
 21
 22 struct bign {
 23     int len, s[numlen];
 24     bign() {
 25         memset(s, 0, sizeof(s));
 26         len = 1;
 27     }
 28     bign(int num) { *this = num; }
 29     bign(const char *num) { *this = num; }
 30     bign operator = (const int num) {
 31         char s[numlen];
 32         sprintf(s, "%d", num);
 33         *this = s;
 34         return *this;
 35     }
 36     bign operator = (const char *num){
 37         int clen = strlen(num);
 38         while(clen > 1 && num[0] == ‘0‘)    num++, clen--;
 39         len = 0;
 40         for(int i = clen-1;i >= 0; i -= numbit) {
 41             int top = min(numbit, i+1), mul = 1;
 42             s[len] = 0;
 43             for(int j = 0;j < top; j++) {
 44                 s[len] += (num[i-j]-‘0‘)*mul;
 45                 mul *= 10;
 46             }
 47             len++;
 48         }
 49         deal();
 50         return *this;
 51     }
 52     void deal() {
 53         while(len > 1 && !s[len-1]) len--;
 54     }
 55     bign operator + (const bign &a) const {
 56         bign ret;
 57         ret.len = 0;
 58         int top = max(len, a.len), add = 0;
 59         for(int i = 0;add || i < top; i++) {
 60             int now = add;
 61             if(i < len) now += s[i];
 62             if(i < a.len)   now += a.s[i];
 63             ret.s[ret.len++] = now%addbit;
 64             add = now/addbit;
 65         }
 66         return ret;
 67     }
 68     bign operator * (const bign &a)const {
 69         bign ret;
 70         ret.len = len + a.len;
 71         for(int i = 0;i < len; i++) {
 72             int pre = 0;
 73             for(int j = 0;j < a.len; j++) {
 74                 int now = s[i]*a.s[j] + pre;
 75                 pre = 0;
 76                 ret.s[i+j] += now;
 77                 if(ret.s[i+j] >= addbit) {
 78                     pre = ret.s[i+j]/addbit;
 79                     ret.s[i+j] -= pre*addbit;
 80                 }
 81             }
 82             if(pre) ret.s[i+a.len] = pre;
 83         }
 84         ret.deal();
 85         return ret;
 86     }
 87 }dp[2][1005];
 88 istream& operator >> (istream &in, bign &x) {
 89     string s;
 90     in >> s;
 91     x = s.c_str();
 92     return in;
 93 }
 94 ostream& operator << (ostream &out, const bign &x) {
 95     printf("%d", x.s[x.len-1]);
 96     for(int i = x.len-2;i >= 0; i--)    printf("%04d", x.s[i]);
 97     return out;
 98 }
 99
100 int main(void)        //URAL 1036 Lucky Tickets
101 {
102     //freopen ("W.in", "r", stdin);
103
104     int n, s;
105     while (scanf ("%d%d", &n, &s) == 2)
106     {
107         if (s & 1)    {puts ("0");    continue;}
108
109         dp[0][0] = 1;
110         int cur = 0;
111         for (int i=1; i<=n; ++i)
112         {
113             for (int j=0; j<=s/2; ++j)    dp[cur^1][j] = 0;
114             for (int j=0; j<=9; ++j)
115             {
116                 for (int k=0; k+j<=s/2; ++k)
117                     dp[cur^1][k+j] = dp[cur^1][k+j] + dp[cur][k];
118             }
119             cur ^= 1;
120         }
121
122         cout << dp[cur][s/2] * dp[cur][s/2] << endl;
123     }
124
125     return 0;
126 }
时间: 2024-12-26 18:18:49

DP+高精度 URAL 1036 Lucky Tickets的相关文章

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 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 n

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 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 digi

递推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

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

poj 1625 Censored!(AC自动机+DP+高精度)

题目链接:poj 1625 Censored! 题目大意:给定N,M,K,然后给定一个N字符的字符集和,现在要用这些字符组成一个长度为M的字符串,要求不包 括K个子字符串. 解题思路:AC自动机+DP+高精度.这题恶心的要死,给定的不能匹配字符串里面有负数的字符情况,也算是涨姿势 了,对应每个字符固定偏移128单位. #include <cstdio> #include <cstring> #include <queue> #include <vector>

【bzoj2764】[JLOI2011]基因补全 dp+高精度

题目描述 在生物课中我们学过,碱基组成了DNA(脱氧核糖核酸),他们分别可以用大写字母A,C,T,G表示,其中A总与T配对,C总与G配对.两个碱基序列能相互匹配,当且仅当它们等长,并且任意相同位置的碱基都是能相互配对的.例如ACGTC能且仅能与TGCAG配对.一个相对短的碱基序列能通过往该序列中任意位置补足碱基来与一个相对长的碱基序列配对.补全碱基的位置.数量不同,都将视为不同的补全方案.现在有两串碱基序列S和T,分别有n和m个碱基(n>=m),问一共有多少种补全方案. 输入 数据包括三行. 第