POJ1745Divisibility(dp)(AC)
来源:互联网 发布:中文相似度 python 库 编辑:程序博客网 时间:2024/05/10 22:27
Divisibility
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 11107 Accepted: 3975
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
#define _CRT_SECURE_NO_WARNINGS#include <stdio.h>//.http://blog.csdn.net/wangjian8006/article/details/10170671这个讲的很详细/*第一次提交WA dps[][]没有初始化,初始化了第二次提交还是不行*//*第二次提交WA 自己做减号的时候不对,看别人是j-map[i]+K*//*POJAC*/#define MAXINT 10005#define MAXK 105int N = 0;int K = 0;int map[MAXINT];//要用动态规划的思想去做int dps[MAXINT][MAXK]; //dp[i][j] 前i个数被j整除的余数void init(){int i = 0;int j = 0;for (i = 0; i < MAXINT; i++){map[i] = 0;for (j = 0; j < MAXK;j++){dps[i][j] = -1;}}return;}//bfs有个问题是堆栈开多大,如果按最大数,那就是2的N-1次方,这个实在是太大了int bfs(){return 0;}void dp(){int i = 0;int j = 0;int minus = 0;dps[1][map[0] % K] = 1; //说明前1个数的余数是map[0],可以直接用map[0]是因为在输入的时候就做了处理for (i = 2; i <= N; i++){for (j = 0; j < K; j++){if (1 == dps[i-1][j]){//加号dps[i][(j+map[i-1])%K] = 1;//减号//minus = (j - map[i]) % K;//if (minus < 0)//{//minus = -minus;//}dps[i][(j - map[i-1]+K) % K] = 1;}}}return;}//用bfs是不是会好点,不会超时,如果用dfs应该会超时吧 最多可能会有10000个数字这个肯定超时了int main(){int i = 0;freopen("input.txt","r",stdin);scanf("%d %d",&N,&K);init();for (i = 0; i < N;i++){scanf("%d",&map[i]);//刚开始对负数做处理,因为有+或-其实如果是负数变成正数都是一样的#if 1if (map[i] < 0){map[i] = -map[i];}map[i] = map[i] % K;#endif}//百度上说这个用dp来做的dp();if (1 == dps[N][0]) //没有余数{printf("Divisible\n");}else{printf("Not divisible\n");}//int a = (-2) % 6;//printf("%d\n",a);return 0;}
0 0
- POJ1745Divisibility(dp)(AC)
- POJ1745Divisibility【dp】
- POJ1745Divisibility(dp)
- POJ1745Divisibility题解动态规划DP
- poj1745Divisibility
- POJ1745Divisibility
- hdu4758 AC自动机+dp
- AC自动机 + DP小结
- AC自动机+DP+hdu2296
- hdu2457 AC自动机+DP
- hdu2296(AC自动机+DP)
- hdu2296 AC自动机+DP
- uvalive4842(AC自动机+DP)
- poj3691(ac自动机+dp)
- hdu3689(ac自动机+dp)
- poj3691(ac自动机+dp)
- AC自动机+dp(CodeForces
- poj 2778 AC自动机DP
- CAKeyframeAnimation的简单使用
- VAPI_SetEthMac
- Android View的second layout pass
- iOS 9之适配ATS
- Android 颜色渲染(九) PorterDuff及Xfermode详解
- POJ1745Divisibility(dp)(AC)
- 硬盘组成介绍
- "_ABAddressBookCreate", referenced from: -[IFlyContact contact] in iflyMSC(IFlyContact.o)
- android自定义权限
- Android中的权限机制
- 用EXCEL打开CSV格式后,如何避免11位以上数字用科学计数法显示
- pythos 文件目录操作
- 2015-11-25 cocoapods 0.39.0 specs 下面的 13746 个项目的清单
- 整理 iOS 9 适配中出现的坑(图文)