2015暑假多校联合---Zero Escape(变化的01背包)
题目链接
http://acm.hust.edu.cn/vjudge/contest/130883#problem/C
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.
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
题意:输入n,A,B 表示有n个数,一部分放入A中,剩余部分放入B中,或者全放入A中、B中,A中数得满足 和的每一位相加 再对和的每一位求和......直至最后变成一位数,而这个数必须和A相等,B同样如此,求有多少种分配方案?
思路:上述对A中和B中数和的运算 等价于 和对9取余,令A中的和为suma,B中和为sumb,suma+sumb==sum 则suma%9==A sumb%9==B 所以 如果(suma+sumb)%9==(A+B)%9 且 suma%9==A 那么必有sumb%9==B 所以问题得到简化,先判断(suma+sumb)%9==(A+B)%9 是否成立,若成立,才有可能把n个数分成两部分满足上述条件。 那么在满足的条件下,我们只需算取数放入A中满足条件的方案数,可以用01背包实现;
代码如下:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int mod=;
int a[];
int dp[][]; int main()
{
int T,n,A,B;
cin>>T;
while(T--)
{
int sum=;
scanf("%d%d%d",&n,&A,&B);
memset(dp,,sizeof(dp));
for(int i=;i<=n;i++)
{
scanf("%d",&a[i]);
sum+=a[i];
}
sum%=;
int res=;
if(sum==B%) res++;
if(sum==(A+B)%)
{
dp[][]=;
for(int i=;i<=n;i++)
{
for(int j=;j<=;j++)
{
if(j>=a[i]) dp[i][j]=dp[i-][j]+dp[i-][j-a[i]];
else dp[i][j]=dp[i-][j]+dp[i-][j+-a[i]];
dp[i][j]%=mod;
}
}
printf("%d\n",(dp[n][A]+res)%mod);
}
else printf("%d\n",(sum==A%)+res);
}
return ;
}
2015暑假多校联合---Zero Escape(变化的01背包)的更多相关文章
- 2015暑假多校联合---CRB and His Birthday(01背包)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5410 Problem Description Today is CRB's birthda ...
- 2015暑假多校联合---Expression(区间DP)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5396 Problem Description Teacher Mai has n numb ...
- 2015暑假多校联合---Mahjong tree(树上DP 、深搜)
题目链接 http://acm.split.hdu.edu.cn/showproblem.php?pid=5379 Problem Description Little sun is an artis ...
- 2015暑假多校联合---Cake(深搜)
题目链接:HDU 5355 http://acm.split.hdu.edu.cn/showproblem.php?pid=5355 Problem Description There are m s ...
- 2015暑假多校联合---Friends(dfs枚举)
原题链接 Problem Description There are n people and m pairs of friends. For every pair of friends, they ...
- 2015暑假多校联合---Assignment(优先队列)
原题链接 Problem Description Tom owns a company and he is the boss. There are n staffs which are numbere ...
- 2015暑假多校联合---Problem Killer(暴力)
原题链接 Problem Description You are a "Problem Killer", you want to solve many problems. Now ...
- 2016暑假多校联合---Rikka with Sequence (线段树)
2016暑假多校联合---Rikka with Sequence (线段树) Problem Description As we know, Rikka is poor at math. Yuta i ...
- 2016暑假多校联合---Windows 10
2016暑假多校联合---Windows 10(HDU:5802) Problem Description Long long ago, there was an old monk living on ...
随机推荐
- ssh文件传输命令:sz与rz命令
内容概要: rz 上传文件到linux服务器,会出现选择框 sz filename 下载linux文件到windows 一般来说,linux服务器大多是通过ssh客户端来进行远程的登陆和管理的,使用s ...
- JS脚本
js脚本是嵌在网页里打出的一块区域,一般写在最下端 script 脚本 // 这是单行注释的一种语法 /**/ 这是多行注释的一种语法 存储内容的东西叫变量 数据类型的有: 1 整型 ...
- 每天一个linux命令(13):less 命令
less 工具也是对文件或其它输出进行分页显示的工具,应该说是linux正统查看文件内容的工具,功能极其强大.less 的用法比起 more 更加的有弹性.在 more 的时候,我们并没有办法向前面翻 ...
- cordova添加plugin
cordova添加plugin #在线安装 cordova create chankoujie com.example.chankoujie ChanKouJie cordova plugin add ...
- Nexus3.0私服搭建
官方文档:http://books.sonatype.com/nexus-book/3.0/reference/install.html 1.下载 http://www.sonatype.com/do ...
- java中异常注意问题(发生在多态是的异常问题)
/* 异常的注意事项: 1,子类在覆盖父类方法时,父类的方法如果抛出了异常,那么子类的方法只能抛出父类的异常或者该异常的子类. 2,如果父类抛出多个异常,那么子类只能抛出父类异常的子集. 简单说:子类 ...
- 漫谈可视化Prefuse(五)---一款属于我自己的可视化工具
伴随着前期的基础积累,翻过API,读过一些Demo,总觉得自己已经摸透了Prefuse,小打小闹似乎已经无法满足内心膨胀的自己.还记得儿时看的<武状元苏乞儿>中降龙十八掌最后一张居然是空白 ...
- HTML5本地存储 Web Storage
Web Storage基本介绍 HTML5 定义了本地存储规范 Web Storage , 提供了两种存储类型 API sessionStorage 和 localStorage,二者的差异主要是数 ...
- angularjs学习总结 详细教程(转载)
1 前言 前端技术的发展是如此之快,各种优秀技术.优秀框架的出现简直让人目不暇接,紧跟时代潮流,学习掌握新知识自然是不敢怠慢. AngularJS是google在维护,其在国外已经十分火热,可是国内的 ...
- Windows Azure Virtual Machine (33) Azure虚拟机删除重建
<Windows Azure Platform 系列文章目录> 注意:本文介绍的重建Azure虚拟机,必须在同一个订阅内删除重建,必须在同一个订阅内删除重建,必须在同一个订阅内删除重建. ...