首页 > 代码库 > 2014-2015 CT S02E01: Codeforces Trainings Season 2 Episode 1 题解

2014-2015 CT S02E01: Codeforces Trainings Season 2 Episode 1 题解

A:给你n个数(正负都有),让你添加 n-1个符号,使得它能够被 k整除

解题思路:显然我们把所有树MOD k 然后  -a[i] or  + a[i]  背包处理就行了

解题代码:

 1 // File Name: a.cpp 2 // Author: darkdream 3 // Created Time: 2014年09月11日 星期四 20时09分52秒 4  5 #include<vector> 6 #include<list> 7 #include<map> 8 #include<set> 9 #include<deque>10 #include<stack>11 #include<bitset>12 #include<algorithm>13 #include<functional>14 #include<numeric>15 #include<utility>16 #include<sstream>17 #include<iostream>18 #include<iomanip>19 #include<cstdio>20 #include<cmath>21 #include<cstdlib>22 #include<cstring>23 #include<ctime>24 #define LL long long25 26 using namespace std;27 int a[10005];28 int dp[10005][102];29 int main(){30   int n, m; 31   scanf("%d %d",&n,&m);32   for(int i = 1;i <= n;i ++)33   {34      scanf("%d",&a[i]);35      a[i] = a[i] % m; 36   }37   memset(dp,0,sizeof(dp));38   dp[1][abs(a[1])] = 1;39   for(int i = 2;i <= n;i ++)40   {41       for(int j = 0 ;j < m;j ++)42       {43         if(dp[i-1][j])44         {45               dp[i][abs(j-a[i])%m] = 1; 46               dp[i][abs(j+a[i])%m] = 1; 47         }48       }49   }50   if(dp[n][0])51       printf("Divisible\n");52   else printf("Not divisible\n");53   54 return 0;55 }
View Code

 

2014-2015 CT S02E01: Codeforces Trainings Season 2 Episode 1 题解