Divisibility
Description
17 + 5 + -21 - 15 = -14
17 + 5 - -21 + 15 = 58
17 + 5 - -21 - 15 = 28
17 - 5 + -21 + 15 = 6
17 - 5 + -21 - 15 = -24
17 - 5 - -21 + 15 = 48
17 - 5 - -21 - 15 = 18
We call the sequence of integers divisible by K if + or - operators can be placed between integers in the sequence in such way that resulting value is divisible by K. In the above example, the sequence is divisible by 7 (17+5+-21-15=-14) but is not divisible by 5.
You are to write a program that will determine divisibility of sequence of integers.
Input
The second line contains a sequence of N integers separated by spaces. Each integer is not greater than 10000 by it's absolute value.
Output
Sample Input
4 7
17 5 -21 15
Sample Output
Divisible
题意:给你一列整数,在整数间加‘ + ’ 或 ‘ - ‘,使这个算式的值能被k整除。
用dp[ i ][ j ] 表示加上或减去第 i 个数后,所得值取模后的值能否为 j ,所以dp为bool型即可。
状态转移方程:dp[ i ][ abs( j + num[i]) % k] = true;
dp[ i ][ abs( j - num[i]) % k] = true; (当然,必须满足dp[ i - 1 ][ j ] == true, 才能进行状态转移)
边界条件:dp[ 0 ][ 0 ] = true;
#include"iostream"
#include"cstdio"
#include"cstring"
#include"algorithm"
#include"map"
#include"set"
#include"stack"
#include"queue"
using namespace std;
const int ms=;
const int mn=;
bool dp[ms][mn];
int a[ms];
int N,K;
void solve()
{
memset(dp,false,sizeof(dp));
dp[][]=true;
for(int i=;i<=N;i++)
for(int j=;j<K;j++)
if(dp[i-][j])
{
dp[i][abs(j+a[i])%K]=true; //涉及一点数论
dp[i][abs(j-a[i])%K]=true;
}
if(dp[N][])
cout<<"Divisible"<<endl;
else
cout<<"Not divisible"<<endl;
return ;
}
int main()
{
cin>>N>>K;
for(int i=;i<=N;i++)
cin>>a[i];
solve();
return ;
}
Divisibility的更多相关文章
- cf306 C. Divisibility by Eight(数学推导)
C. Divisibility by Eight time limit per test 2 seconds memory limit per test 256 megabytes input sta ...
- 周赛-Clique in the Divisibility Graph 分类: 比赛 2015-08-02 09:02 23人阅读 评论(3) 收藏
Clique in the Divisibility Graph time limit per test1 second memory limit per test256 megabytes inpu ...
- Codeforces Round #306 (Div. 2) C. Divisibility by Eight 暴力
C. Divisibility by Eight Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/ ...
- Divisibility by Eight (数学)
Divisibility by Eight time limit per test 2 seconds memory limit per test 256 megabytes input standa ...
- codeforces 630J Divisibility
J. Divisibility time limit per test 0.5 seconds memory limit per test 64 megabytes input standard in ...
- Codeforces Testing Round #12 A. Divisibility 水题
A. Divisibility Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/597/probl ...
- HDU 3335 Divisibility (DLX)
Divisibility Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit ...
- light oj 1078 - Integer Divisibility
1078 - Integer Divisibility PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 3 ...
- POJ 1745 Divisibility (线性dp)
Divisibility Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10598 Accepted: 3787 Des ...
随机推荐
- PHP获取Cookie模拟登录
关键字:CURL Cookie CURLOPT_COOKIEJAR CURLOPT_COOKIEFILE 模拟登录 PHP作者:方倍工作室原文:http://www.cnblogs.com/txw19 ...
- hdfs 数据块重分布 sbin/start-balancer.sh -threshold
数据块重分布sbin/start-balancer.sh -threshold <percentage of disk capacity>percentage of disk capa ...
- XML学习笔记(2)--dom4j操作XML
1. 介绍(四种方式的比较这部分转载自:http://www.blogjava.net/xcp/archive/2010/02/12/312617.html) 1)DOM(JAXP Crimson解析 ...
- UVALive 7324 ASCII Addition (模拟)
ASCII Addition 题目链接: http://acm.hust.edu.cn/vjudge/contest/127407#problem/A Description Nowadays, th ...
- C:移位运算符
1在向右移位时,空出的位是由0填充,还是由符号位的副本填充? 如果被移位的对象是无符号数,那么空出的位将被0填充.如果被位移的对象是有符号数,那么C语言实现既可以用0填充空出的位,也可以用符号位的副本 ...
- 使用MySQL正则表达式查询
MySQL用WHERE子句对正则表达式提供了初步的支持,允许你指定用正则表达式过滤SELECT检索出的数据. REGEXP后所跟的东西作为正则表达式处理. 代码 SELECT prod_name FR ...
- 转载JQuery 中empty, remove 和 detach的区别
转载 http://www.cnblogs.com/lisongy/p/4109420.html .empty() 描述: 从DOM中移除集合中匹配元素的所有子节点. 这个方法不接受任何参数. 这个 ...
- Scala List的排序函数sortWith
//原始方法: //val list=List("abc","bcd","cde") scala> list.sortWith( (s ...
- 在数据库各种状态下查询DBID的五大类十种方法汇总
关于DBID: DBID是DataBase IDentifier的缩写,意思就是数据库的唯一标识符. 这个DBID在数据文件头和控制文件都是存在的,可以用于标示数据文件的归属. 对于不同数据库来说,D ...
- OpenNebula Restfull 接口请求示例
Fri Jun 20 07:28:20 2014 [I]: 10.0.2.2 - - [20/Jun/2014 07:28:20] "POST /vmtemplate HTTP/1.1&qu ...