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#做的 排序算法是想要成为大虾程序员必须要掌握的技术,它其实也是一种思想,你对算法熟悉,对以后编程有很大帮助 算法思路 ⒈ 从第一个元素 ...
- Probabilistic SVM 与 Kernel Logistic Regression(KLR)
本篇讲的是SVM与logistic regression的关系. (一) SVM算法概论 首先我们从头梳理一下SVM(一般情况下,SVM指的是soft-margin SVM)这个算法. 这个算法要实现 ...
- 线性模型(2):Linear Regression
此笔记源于台湾大学林轩田老师<机器学习基石><机器学习技法> 我们已经学习过PLA算法,所谓的线性模型就是:计算核心为.PLA是一种分类方法,这里介绍线性回归方法(与概率与统计 ...
- SVM核函数与软间隔
核函数 在上文中我们已经了解到使用SVM处理线性可分的数据,而对于非线性数据需要引入核函数的概念它通过将数据映射到高维空间来实现线性可分.在线性不可分的情况下,支持向量机通过某种事先选择的非线性映射( ...
- CentOS上firefox安装flash
CentOS下firefox安装flash说明 CentOS下自带了firefox,但没有flash插件的,按它自己的提示安装不成功,需要手动安装,如下: 1.打开flash官网,http://lab ...
- HDU 5675 ztr loves math (数学推导)
ztr loves math 题目链接: http://acm.hust.edu.cn/vjudge/contest/123316#problem/A Description ztr loves re ...
- JavaIO(03)字节流--OutputStream and InputStream
IO概述: IO流用来处理设备之间的数据传输 java对数据的操作是通过流的方式 java用于操作流的对象都在IO包中 流按操作数据分为两种:字节流与字符流(编码表) 流按流向分为:输入流,输出流 ...
- HDU1973 http://acm.hdu.edu.cn/showproblem.php?pid=1973
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<queue> #inc ...
- 解决Failed to execute goal org.apache.maven.plugins
1.Maven构建失败 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin: 2.3 . 2 :compile ...
- iOS异常捕获
文章目录 一. 系统Crash 二. 处理signal 下面是一些信号说明 关键点注意 三. 实战 四. Crash Callstack分析 – 进⼀一步分析 五. demo地址 六. 参考文献 前言 ...