Zero Escape

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)

Total Submission(s): 56    Accepted Submission(s): 18

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 of 65536
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, numbered A
and B.
Maybe A=B,
but they are two different door.

And there is n
players, everyone must get into one of these two doors. Some players will get into the doorA,
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 door
9.
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.

 
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 integers
n,A
and B.

Next line contains n
integers idi,
describing the identifier of every player.

T≤100,n≤105,∑n≤106,1≤A,B,idi≤9
 
Output
For each test case, output a single integer in a single line, the number of ways that thesen
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

把N个数分成两组。一组加起来是A,一组加起来是B,1<=A,B<=9,也能够全分到同一组。当中加是依照他给的规则加。就是一位一位加。超过一位数了再拆分成一位一位加。

由于把N个数全加起来再依照那个规则处理和两个两个加是一样的。用dp[i][j][k]表示前i个数分两组。第一组和为j,第二组和为k有多少种,直接依据a[i]和dp[i-1]的情况递推即可了(假设当前和为j,这一位是a[i],若j>a[i],上一位要取的是j-a[i],否则上一位是9-(a[i]-j),尽管和一般加法不一样,但也差不了多少)。这里N非常大,用滚动数组。但我一開始交上去超时,然后发现j和k不须要两重循环。由于前i个数的和是确定的,那么假设j确定了。k也确定了,所以能够先预处理前缀和(按他这样的加法规则的和)。每次依据j直接算出k。这里特别要注意j,k等于0的情况,进行特殊处理。

#include<cstring>
#include<cstdio>
#include<iostream>
#include<cmath>
#include<algorithm>
#include<queue>
#define INF 0x3f3f3f3f
#pragma comment(linker, "/STACK:1024000000,1024000000")
using namespace std; typedef long long LL; const int MAXN=100010;
const LL MOD= 258280327; int T,N,A,B;
int a[MAXN],sum[MAXN];
LL dp[2][10][10]; int cal(int i,int j){
if(j>i) return j-i;
return 9-(i-j);
} int main(){
scanf("%d",&T);
while(T--){
scanf("%d%d%d",&N,&A,&B);
memset(dp,0,sizeof(dp));
sum[0]=0;
for(int i=1;i<=N;i++){
scanf("%d",&a[i]);
sum[i]=sum[i-1]+a[i];
if(sum[i]>=10) sum[i]-=9;
}
int cur=0;
dp[cur][a[1]][0]=1;
dp[cur][0][a[1]]=1;
for(int i=2;i<=N;i++){
cur=!cur;
memset(dp[cur],0,sizeof(dp[cur]));
for(int j=0;j<=9;j++){
int k;
if(j==0) k=sum[i];
else k=cal(j,sum[i]);
if(j==a[i]) dp[cur][j][k]=(dp[cur][j][k]+dp[!cur][0][k])%MOD;
if(k==a[i]) dp[cur][j][k]=(dp[cur][j][k]+dp[!cur][j][0])%MOD;
if(j>0){
int t=cal(a[i],j);
dp[cur][j][k]=(dp[cur][j][k]+dp[!cur][t][k])%MOD;
}
if(k>0){
int t=cal(a[i],k);
dp[cur][j][k]=(dp[cur][j][k]+dp[!cur][j][t])%MOD;
}
//j==sum[i]时k可能为0
if(j==sum[i]){
k=0;
if(j==a[i]) dp[cur][j][k]=(dp[cur][j][k]+dp[!cur][0][k])%MOD;
if(k==a[i]) dp[cur][j][k]=(dp[cur][j][k]+dp[!cur][j][0])%MOD;
if(j>0){
int t=cal(a[i],j);
dp[cur][j][k]=(dp[cur][j][k]+dp[!cur][t][k])%MOD;
}
if(k>0){
int t=cal(a[i],k);
dp[cur][j][k]=(dp[cur][j][k]+dp[!cur][j][t])%MOD;
}
}
}
}
LL ans=(dp[cur][A][0]+dp[cur][0][B]+dp[cur][A][B])%MOD;
printf("%I64d\n",ans);
}
return 0;
}

hdu5389 Zero Escape DP+滚动数组 多校联合第八场的更多相关文章

  1. HDU 5389 Zero Escape(DP + 滚动数组)

    Zero Escape Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) To ...

  2. HDU 1024 Max Sum Plus Plus --- dp+滚动数组

    HDU 1024 题目大意:给定m和n以及n个数,求n个数的m个连续子系列的最大值,要求子序列不想交. 解题思路:<1>动态规划,定义状态dp[i][j]表示序列前j个数的i段子序列的值, ...

  3. POJ 3666 Making the Grade (DP滚动数组)

    题意:农夫约翰想修一条尽量平缓的路,路的每一段海拔是A[i],修理后是B[i],花费|A[i] – B[i]|,求最小花费.(数据有问题,代码只是单调递增的情况) #include <stdio ...

  4. HDU 5119 Happy Matt Friends (背包DP + 滚动数组)

    题目链接:HDU 5119 Problem Description Matt has N friends. They are playing a game together. Each of Matt ...

  5. USACO 2009 Open Grazing2 /// DP+滚动数组oj26223

    题目大意: 输入n,s:n头牛 s个栅栏 输入n头牛的初始位置 改变他们的位置,满足 1.第一头与最后一头的距离尽量大 2.相邻两头牛之间的距离尽量满足 d=(s-1)/(n-1),偏差不超过1 3. ...

  6. hdu5379||2015多校联合第7场1011 树形统计

    pid=5379">http://acm.hdu.edu.cn/showproblem.php? pid=5379 Problem Description Little sun is ...

  7. BZOJ-1925 地精部落 烧脑DP+滚动数组

    1925: [Sdoi2010]地精部落 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1053 Solved: 633 [Submit][Status ...

  8. Codeforces 712 D. Memory and Scores (DP+滚动数组+前缀和优化)

    题目链接:http://codeforces.com/contest/712/problem/D A初始有一个分数a,B初始有一个分数b,有t轮比赛,每次比赛都可以取[-k, k]之间的数,问你最后A ...

  9. hdu 1513 && 1159 poj Palindrome (dp, 滚动数组, LCS)

    题目 以前做过的一道题, 今天又加了一种方法 整理了一下..... 题意:给出一个字符串,问要将这个字符串变成回文串要添加最少几个字符. 方法一: 将该字符串与其反转求一次LCS,然后所求就是n减去 ...

随机推荐

  1. Html5 ajax的跨域请求

    1.XMLHttpRequest升级版已经实现了跨域请求.不过需要在后台设置:header("Access-Control-Allow-Origin:http://www.a.com&quo ...

  2. Python 33(2)进程理论

    一:什么是进程         进程指的是一个正在进行 / 运行的程序,进程是用来描述程序执行过程的虚拟概念 进程vs程序 程序:一堆代码 进程:程序的执行的过程 进程的概念起源于操作系统,进程是操作 ...

  3. POJ 1118 求平面上最多x点共线

    题意:给你n个点的坐标.求一条直线最多能穿过多少个点. 思路:枚举(n^2)+求斜率+排序 (复杂度n^2logn)大功告成 //By: Sirius_Ren #include <cmath&g ...

  4. 自学Python十 爬虫实战三(美女福利续)

    我又来送福利啦!!!不同于上篇文章,这次我们的爬虫采用了多线程,一直以来被所谓的分布式  多线程  爬虫 给唬的怕怕的.今天就来一发多线程爬虫吧,还能看妹子图,想想就觉得很激动!!! 依然是流程解释: ...

  5. Get 和 Post

    理论: Http定义了与服务器交互的不同方法,最基本的方法有4种,分别是GET,POST,PUT,DELETE.URL全称是资源描述符,我们可以这样认为:一个URL地址,它用于描述一个网络上的资源,而 ...

  6. Windows 下MySQL zip 安装

    主要步骤: 1.下载解压到安装的文件夹 2.配置环境路径 3.配置my.ini文件,设置程序路径和数据存储路径 4.以管理员身份启动Mysqld install(提示sevice安装成功) 5.启动M ...

  7. 【Oracle】to_char技巧

    Select to_char(sysdate,'ss') from dual;      取当前时间秒部分 Select to_char(sysdate,'mi') from dual;      取 ...

  8. 常用SQL函数

    —————常用SQL函数(实例简述)————— 数据库环境:DB2数据库: 执行工具:Toad for  DB2 1.转字符串:to_char() 日期类型:to_char(birthday,'yyy ...

  9. QT-Creator+SDK+编译器+自定义配置

    QT4.8的软件曾经耗费巨大的功夫进行构建,不舍得扔掉!重新安装Qt4.8版本 1.安装qt-creator 安装qt-creator-win-opensource-2.4.0.exe版本,不建议使用 ...

  10. 【转载】java中的反射

    主要介绍以下几方面内容 理解 Class 类 理解 Java 的类加载机制 学会使用 ClassLoader 进行类加载 理解反射的机制 掌握 Constructor.Method.Field 类的用 ...