Divisibility


Time Limit: 2 Seconds      Memory Limit:65536 KB

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.

Input

The first line of the input 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.

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.

This problem contains multiple test cases!

The first line of a multiple input is an integer N, then a blank line followed by N input blocks. Each input block is in the format indicated in the problem description. There is a blank line between input blocks.

The output format consists of N output blocks. There is a blank line between output blocks.


Sample Input

2

4 7
17 5 -21 15

4 5
17 5 -21 15

Sample Output

Divisible

Not divisible

【问题分析】http://blog.csdn.net/tsaid/article/details/7840487(可怜原作者不晓得输出的恶心换行套路,找错绝对很头疼)

看到题目第一个反映就是枚举中间添的运算符,算出值在MOD K如果有一个值MOD K=0则输出“Divisible”。

时间复杂度是O(2N-1)。

但是题目给出的数据量很大,这样做效率太低了。

因为题目涉及MOD运算,要想简化问题就需要知道一些基本的MOD运算性质:

A*B mod C=(A mod C*B mod C) mod C

(A+B) mod C=(A mod C+B mod C) mod C

有了这个性质,我们就可以把累加后求余转化成求余后累加(我们把减法看作加负数以后分析只说加法)再求余。这样我们的读入数据就控制在了1-K到K-1的范围内了。

我们要判断的就是

所有结果的累加和 MOD K 是否为0。简记为:

(A+B)mod K=0 or (A+B) mod K<>0

如果我们按数的个数划分阶段,前N-1个数的运算结果 MOD K看做A,第N个数看作B就OK了。

于是我们想到了这样的状态:opt[i,j]表示前i个数是否可以得到余数为J的结果。

那么状态转移方程就是

opt[i,(j-a[i] mod k )modk]=opt[i-1,j]           (opt[i-1,j]=true);

opt[i,(j+a[i] mod k) modk]=opt[i-1,j]          (opt[i-1,j]=true);

如果opt[n,0]=true就输出‘Divisible’

我一开始想用滚动数组来解决(尽管没有这个必要)。

a[1][(x%k+k)%k]=true;
for(int i=2;i<=n;i++)
{
cin>>x;//=_S();
for(int j=0;j<k;j++)
{
if(a[(i-1)%2][j])
{
a[i%2][((j+x)%k+k)%k]=true;
a[i%2][((j-x)%k+k)%k]=true;
}
}
}
if(a[n%2][0]==true) printf("Divisible\n");
else printf("Not divisible\n");
if(T) printf("\n");

然后一直wa,后来发现滚动数组必须每次刷新,很尴尬,反而使得效率低下。如下:

 memset(a[1],false,sizeof(a[1]));
x=_S();
a[1][(x%k+k)%k]=true;
for(int i=2;i<=n;i++)
{
memset(a[i%2],false,sizeof(a[i%2]));
x=_S();
for(int j=0;j<k;j++)
{
if(a[(i-1)%2][j])
{
a[i%2][((j+x)%k+k)%k]=true;
a[i%2][((j-x)%k+k)%k]=true;
}
}
}
if(a[n%2][0]) printf("Divisible\n");
else printf("Not divisible\n");
if(T) printf("\n");

就是memset这个可恶的东西,然后改了一下:

                a[1][(x%k+k)%k]=1;
for(int i=2;i<=n;i++)
{
x=_S();
for(int j=0;j<k;j++)
{
if(a[(i-1)%2][j]==i-1)//用唯一的数字标记,不需要更新
{
a[i%2][((j+x)%k+k)%k]=i;
a[i%2][((j-x)%k+k)%k]=i;
}
}
}
if(a[n%2][0]==n) printf("Divisible\n");
else printf("Not divisible\n");
if(T) printf("\n");

但是搞来搞去,好像效率还是不怎么样,不过数字代替memset的思路是挺好的。

建议赛场上还是开a[10001][101]的数组,免得乱搞麻烦。

完整代码:100ms,还算可以了

#include<iostream>
#include<cstdio>
#include<cstring>
#include<memory.h>
#include<cmath>
using namespace std;
int a[2][101];
int _S()
{
char c=getchar();
while(c<'0'||c>'9')
c=getchar();
int s=0;
while(c>='0'&&c<='9'){
s=s*10+c-'0';
c=getchar();
}
return s;
}
int main()
{
int T,k,x,n;
T=_S();
while(T--)
{ n=_S();
k=_S();
memset(a,0,sizeof(a));
x=_S();
a[1][x%k]=1;
for(int i=2;i<=n;i++)
{
x=_S();
for(int j=0;j<k;j++)
{
if(a[(i-1)%2][j]==i-1)
{
a[i%2][(j+x)%k]=i;
a[i%2][((j-x)%k+k)%k]=i;
}
}
}
if(a[n%2][0]==n) printf("Divisible\n");
else printf("Not divisible\n");
if(T) printf("\n");
}
return 0;
}

ZOJ 2042 Divisibility (DP)的更多相关文章

  1. ZOJ - 2042 模运算DP

    解法见网上参考 这种只判断可达性的DP一般用bool 除非int能得到更多的信息 #include<iostream> #include<algorithm> #include ...

  2. ZOJ 3626(树形DP+背包+边cost)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3626 题目大意:树中取点.每过一条边有一定cost,且最后要回 ...

  3. zoj 3822 Domination(dp)

    题目链接:zoj 3822 Domination 题目大意:给定一个N∗M的棋盘,每次任选一个位置放置一枚棋子,直到每行每列上都至少有一枚棋子,问放置棋子个数的期望. 解题思路:大白书上概率那一张有一 ...

  4. ZOJ 3201 树形dp+背包(简单题)

    #include<cstdio> #include<vector> #include<cstring> #include<iostream> using ...

  5. ZOJ 3805 (树形DP)

    题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5337 题目大意:方块连接,呈树形.每个方块有两种接法,一种接在父块 ...

  6. ZOJ 3822 可能性DP

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3822 本场比赛之前,我记得.见WALK概率路DP称号.那么它应该是可以考虑 ...

  7. zoj 3640 概率dp

    题意:一只吸血鬼,有n条路给他走,每次他随机走一条路,每条路有个限制,如果当时这个吸血鬼的攻击力大于等于某个值,那么就会花费t天逃出去,否则,花费1天的时间,并且攻击力增加,问他逃出去的期望 用记忆化 ...

  8. ZOJ 3211dream city dp(效率优化)

    Dream City Time Limit: 1 Second      Memory Limit:32768 KB JAVAMAN is visiting Dream City and he see ...

  9. zoj 3537 区间dp+计算几何

    题意:给定n个点的坐标,先问这些点是否能组成一个凸包,如果是凸包,问用不相交的线来切这个凸包使得凸包只由三角形组成,根据costi, j = |xi + xj| * |yi + yj| % p算切线的 ...

随机推荐

  1. 原创:LNMP架构部署个人博客网站 禁止转载复制

    nginx编译安装步骤 ①. 检查软件安装的系统环境 cat /etc/redhat-release uname -r ②. 安装nginx的依赖包(pcre-devel openssl-devel) ...

  2. 自制VTP实验总结

    (packet tracer模拟器) 6.1)实验拓扑 //绿色:通:橙色:不通 //sw0是根桥:全通 Pc0:ip 192.168.1.1 Pc1: ip 192.168.1.2 Pc2:ip 1 ...

  3. 201521123084 《Java程序设计》第9周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结异常相关内容. 2. 书面作业 本次PTA作业题集异常 1. 常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自 ...

  4. 【Beta】 第二次Daily Scrum Meeting

    一.本次会议为第二次meeting会议 二.时间:13:30AM-13:55AM 地点:禹州 三.会议站立式照片 四.今日任务安排 成员 昨日任务 今日任务 林晓芳 对已完成的功能进行进一步测试,以便 ...

  5. 团队作业4——第一次项目冲刺(Alpha版本)1st day

    一.Daily Scrum Meeting照片 二.燃尽图 三.项目进展 1.界面 主界面以及游戏界面大体上完成了 界面内的功能正在写 2.登陆方面 QQ授权还未申请 申请完在登陆界面完成后实现用QQ ...

  6. 201521123005《java程序设计》第五周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 1.2 可选:使用常规方法总结其他上课内容. 接口 有点类似继承中父类与子类的关系 方法声明和常量值的集合 对行为的抽象 一种 ...

  7. 201521123062《Java程序设计》第5周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 1.2 可选:使用常规方法总结其他上课内容. 2. 书面作业 1.代码阅读:Child压缩包内源代码 1.1 com.pare ...

  8. 201521123055 《Java程序设计》第4周学习总结

    1. 本章学习总结 2. 书面 Q1.注释的应用使用类的注释与方法的注释为前面编写的类与方法进行注释,并在Eclipse中查看.(截图) Q2.面向对象设计 2.1 将在网上商城购物或者在班级博客进行 ...

  9. java课设-计算数学表达式的程序,201521123050,肖世松,个人

    1.团队课程设计博客链接 http://www.cnblogs.com/xss666/p/7063780.html 2.个人负责模块或任务说明 个人负责:计算器外观,左容器CalPanelL p1: ...

  10. 详解go语言的array和slice 【一】

    本篇会详细讲解go语言中的array和slice,和平时开发中使用他样时需要注意的地方,以免入坑. Go语言中array是一组定长的同类型数据集合,并且是连续分配内存空间的. 声明一个数组 var a ...