切换到宽版
  • 12051阅读
  • 13回复

问下ZJU2042 [复制链接]

上一主题 下一主题
离线勇气les
 
只看楼主 正序阅读 0 发表于: 2006-07-28
Divisibility

--------------------------------------------------------------------------------

Time limit: 1 Seconds   Memory limit: 32768K  
Total Submit: 832   Accepted Submit: 223  

--------------------------------------------------------------------------------

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
离线勇气les
只看该作者 13 发表于: 2006-07-31
谢谢————
离线r134a
只看该作者 12 发表于: 2006-07-30
一个字,强!!!~~~~我对大牛的敬仰犹如滔滔江水连绵不绝~~~~
.


祝大家明年NOIP大获全盛!


.
离线stevenjl

只看该作者 11 发表于: 2006-07-30
别的大牛写的~
  1. const maxk=100;
  2. var old,newone:array[-maxk..maxk] of boolean;
  3.   kase,num,n,k,i,j,temp:integer;
  4. begin
  5. readln(kase);readln;
  6. for num:=1 to kase do begin
  7.   if num>=2 then begin writeln;readln;end;
  8.   readln(n,k);
  9.   fillchar(old,sizeof(old),0);old[0]:=true;
  10.   for i:=1 to n do begin
  11.     fillchar(newone,sizeof(newone),0);
  12.     read(temp);
  13.     for j:=-k to k do
  14.     if old[j] then begin
  15.       newone[(j+temp)mod k]:=true;
  16.       newone[(j-temp)mod k]:=true;
  17.     end;
  18.     old:=newone;
  19.   end;
  20.   if newone[0] then writeln(\'Divisible\')
  21.           else writeln(\'Not divisible\');
  22. end;
  23. end.
Dream Walker...
离线eagleoi
只看该作者 10 发表于: 2006-07-30
说了枚举不行吗...
离线stevenjl

只看该作者 9 发表于: 2006-07-30
枚举的确超时,下面是我悲哀的超时程序
  1. program usacozju2042;
  2. var sj: array[1..10000] of integer;
  3.   opr: array[1..9999] of integer;
  4.   m,n,k:integer;
  5. procedure duru;
  6. var i,j:integer;
  7. ch:char;
  8. function opradd :boolean;
  9. var i:integer;
  10. begin
  11. inc(opr[n-1]);
  12. if opr[n-1] = 2 then
  13. begin
  14. for i:= n-1 downto 2 do
  15. begin
  16. if opr[i]>1 then begin opr[i]:=0; inc(opr[i-1]); if opr[i-1] <> 2 then break; end;
  17. end;
  18. end;
  19. if opr[1]=2 then exit(false);
  20. exit(true);
  21. end;
  22. function yz : boolean;
  23. var i:integer;
  24. sum:longint;
  25. begin
  26. sum:=sj[1];
  27. for i:= 1 to n-1 do
  28. begin
  29. if opr[i]=0 then sum:=sum+sj[i+1]
  30. else sum:=sum-sj[i+1];
  31. end;
  32. if sum mod k = 0 then exit(true);
  33. exit(false);
  34. end;
  35. procedure main;
  36. var i,total: integer;
  37. begin
  38. total:=0;
  39. for i:= 1 to n-2 do opr[i]:=0;
  40. opr[n-1]:=-1;
  41. while opradd do
  42. if yz then begin writeln('Divisible'); exit; end;
  43. writeln('Not divisible');
  44. end;
  45. begin
  46. read(m);
  47. read(ch);
  48. if ch<>' ' then
  49. begin
  50. for j:= 1 to m do
  51. begin
  52. read(n,k);
  53. for i:= 1 to n do read(sj[i]);
  54. for i:= n+1 to 10000 do sj[i]:=0;
  55. main();
  56. if j<>m then writeln;
  57. end;
  58. end
  59. else
  60. begin
  61. n:=m;
  62. read(k);
  63. for i:= 1 to n do read(sj[i]);
  64. main();
  65. end;
  66. end;
  67. begin
  68. duru();
  69. end.
Dream Walker...
离线eagleoi
只看该作者 8 发表于: 2006-07-30
应该是数学方法把,你看,不剪枝的搜索时间复杂度最高2^9999,1天都算不完
离线r134a
只看该作者 7 发表于: 2006-07-30
难道有数学方法???
.


祝大家明年NOIP大获全盛!


.
离线eagleoi
只看该作者 6 发表于: 2006-07-30
枚举?1 <= N <= 10000!!!!!!,复杂度最高2^9999!枚举可能吗?
离线r134a
只看该作者 5 发表于: 2006-07-30
搜索?
.


祝大家明年NOIP大获全盛!


.
快速回复
限100 字节
 
上一个 下一个