1745:Divisibility
来源:互联网 发布:软件项目监理方案 编辑:程序博客网 时间:2024/05/19 06:15
1745:Divisibility
总时间限制: 1000ms 内存限制: 65536kB
描述
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.
输入
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.
输出
Write to the output file the word “Divisible” if given sequence of integers is divisible by K or “Not divisible” if it’s not.
样例输入
4 7
17 5 -21 15
样例输出
Divisible
我自己做的时候因为n范围太大,超时了,我是直接枚举了,下面是错误代码:
#include<iostream>using namespace std;int n,k,a[11000],f[11000],res;bool g(){ res=a[0]; for(int i=1;i<n;i++){ if(f[i]==0){ res+=a[i]; } if(f[i]==1){ res-=a[i]; } } //cout<<res<<endl; if(res%k==0)return true; else return false;}int main(){ cin>>n>>k; for(int i=0;i<n;i++){ cin>>a[i]; } while(f[n]!=1&&!g()){ f[1]++; int c=1; while(f[c]>1){ f[c]=0; c++; f[c]++; } } if(f[n]==1){ cout<<"Not divisible"<<endl; } else cout<<"Divisible"<<endl;}
这个是参考的:状态:dp[i][j] 表示前 i 个数合成模 K 为 j 是否可行
状态转移方程:
dp[i + 1][((j + a[i + 1]) % K + K) % K] = 1;
dp[i + 1][((j - a[i + 1]) % K + K) % K] = 1;
#include <cstdio>#include <cstring>const int MAXN = 10000 + 10;const int MAXK = 100 + 10;int N, K;int a[MAXN];char dp[MAXN][MAXK];void Read(){ for (int i = 1; i <= N; ++i) { scanf("%d", a + i); }}void Dp(){ memset(dp, 0, sizeof(dp)); dp[1][(a[1] % K + K) % K] = 1; for (int i = 1; i < N; ++i) { for (int j = 0; j < K; ++j) { if (dp[i][j] != 0) { dp[i + 1][((j + a[i + 1]) % K + K) % K] = 1; dp[i + 1][((j - a[i + 1]) % K + K) % K] = 1; } } } dp[N][0] == 0 ? puts("Not divisible") : puts("Divisible");}int main(){ while (scanf("%d%d", &N, &K) == 2) { Read(); Dp(); } return 0;}
- 1745:Divisibility
- 1745 Divisibility DP
- poj 1745 Divisibility
- POJ 1745 Divisibility
- POJ 1745 Divisibility
- POJ 1745 Divisibility
- POJ 1745 Divisibility
- poj 1745 Divisibility
- poj 1745 Divisibility
- poj 1745 Divisibility dp
- POJ 1745 Divisibility DP
- poj 1745 Divisibility
- poj 1745-Divisibility
- POJ 1745 Divisibility
- poj 1745 Divisibility (dp)
- poj 1745 Divisibility dp
- 【DP】POJ-1745 Divisibility
- poj 1745 Divisibility 【DP】
- Swift 函数与闭包
- jQuery ajax普通情况 提交表单情况
- 基于Swift的iOS应用程序开发:使用UIImagePickerController调取摄像头进行拍照
- Java学习笔记—集合
- ajax 删除用户 异步刷新
- 1745:Divisibility
- PHP mysql 优化
- #CCF准备一年日常刷题#201412-1 门禁系统
- SpringBoot对消息队列(MQ)的支持
- dev_dbg 的粗暴使用
- Spring-bean的循环依赖以及解决方式
- 流量汇总mapreduce
- Spring-day01
- git pull error: remote ref is at but expected