HDU 5389 Zero Escape(DP + 滚动数组)
数组 HDU DP 滚动 Zero escape
2023-09-11 14:14:43 时间
Zero Escape
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 864 Accepted Submission(s): 438
Problem Description
Zero Escape, is a visual novel adventure video game directed by Kotaro Uchikoshi (you may hear about ever17?) and developed by Chunsoft.
Stilwell is enjoying the first chapter of this series, and in this chapter digital root is an important factor.
This is the definition of digital root on Wikipedia:
The digital root of a non-negative integer is the single digit value obtained by an iterative process of summing digits, on each iteration using the result from the previous iteration to compute a digit sum. The process continues until a single-digit number is reached.
For example, the digital root of65536 is 7 ,
because 6+5+5+3+6=25 and 2+5=7 .
In the game, every player has a special identifier. Maybe two players have the same identifier, but they are different players. If a group of players want to get into a door numberedX(1≤X≤9) ,
the digital root of their identifier sum must be X .
For example, players{1,2,6} can
get into the door 9 ,
but players {2,3,3} can't.
There is two doors, numberedA and B .
Maybe A=B ,
but they are two different door.
And there isn players,
everyone must get into one of these two doors. Some players will get into the door A ,
and others will get into the door B .
For example:
players are{1,2,6} , A=9 , B=1
There is only one way to distribute the players: all players get into the door9 .
Because there is no player to get into the door 1 ,
the digital root limit of this door will be ignored.
Given the identifier of every player, please calculate how many kinds of methods are there,mod 258280327 .
Stilwell is enjoying the first chapter of this series, and in this chapter digital root is an important factor.
This is the definition of digital root on Wikipedia:
The digital root of a non-negative integer is the single digit value obtained by an iterative process of summing digits, on each iteration using the result from the previous iteration to compute a digit sum. The process continues until a single-digit number is reached.
For example, the digital root of
In the game, every player has a special identifier. Maybe two players have the same identifier, but they are different players. If a group of players want to get into a door numbered
For example, players
There is two doors, numbered
And there is
For example:
players are
There is only one way to distribute the players: all players get into the door
Given the identifier of every player, please calculate how many kinds of methods are there,
Input
The first line of the input contains a single number T ,
the number of test cases.
For each test case, the first line contains three integersn , A and B .
Next line containsn integers idi ,
describing the identifier of every player.
T≤100 , n≤105 , ∑n≤106 , 1≤A,B,idi≤9
For each test case, the first line contains three integers
Next line contains
Output
For each test case, output a single integer in a single line, the number of ways that these n players
can get into these two doors.
Sample Input
4 3 9 1 1 2 6 3 9 1 2 3 3 5 2 3 1 1 1 1 1 9 9 9 1 2 3 4 5 6 7 8 9
Sample Output
1 0 10 60
Author
SXYZ
Source
题意:给出n个人的id,有两个门,每一个门有一个标号,我们记作a和b,如今我们要将n个人分成两组。进入两个门中,使得两部分人的标号的和(迭代的求,直至变成一位数)各自等于a和b,问有多少种分法,(能够全部的人进入一个门)。
pt = j - p[i];
状态转移方程: dp[i][j] = dp[i-1][j] + dp[i-1][pt];
两种处理方法:
一
#include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> #include<stdlib.h> using namespace std; const int N = 100001; const int mod = 258280327; int dp[N][10]; int n,a,b; int p[N]; int num(int xx,int yy) { int t = xx + yy; if(t%9 == 0) { return 9; } return t%9; } int pnum(int xx,int yy) { int tt = xx - yy; if(tt%9 == 0) { return 9; } if(tt%9<0) { return 9+(tt%9); } return tt%9; } int main() { int T; scanf("%d",&T); while(T--) { int sum = 0; scanf("%d%d%d",&n,&a,&b); for(int i=1;i<=n;i++) { scanf("%d",&p[i]); sum = num(sum,p[i]); } memset(dp,0,sizeof(dp)); dp[0][0] = 1; for(int i=1;i<=n;i++) { for(int j=0;j<=9;j++) { dp[i][j] += dp[i-1][j]; dp[i][j] = dp[i][j]%mod; int pt = pnum(j,p[i]); if(pt == 9) { dp[i][j] += max(dp[i-1][0],dp[i-1][9]); } else { dp[i][j] += dp[i-1][pnum(j,p[i])]; } dp[i][j] = dp[i][j]%mod; } } int ans = 0; if(num(a,b) == sum) { ans = dp[n][a]; if(a == sum) { ans--; } } if(a == sum) { ans++; } if(b == sum) { ans++; } printf("%d\n",ans); } return 0; }
二
#include<iostream> #include<algorithm> #include<stdio.h> #include<string.h> #include<stdlib.h> using namespace std; const int N = 100001; const int mod = 258280327; int dp[N][10]; int n,a,b; int p[N]; int num(int xx,int yy) { int t = xx + yy; if(t%9 == 0) { return 9; } return t%9; } int main() { int T; scanf("%d",&T); while(T--) { int sum = 0; scanf("%d%d%d",&n,&a,&b); for(int i=1;i<=n;i++) { scanf("%d",&p[i]); sum = num(sum,p[i]); } memset(dp,0,sizeof(dp)); dp[0][0] = 1; for(int i=1;i<=n;i++) { for(int j=0;j<=9;j++) { int pt = num(j,p[i]); dp[i][j]+=dp[i-1][j]; dp[i][pt]+=dp[i-1][j]; dp[i][j]%=mod; dp[i][pt]%=mod; } } int ans = 0; if(num(a,b) == sum) { ans = dp[n][a]; if(a == sum) { ans--; } } if(a == sum) { ans++; } if(b == sum) { ans++; } printf("%d\n",ans); } return 0; }
相关文章
- shell编程----函数、数组
- php 通过array_merge()和array+array合并数组的区别和效率比较
- 如何处理数组越界而不会让程序崩溃?
- 【死磕jeestie源码】类型后面三个点(String...)和数组(String[])的区别
- Java实现 LeetCode 659 分割数组为连续子序列 (哈希)
- Java实现最大连续乘积子数组
- HDU 1166 敌兵布阵(树状数组)
- 通过数组看栈堆
- 分享五:php数组操作
- javascript中数组揭秘
- 【LabVIEW】数据类型-数组和簇 (簇考虑另开)
- 合并两个有序数组
- [PHP]快速实现:将二维数组转为一维数组
- 【阶段二】Python数据分析NumPy工具使用01篇:NumPy工具介绍、NumPy工具安装与数组的创建
- 当前插入的线段能完整覆盖存在的几条线段 树状数组 HDU 5372 Segment Game
- HDU 2838 (树状数组求逆序数)
- 【华为机试真题 Python实现】数组的转换
- 108. 将有序数组转换为二叉搜索树
- 面试题 10.03. 搜索旋转数组
- Leetcode 1389. 按既定顺序创建目标数组
- Go语言自学系列 | golang数组
- sql array 数组基本用法(一)
- Saving HDU hdu
- 数据分析----numpy数组的索引和切片操作