您的位置:首页 > 运维架构

openjudge divisibility

2015-12-02 19:07 417 查看
747:Divisibility
描述
Consider an arbitrary sequence of integers. One can place + or - operatorsbetween integers in the sequence, thus deriving different arithmeticalexpressions that evaluate to different values. Let us, for example, take
thesequence: 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 beplaced between integers in the sequence in such way that resulting value isdivisible 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 ofintegers. 
输入
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. Eachinteger is not greater than 10000 by it's absolute value. 
输出
Write to the output file the word "Divisible" if given sequenceof integers is divisible by K or "Not divisible" if it's not.
样例输入

4 7
17 5 -21 15

样例输出

Divisible

来源
Northeastern Europe 1999
题目大意:在一串数中插于加号或减号,使最后的结果是k的倍数,如果可以输出Divisible,否则输出Not
divisible。
#include<iostream>

#include<cstdio>

#include<cstring>

using namespace std;

int n,m,i,j,k;

int num[100100],now[210],last[210];//last数组存储的是上一个数计算完后的状态,然后在此基础上计算即可

int main()

{

scanf("%d%d",&n,&k);

for (i=1;i<=n;i++)

  scanf("%d",&num[i]);

now[100]=1;

for (i=1;i<=n;i++)

  {

    num[i]%=k;

    for (j=100-k;j<=100+k;j++)

      {

          last[j]=now[j];

          now[j]=0;

      }

    for (j=100-k;j<=100+k;j++)//动态规划部分

      if (last[j]==1)

       {

          now[(j-100-num[i])%k+100]=1;

          now[(j-100+num[i])%k+100]=1; 

       }

  }

if (now[100]==1)

 cout<<"Divisible";

else

  cout<<"Notdivisible";

return 0;

}
//标准的动态规划,因为考虑到有可能会出现负数,所以把余数统一加上100,保证它恒为正,但是注意转移时要减去100,用原来的数据进行计算得出答案
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: