POJ1745Divisibility 문제 해결 동적 계획 DP

Divisibility
Time Limit: 1000MS
 
Memory Limit: 10000K
Total Submissions: 6514
 
Accepted: 2161
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.
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.
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 7
17 5 -21 15

Sample Output
Divisible

Source
Northeastern Europe 1999
상태:
d[i][j]는 이전 i개수modk=j가 도달할 수 있는지 여부를 나타낸다
상태 이동 방정식:
d[i][j]=1 if(d[i-1]][(((j-a[i])%m+m)%m]|d[i-1][((j+a[i])%m+m)%m]) 그래서:
d[i][j]=d[i-1][((j-a[i])%m+m)%m]+d[i-1][((j+a[i])%m+m)%m]
경계:
d[1][(a[1]%m+m)%m]=1
 
코드:
#include bool d[2][105]; int main() { int n,m,i,j,k,a; scanf("%d%d%d",&n,&m,&a); d[0][(a%m+m)%m]=1; for(k=1,i=2;i<=n;i++,k^=1) { scanf("%d",&a); for(j=0;j

좋은 웹페이지 즐겨찾기