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 ...
随机推荐
- c++ 概念及学习/c++ concept&learning(二)
上篇内容讲述了整个语言的发展[为什么会产生编程语言],以及学习C++所需要掌握的内容.这节开始认识第一部分最基本的内容:C++的内建类型,也就是基本类型. 在这些知识之前留一个问题:为什么基本所有语言 ...
- 机器学习中的数学(5)-强大的矩阵奇异值分解(SVD)及其应用
版权声明: 本文由LeftNotEasy发布于http://leftnoteasy.cnblogs.com, 本文可以被全部的转载或者部分使用,但请注明出处,如果有问题,请联系wheeleast@gm ...
- 编译器对C++ 11变参模板(Variadic Template)的函数包扩展实现的差异
编译器对C++ 11变参模板(Variadic Template)的函数包扩展实现的差异 题目挺绕口的.C++ 11的好东西不算太多,但变参模板(Variadic Template)肯定是其中耀眼的一 ...
- WinJS.Binding.List与kendo.data.ObservableArray
avalon0.8一个最大目标是实现对数组的深层监控,可是面临的困难重重,至今还没有什么起色.于是看一下其他两个MVVM框架的做法(knockout, emberjs, angular都不能监听家庭数 ...
- nyoj 10 skiing(记忆化搜索)
skiing 时间限制:3000 ms | 内存限制:65535 KB 难度:5 描述 Michael喜欢滑雪百这并不奇怪, 因为滑雪的确很刺激.可是为了获得速度,滑的区域必须向下倾斜,而且当 ...
- Spring Data JPA教程, 第五部分: Querydsl(未翻译)
The fourth part of my Spring Data JPA tutorialdescribed how you can implement more advanced queries ...
- jeecms附件标签用法
[#if content.attachments?size gt 0] [#list content.attachments as attach] <a id="attach${att ...
- Android_通过传感器抓小偷
package com.beyond.phonestolen; import android.hardware.Sensor; import android.hardware.SensorEvent; ...
- 04.URL路径访问与模块控制器之间的关系
<?php //初使化,进行加载. //通过这个英文名来了解,他是定义的与thinkphp有关的核心框架文件目录路径 //他可以通过这一个常量,在以后运行的时候都去找这个路径,确保在运行过程当, ...
- [MODx] 3. Working with chunks, TV, Category
1. Add chunk: For example, replace the header by using chunk. Usage: [[$chunk_name]] Cut all the hea ...