POJ 题目1745 Divisibility(DP)
来源:互联网 发布:mac如何导入ps字体 编辑:程序博客网 时间:2024/05/05 06:04
Divisibility
Description
Consider an arbitrary sequence of integers. One can place + or - operators between integers in the sequence, thus deriving different arithmetical expressions that evaluate to different values. Let us, for example, take the sequence: 17, 5, -21, 15. There are eight possible expressions: 17 + 5 + -21 + 15 = 16
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.
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 first line of the input file contains two integers, N and K (1 <= N <= 10000, 2 <= K <= 100) separated by a space.
The second line contains a sequence of N integers separated by spaces. Each integer is not greater than 10000 by it‘s absolute value.
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
Write to the output file the word "Divisible" if given sequence of integers is divisible by K or "Not divisible" if it‘s not.
Sample Input
4 717 5 -21 15
Sample Output
Divisible
#include<stdio.h>#include<string.h>int dp[10010][110],a[10010];int n,k;int fun(int num){int temp=num%k;if(temp<0)temp+=k;return temp;}int main(){while(scanf("%d%d",&n,&k)!=EOF){int i,j;for(i=1;i<=n;i++)scanf("%d",&a[i]);memset(dp,0,sizeof(dp));dp[1][fun(a[1])]=1;for(i=2;i<=n;i++){for(j=0;j<k;j++){if(dp[i-1][j]){dp[i][fun(j+a[i])]=1;dp[i][fun(j-a[i])]=1;}}}if(dp[n][0]){printf("Divisible\n");}elseprintf("Not divisible\n");}}
0 0
- POJ 题目1745 Divisibility(DP)
- POJ 题目1745 Divisibility(DP,数学)
- poj 1745 Divisibility dp
- POJ 1745 Divisibility DP
- poj 1745 Divisibility (dp)
- poj 1745 Divisibility dp
- 【DP】POJ-1745 Divisibility
- poj 1745 Divisibility 【DP】
- poj 1745 Divisibility dp
- poj 1745 Divisibility(DP)
- poj - 1745 - Divisibility(dp)
- POJ 1745 Divisibility (线性dp)
- POJ 1745 Divisibility (DP)
- POJ Problem 1745 Divisibility 【dp】
- POJ 1745 / UVa 10036 Divisibility (同余定理,DP,经典题目)
- poj 1745 Divisibility(DP + 数学)
- 1745 Divisibility DP
- POJ 1745 Divisibility 类似0-1背包DP
- 数据结构实验之二叉树五:层序遍历
- 【《代码整洁之道》精读与演绎】之二 高质量代码的命名法则
- HDU 1695 容斥
- 子集生成之增量构造法(允许有重复元素)
- IMTalkSDK API文档-Android
- POJ 题目1745 Divisibility(DP)
- JAVA基础再回首(九)——Object类、String类及其获取功能、转换功能、其他功能
- hdu 5794 A Simple Chess (dp+lucas定理)
- LeetCode 72 Edit Distance
- HDU 1850 Being a Good Boy in Spring Festival(Nim博弈)
- Oracle 11g R2 DBA 操作指南(10) 控制文件和数据库启动
- MySQL Where浅析
- Dx unsupported class file version 52.0 …while parsing
- 三大框架整合出现错误processing request: null java.lang.reflect.InvocationTargetException