ural 1217. Unlucky Tickets

1217. Unlucky Tickets

Time limit: 1.0 second
Memory 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 are equal, they suppose such a ticket to be a lucky one. A person, who owns the lucky ticket, should dream about something, eat the ticket (no, it’s not made of chocolate, it’s made of paper!) and the dream will come true… At least, they believe it!

Strange people live in St.Petersburg! Each time in the bus, getting a ticket with a 6-digit number, they try to sum up the digits on the odd positions and the digits on the even positions. If these two sums are equal, they suppose such a ticket to be a lucky one. A person, who owns the lucky ticket, should dream about something, eat the ticket (no, even in St. Petersburg lucky tickets are not made of chocolate, they’re made of paper!) and the dream will come true… At least, they believe it!

In the "third Russian capital" — Yekaterinburg — we laugh about such strange ideas. We are practical. We are not superstitious, even a little bit. But we understand that too much luck cannot be good. Thus we consider every ticket, which is lucky both in "Moscow sense" and "St. Petersburg sense" to be unlucky. If we get an unlucky ticket in the bus, we throw it away and leave the bus immediately! Two examples of unlucky tickets are 472175 and 810513.

You are to write a program, which calculates the total number of unlucky N-digit tickets.

Input

The input contains a single even positive integer N (2 ≤ N ≤ 20) — the number of digits in the ticket. Please note, that, for example 00742544 is a valid 8-digit ticket (by the way, it is a St.Petersburg-style lucky ticket).

Output

Your program should output a single integer number — the total number of unlucky N-digit tickets.

Sample

input output
4
100

Problem Author: Leonid Volkov
Problem Source: The Seventh Ural State University collegiate programming contest

Tags: none  (hide tags for unsolved problems)

Difficulty: 396

题意:问n(n为偶数)位数里面,有多少满足1、奇数位之和和偶数位之和相同 2、前一半的数之和等于后一半的数之和

分析:简单的数位dp,dp[i][j][k]表示前i位,前一半比后一半多j,奇数位的和比偶数位的和多k(在数组里记录相对于某个数的偏移量)的有多少。

答案就是dp[n][0][0],暴力转移即可

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <cstdlib>
 4 #include <cmath>
 5 #include <deque>
 6 #include <vector>
 7 #include <queue>
 8 #include <iostream>
 9 #include <algorithm>
10 #include <map>
11 #include <set>
12 #include <ctime>
13 using namespace std;
14 typedef long long LL;
15 typedef double DB;
16 #define For(i, s, t) for(int i = (s); i <= (t); i++)
17 #define Ford(i, s, t) for(int i = (s); i >= (t); i--)
18 #define Rep(i, t) for(int i = (0); i < (t); i++)
19 #define Repn(i, t) for(int i = ((t)-1); i >= (0); i--)
20 #define rep(i, x, t) for(int i = (x); i < (t); i++)
21 #define MIT (2147483647)
22 #define INF (1000000001)
23 #define MLL (1000000000000000001LL)
24 #define sz(x) ((int) (x).size())
25 #define clr(x, y) memset(x, y, sizeof(x))
26 #define puf push_front
27 #define pub push_back
28 #define pof pop_front
29 #define pob pop_back
30 #define ft first
31 #define sd second
32 #define mk make_pair
33 inline void SetIO(string Name) {
34     string Input = Name+".in",
35     Output = Name+".out";
36     freopen(Input.c_str(), "r", stdin),
37     freopen(Output.c_str(), "w", stdout);
38 }
39
40 inline int Getint() {
41     int Ret = 0;
42     char Ch = ‘ ‘;
43     while(!(Ch >= ‘0‘ && Ch <= ‘9‘)) Ch = getchar();
44     while(Ch >= ‘0‘ && Ch <= ‘9‘) {
45         Ret = Ret*10+Ch-‘0‘;
46         Ch = getchar();
47     }
48     return Ret;
49 }
50
51 const int N = 25, M = 190;
52 int n;
53 LL Dp[N][M][M*2];
54
55 inline void Input() {
56     scanf("%d", &n);
57 }
58
59 inline void Solve() {
60     int m = n*9+1, Left, Right;
61     Left = n/2+1, Right = n/2;
62     Dp[0][0][m] = 1;
63     Rep(i, n+1)
64         Rep(j, m)
65             Rep(k, m*2)
66                 if(Dp[i][j][k]) {
67                     int x = k-m, _j, y;
68                     Rep(l, 10) {
69                         if(i+1 >= Left && l > j) break;
70                         if(i+1 <= Right) _j = j+l;
71                         else if(i+1 >= Left) _j = j-l;
72                         if((i+1)&1) y = x+l;
73                         else y = x-l;
74                         //printf("%d %d %d %d %d %d\n", i, j, k-m,i+1, _j, y);
75                         Dp[i+1][_j][y+m] += Dp[i][j][k];
76                     }
77                     //printf("%d %d %d %d\n", i, j, k-m, Dp[i][j][k]);
78                 }
79
80     cout<<Dp[n][0][m]<<endl;
81 }
82
83 int main() {
84     #ifndef ONLINE_JUDGE
85     SetIO("B");
86     #endif
87     Input();
88     Solve();
89     return 0;
90 }

时间: 2024-12-15 14:11:11

ural 1217. Unlucky Tickets的相关文章

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

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 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 1031 Railway Tickets

动态规划. 一道完全自己想然后看了眼别人给的样例测试了一下程序的题...... 题的意思就是有许多站台,给出的距离是距离第一个的,然后一个长度一个价钱,问从一个站台到另一个站台需要的最小费用 动态规划嘛... 写的是n^2的,,应该有n的....注意给的起点终点的大小关系!!! #include<stdio.h>记得开LONG LONG #include<string.h> #include <algorithm> #include <bits/stdc++.h&

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

POJ 2774 Long Long Message &amp;&amp; URAL 1517. Freedom of Choice(求最长重复子序列)

两个题目意思差不多,都是让求最长公共子串,只不过poj那个让输出长度,而URAL那个让输出一个任意的最长的子串. 解体思路: Long Long Message Time Limit: 4000MS   Memory Limit: 131072K Total Submissions: 22313   Accepted: 9145 Case Time Limit: 1000MS Description The little cat is majoring in physics in the cap

ural 1070. Local Time

1070. Local Time Time limit: 1.0 secondMemory limit: 64 MB Soon the USU team will go to Vancouver to participate in the final of the ACM International Collegiate Programming Contest. They will be to take four different planes (three changes on the wa