POJ 1745 Divisibility
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 9476 | Accepted: 3300 |
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
#include <stdio.h>
#include <iostream>
using namespace std; int dp[][] = {};
int num[] = {}; int main()
{
int k, n;
scanf("%d%d", &n, &k);
for (int i = ; i < n; i++)
{
scanf("%d", &num[i]);
if (num[i] < )
{
num[i] *= -;
}
num[i] = num[i] % k;
}
dp[][num[]] = ;
for (int i = ; i < n; i++)
{
for (int j = ; j <= k; j++)
{
if (dp[i - ][j])
{
dp[i][(j + num[i]) % k] = ;
dp[i][(k + j - num[i]) % k] = ;
}
}
}
if(dp[n-][])
{
printf("Divisible\n");
}
else
{
printf("Not divisible\n");
}
return ;
}
POJ 1745 Divisibility的更多相关文章
- POJ 1745 Divisibility (线性dp)
Divisibility Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 10598 Accepted: 3787 Des ...
- POJ 1745 Divisibility DP
POJ:http://poj.org/problem?id=1745 A完这题去买福鼎肉片,和舍友去买滴~舍友感慨"这一天可以卖好几百份,每份就算赚一块钱..那么一个月..一年...&quo ...
- POJ 1745 Divisibility【DP】
题意:给出n,k,n个数,在这n个数之间任意放置+,-号,称得到的等式的值能够整除k则为可划分的,否则为不可划分的. 自己想的是枚举,将所有得到的等式的和算出来,再判断它是否能够整除k,可是有1000 ...
- POJ 1745 【0/1 背包】
题目链接:http://poj.org/problem?id=1745 Divisibility Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- POJ 1745 线性和差取余判断
POJ 1745 线性和差取余判断 题目大意:每个数都必须取到,相加或相减去,问所有的方案最后的得数中有没有一个方案可以整除k 这个题目的难点在于dp数组的安排上面 其实也就是手动模仿了一下 比如 一 ...
- POJ 1745:Divisibility 枚举某一状态的DP
Divisibility Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11001 Accepted: 3933 Des ...
- [POJ 1745] Divisbility
[题目链接] http://poj.org/problem?id=1745 [算法] DP [代码] #include <algorithm> #include <bitset> ...
- 1745 Divisibility
Divisibility Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 14084 Accepted: 4989 Descrip ...
- POJ 1745
#include <iostream> #define MAXN 10005 using namespace std; int _m[MAXN]; ]; int main() { //fr ...
随机推荐
- Vue-router 的练习
使用了vue-cli 生成了一套webpack的模版. 之后在其中练习 vue-router. 以下是一些记录. 1.动态路由的配置 import Vue from 'vue' import Rout ...
- Android 图片在SD卡及包下的存储
public class FileBitmap { /** * 获取sd卡中的bitmap,bitmap可见 * * @param bitmap * 读取bitmap的路径 * @return bit ...
- SqlServer表和excel数据批量复制方法
SqlServer表和excel数据批量复制方法 一.SqlServer表数据复制到excel方法: 1.新建查询,用sql语句把表数据读出来 2.然后,选择数据,右键“复制”(如果需要表字段名称,则 ...
- 洛谷 P1168 中位数
题目描述 给出一个长度为N的非负整数序列A[i],对于所有1 ≤ k ≤ (N + 1) / 2,输出A[1], A[3], …, A[2k - 1]的中位数.[color=red]即[/color] ...
- hash 散列表
一个字符串的hash值: •现在我们希望找到一个hash函数,使得每一个字符串都能够映射到一个整数上 •比如hash[i]=(hash[i-1]*p+idx(s[i]))%mod •字符串:abc,b ...
- CPP-基础:非静态成员函数后面加const,以及mutable修饰成员变量
非静态成员函数后面加const(加到非成员函数或静态成员后面会产生编译错误),表示成员函数隐含传入的this指针为const指针,决定了在该成员函数中,任意修改它所在的类的成员的操作都是不允许的(因为 ...
- grep-sed命令用法:
用户切换 su username:非登录式切换 su - username:登录式切换 su -l username:登录式切换 su username -c COMMAND echo -n ...
- opencast的docker安装
在之前的从源安装和从包安装opencast,都遇到较多环境问题导致失败.所有采用docker安装. Dockers是有能力打包应用程序及其虚拟容器,可以在任何Linux服务器上运行的依赖性工具,这有助 ...
- docker系列之file基本操作
dockerfile基础操作 Dockerfile 是记录了镜像是如何被构建出来的配置文件, 可以被 docker 直接执行以创建一个镜像. 它的样子: FROM ubuntu:14.04 MAINT ...
- perl学习之内置变量
Perl内置特殊变量 一.正则表达式特殊变量:1.$n :包含上次模式匹配的第n个子串2.$& :前一次成功模式匹配的字符串3.$` :前次匹配成功的子串之前的内容4.$’ :前次匹配 ...