poj 1745 Divisibility

简介: 点击打开链接poj 1745 思路: dp 分析: 1 又是一道看了题解还不懂怎么个回事的题,然后各种YY之后有点感觉 2 题目要求的是在n个数中间插入n-1个的+或-使得结果能否被k整除 3 看一个数学的公式(a+b)%k = a%k...

点击打开链接poj 1745

思路: dp

分析:
1 又是一道看了题解还不懂怎么个回事的题,然后各种YY之后有点感觉
2 题目要求的是在n个数中间插入n-1个的+或-使得结果能否被k整除
3 看一个数学的公式(a+b)%k = a%k+b%k,按照网上的题解dp[i][j]表示的是前i个数运算能否得到模为j,如果可以则dp[i][j] = true,否则为false
4 那么如果dp[i-1][j] = true,则可以知道dp[i][(j+arr[i])%k]] = true , dp[i][(j-arr[i])%k]] = true。怎么理解这个状态转移方程呢,因为对于arr[i]这个数我们可以加也可以减,所以说有两种的情况
5 由于题目要求的是模,所以我们只要把所有的负数全部搞成正数即可。

代码:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;

const int MAX = 110;
const int MAXN = 10010;

int n , k;
bool dp[MAXN][MAX];

int main(){
    int num;
    while(scanf("%d%d" , &n , &k) != EOF){
         memset(dp , false , sizeof(dp));
         scanf("%d" , &num);
         dp[0][abs(num)%k] = true;
         for(int i = 1 ; i < n ; i++){
             scanf("%d" , &num);
             num = abs(num)%k;
             for(int j = 0 ; j < k ; j++){
                 if(dp[(i-1)%2][j]){
                    dp[i%2][(j+num)%k] = true;
                    dp[i%2][(j-num+k)%k] = true; 
                 } 
             } 
         }
         if(dp[(n-1)%2][0])
             puts("Divisible");
         else
             puts("Not divisible");
    }
    return 0;
}




目录
打赏
0
0
0
0
15
分享
相关文章
|
10月前
poj-1611-The Suspects
poj-1611-The Suspects
41 0
poj 3298 数状数组
题目大意是一条大街上住着n个乒乓球爱好者,他们的水平高低用一个数值表示,他们经常举办比赛,比赛要三个人,一人当裁判。对裁判是有一定要求的,裁判的水平必须介于两选手之间且必须住他们中间,计算可以举办多少场比赛
50 0
POJ 1012 Joseph
Joseph Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 53862   Accepted: 20551 Description The Joseph's problem is notoriously known.
851 0
poj 3664
http://poj.org/problem?id=3664 进行两轮选举,第一轮选前n进入第二轮,第二轮选最高   #include #include using namespace std; struct vote { int a,b; int c; ...
746 0
POJ 2027 No Brainer
Problem Description Zombies love to eat brains. Yum. Input The first line contains a single integer n indicating the number of data sets.
876 0
最小割-poj-2914
poj-2914-Minimum Cut Description Given an undirected graph, in which two vertices can be connected by multiple edges, what is the size of the minimum cut of the graph? i.e. how many edges must b
1576 0