hdu5389 Zero Escape(动态规划)

题目:

Zero Escape

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

Total Submission(s): 1209    Accepted Submission(s): 594

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 numbered
X(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 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 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 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

2015 Multi-University Training Contest 8

Recommend

wange2014

题意:有n个人,每个人有自己的编号,可以相同,有两个门也有自己的编号,可以相同,一群人能够通过一个门当且仅当他们的编号之和的数字根等于门的编号,问有多少种使得这n个人通过这两个门的方案。

思路:动态规划,01背包变种,每个人要么选A门,要么选B门,dp[i][j]表示前i个人数字根为j的方案数,那么转移方程就是dp[i][j]=dp[i-1][j]+dp[i-1][(j-idx(i)+9)%9],最终答案就是dp[n][A],注意如果可以全部从B门通过,答案应该加一。

代码:

#include <cstdlib>
#include <cctype>
#include <cstring>
#include <cstdio>
#include <cmath>
#include<climits>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <sstream>
#include <map>
#include <set>
#include <queue>
#include <stack>
#include <fstream>
#include <numeric>
#include <iomanip>
#include <bitset>
#include <list>
#include <stdexcept>
#include <functional>
#include <utility>
#include <ctime>
using namespace std;

#define PB push_back
#define MP make_pair

#define REP(i,x,n) for(int i=x;i<(n);++i)
#define FOR(i,l,h) for(int i=(l);i<=(h);++i)
#define FORD(i,h,l) for(int i=(h);i>=(l);--i)
#define SZ(X) ((int)(X).size())
#define ALL(X) (X).begin(), (X).end()
#define RI(X) scanf("%d", &(X))
#define RII(X, Y) scanf("%d%d", &(X), &(Y))
#define RIII(X, Y, Z) scanf("%d%d%d", &(X), &(Y), &(Z))
#define DRI(X) int (X); scanf("%d", &X)
#define DRII(X, Y) int X, Y; scanf("%d%d", &X, &Y)
#define DRIII(X, Y, Z) int X, Y, Z; scanf("%d%d%d", &X, &Y, &Z)
#define OI(X) printf("%d",X);
#define RS(X) scanf("%s", (X))
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
#define F first
#define S second
#define Swap(a, b) (a ^= b, b ^= a, a ^= b)
#define Dpoint  strcut node{int x,y}
#define cmpd int cmp(const int &a,const int &b){return a>b;}

 /*#ifdef HOME
    freopen("in.txt","r",stdin);
    #endif*/
const int MOD = 1e9+7;
typedef vector<int> VI;
typedef vector<string> VS;
typedef vector<double> VD;
typedef long long LL;
typedef pair<int,int> PII;
//#define HOME

int Scan()
{
	int res = 0, ch, flag = 0;

	if((ch = getchar()) == '-')				//判断正负
		flag = 1;

	else if(ch >= '0' && ch <= '9')			//得到完整的数
		res = ch - '0';
	while((ch = getchar()) >= '0' && ch <= '9' )
		res = res * 10 + ch - '0';

	return flag ? -res : res;
}
/*----------------PLEASE-----DO-----NOT-----HACK-----ME--------------------*/

#define MAXN 100000
#define mod 258280327
int id[MAXN+5];
int dp[MAXN+5][10];
int main()
{int T;
RI(T);
while(T--)
{
    int n,A,B;
    RIII(n,A,B);
    int sum=0;
    for(int i=1;i<=n;i++)
        {RI(id[i]);
        sum+=id[i];}
    sum%=9;
    MS0(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-id[i])%9+9)%9]+dp[i-1][j])%mod;
    int ans=dp[n][A];
    if(sum==B%9)
    ans=(ans+1)%mod;
    printf("%d\n",ans);

}

        return 0;
}

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

时间: 2024-08-11 12:14:54

hdu5389 Zero Escape(动态规划)的相关文章

hdu5389 Zero Escape

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

hdu5389 Zero Escape DP+滚动数组 多校联合第八场

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 Uchikos

[hdu5389 Zero Escape]数根的性质,DP

题意:把n个数(1-9)放到A集合和B集合里面去,使得A集合里面的数的数根为a,B集合里面的数的数根为b,也可以只放在A或B任一个集合里面.求方法总数.比如A={2,4,5},则A的数根为[2+4+5]=[11]=[2]=2 思路:一个数为a,则它的数根b=(a-1)%9+1=(digit-1)%9+1,digit是a的十进制各位上的数的和.如果存在解,那么任选一些数放到A集合里面,使得A集合的数根为a,那么B集合的数根一定为b.由公式可知,数根可以转化为余数来做,令dp[i][x]表示考虑前i

HDU 5389 Zero Escape(2015年多校联合第八场 动态规划)

题目连接:传送门 题意: 讲一个长度为n的序列分为两组,使得一组的和为A,一组的和为B. 注意这里的和定义为这些数的和的数根. 一个数的数根的计算公式为,root = (x-1)%9+1; 很明显一个正整数的数根是1~9的分析,如果这n个数的数根分成两组使得 一组的数根为A,一组的数根为B那么这两组的数的和的数根等于(A+B)的 数根.因此我们只需要考虑组成其中一个数的情况,然后再最后进行一个 判断即可我们设dp[i][j]表示前i个数组成的数根为j的数目. 注意其中任意一组可以为空. 代码如下

hdu5389(2015多校8)--Zero Escape(dp)

题目链接:点击打开链接 题目大意:有A和B两个门,每个门上有一个数字,现在有n个人,每个人手里也有一个数字,现在n个人都要选择一个门进去,要求计算门内的人的数字每一位的和,一直累加到一位.这个数和门代表的数字是相同的.问n个人有多少种走法. 例如 65536 -> 25 -> 7 输入n A B,之后一行输入n个数字代表n个人手中的数字.每个数字都是大于0的 输出种类个数. 对于累加每一位的和到一位的值 = 初始数的每一位的和%9,其中0代表累加的结果是9 首先判断所有人的和%9,是不是(A+

【DP专辑】ACM动态规划总结

转载请注明出处,谢谢.   http://blog.csdn.net/cc_again?viewmode=list          ----------  Accagain  2014年5月15日 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间效率高,代码量少,多元性强,主要考察思维能力.建模抽象能力.灵活度. 本人动态规划博客地址:http://blog.csdn.net/cc_again/article/category/1261899 ******************

(转)dp动态规划分类详解

dp动态规划分类详解 转自:http://blog.csdn.NET/cc_again/article/details/25866971 动态规划一直是ACM竞赛中的重点,同时又是难点,因为该算法时间效率高,代码量少,多元性强,主要考察思维能力.建模抽象能力.灵活度. ****************************************************************************************** 动态规划(英语:Dynamic programm

Leetcode 494 Target Sum 动态规划 背包+滚动数据

这是一道水题,作为没有货的水货楼主如是说. 题意:已知一个数组nums {a1,a2,a3,.....,an}(其中0<ai <=1000(1<=k<=n, n<=20))和一个数S c1a1c2a2c3a3......cnan = S, 其中ci(1<=i<=n)可以在加号和减号之中任选. 求有多少种{c1,c2,c3,...,cn}的排列能使上述等式成立. 例如: 输入:nums is [1, 1, 1, 1, 1], S is 3. 输出 : 5符合要求5种

活动选择的贪心算法与动态规划(未完成)

// greedy_algorithm.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include<iostream> #include<queue> using namespace std; #define NofActivity 11 int c[NofActivity + 1][NofActivity + 1]; int reme[NofActivity + 1][NofActivity + 1]; //活动的