openjudge divisibility
2015-12-02 19:07
417 查看
747:Divisibility
描述
Consider an arbitrary sequence of integers. One can place + or - operatorsbetween integers in the sequence, thus deriving different arithmeticalexpressions that evaluate to different values. Let us, for example, take
thesequence: 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 beplaced between integers in the sequence in such way that resulting value isdivisible 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 ofintegers.
输入
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. Eachinteger is not greater than 10000 by it's absolute value.
输出
Write to the output file the word "Divisible" if given sequenceof integers is divisible by K or "Not divisible" if it's not.
样例输入
4 7
17 5 -21 15
样例输出
Divisible
来源
Northeastern Europe 1999
题目大意:在一串数中插于加号或减号,使最后的结果是k的倍数,如果可以输出Divisible,否则输出Not
divisible。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,i,j,k;
int num[100100],now[210],last[210];//last数组存储的是上一个数计算完后的状态,然后在此基础上计算即可
int main()
{
scanf("%d%d",&n,&k);
for (i=1;i<=n;i++)
scanf("%d",&num[i]);
now[100]=1;
for (i=1;i<=n;i++)
{
num[i]%=k;
for (j=100-k;j<=100+k;j++)
{
last[j]=now[j];
now[j]=0;
}
for (j=100-k;j<=100+k;j++)//动态规划部分
if (last[j]==1)
{
now[(j-100-num[i])%k+100]=1;
now[(j-100+num[i])%k+100]=1;
}
}
if (now[100]==1)
cout<<"Divisible";
else
cout<<"Notdivisible";
return 0;
}
//标准的动态规划,因为考虑到有可能会出现负数,所以把余数统一加上100,保证它恒为正,但是注意转移时要减去100,用原来的数据进行计算得出答案
描述
Consider an arbitrary sequence of integers. One can place + or - operatorsbetween integers in the sequence, thus deriving different arithmeticalexpressions that evaluate to different values. Let us, for example, take
thesequence: 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 beplaced between integers in the sequence in such way that resulting value isdivisible 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 ofintegers.
输入
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. Eachinteger is not greater than 10000 by it's absolute value.
输出
Write to the output file the word "Divisible" if given sequenceof integers is divisible by K or "Not divisible" if it's not.
样例输入
4 7
17 5 -21 15
样例输出
Divisible
来源
Northeastern Europe 1999
题目大意:在一串数中插于加号或减号,使最后的结果是k的倍数,如果可以输出Divisible,否则输出Not
divisible。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,i,j,k;
int num[100100],now[210],last[210];//last数组存储的是上一个数计算完后的状态,然后在此基础上计算即可
int main()
{
scanf("%d%d",&n,&k);
for (i=1;i<=n;i++)
scanf("%d",&num[i]);
now[100]=1;
for (i=1;i<=n;i++)
{
num[i]%=k;
for (j=100-k;j<=100+k;j++)
{
last[j]=now[j];
now[j]=0;
}
for (j=100-k;j<=100+k;j++)//动态规划部分
if (last[j]==1)
{
now[(j-100-num[i])%k+100]=1;
now[(j-100+num[i])%k+100]=1;
}
}
if (now[100]==1)
cout<<"Divisible";
else
cout<<"Notdivisible";
return 0;
}
//标准的动态规划,因为考虑到有可能会出现负数,所以把余数统一加上100,保证它恒为正,但是注意转移时要减去100,用原来的数据进行计算得出答案
相关文章推荐
- oc中的copy与mutableCopy
- oc中的copy与mutableCopy
- Java课程设计笔记-用org.apache.tools.zip中的类压缩/解压zip文件
- 利用php利用root权限执行shell脚本必须进行以下几个步骤
- Linux学习笔记(4)
- apache与weblogic 的整合
- python 实现 Centos 7.1 下系统自动分区并mount到/data*
- php环境搭建-WNMP(Win7+NGINX+MYSQL+PHP)
- shell
- linux命令行后台运行与调回
- Apache CXF实现Web Service(4)——Tomcat容器和Spring实现JAX-RS(RESTful) web service
- Linux中Yum 出现 Temporary failure in name resolution 解决方案
- Nginx安装
- Linux修行学习,网站持更
- nginx实现手机wap版和电脑pc版网站访问选择
- js架构设计模式——从angularJS看MVVM
- 上下联动,右侧按钮过多poper展示
- js架构设计模式——理解javascript中的MVVM开发模式
- js架构设计模式——由项目浅谈JS中MVVM模式
- linux /etc/shadow文件详解