18.06.30 POJ 百练1745:Divisibility

描述

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

来源

Northeastern Europe 1999

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <memory.h>
 4 #include <algorithm>
 5 #include <stdlib.h>
 6 #include <math.h>
 7 #include <iostream>
 8 #include<queue>
 9 #include <vector>
10 #include <bitset>
11 using namespace std;
12
13 int dp[2][105];
14 int n, k;
15 int num[10005];
16
17 void solve() {
18     dp[1][num[1]] = 1;
19     for (int i = 2; i <= n; i++)
20     {
21         for (int j = 0; j < k; j++)dp[i % 2][j] = 0;
22         for (int j = 0; j < k; j++)
23             if (dp[(i - 1) % 2][j])
24             {
25                 dp[i % 2][(num[i] + j) % k] = 1;
26                 dp[i % 2][(k+j - num[i]) % k] = 1;
27             }
28     }
29     if (dp[n%2][0])
30         printf("Divisible\n");
31     else
32         printf("Not divisible\n");
33 }
34
35 int main()
36 {
37     scanf("%d%d", &n, &k);
38     for (int i = 1; i <= n; i++)
39     {
40         scanf("%d", &num[i]);
41         num[i] = abs(num[i]) % k;
42     }
43     memset(dp, 0, sizeof(dp));
44     solve();
45     return 0;
46 }

想用滚动数组结果忘了每次初始化,debug很久

原文地址:https://www.cnblogs.com/yalphait/p/9247211.html

时间: 2024-08-01 03:16:43

18.06.30 POJ 百练1745:Divisibility的相关文章

百练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, tak

poj 百练 2765 八进制小数(精度问题)

2765:八进制小数 查看 提交 统计 提示 提问 总时间限制:  1000ms  内存限制:  65536kB 描述 八进制小数可以用十进制小数精确的表示.比如,八进制里面的0.75等于十进制里面的0.963125 (7/8 + 5/64).所有小数点后位数为n的八进制小数都可以表示成小数点后位数不多于3n的十进制小数. 你的任务是写一个程序,把(0, 1)中的八进制小数转化成十进制小数. 输入 输入包括若干八进制小数,每个小数占用一行.每个小数的形式是0.d1d2d3 ... dk,这里di

POJ百练—IP地址转换

#include<iostream> #include<cstdio> #include<cstring> using namespace std; int sign[9]; void change_to_8(int x) { int index=8; while(x) { if(x&1) { sign[index]=1; } x>>=1; index--; } } int main() { int a[4]; while(scanf("%

Poj OpenJudge 百练 2632 Crashing Robots

1.Link: http://poj.org/problem?id=2632 http://bailian.openjudge.cn/practice/2632/ 2.Content: Crashing Robots Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7912   Accepted: 3441 Description In a modernized warehouse, robots are used to

[OpenJudge] 百练2754 八皇后

八皇后 Description 会下国际象棋的人都很清楚:皇后可以在横.竖.斜线上不限步数地吃掉其他棋子.如何将8个皇后放在棋盘上(有8 * 8个方格),使它们谁也不能被吃掉!这就是著名的八皇后问题. 对于某个满足要求的8皇后的摆放方法,定义一个皇后串a与之对应,即a=b1b2...b8,其中bi为相应摆法中第i行皇后所处的列数.已经知道8皇后问题一共有92组解(即92个不同的皇后串).给出一个数b,要求输出第b个串.串的比较是这样的:皇后串x置于皇后串y之前,当且仅当将x视为整数时比y小. I

百练 1088 滑雪

“人人为我”的解法: dp[i][j]表示坐标为(i,j)的点开始下滑的最大长度. 则dp[i][j]为(i,j)周围四个点中比(i,j)低,且最大长度最大再加一的值 用结构体来储存一个点的坐标和高度,这样按高度从小到大排完序以后还不会丢失坐标的值 从小到大遍历所有的点,经过一个点(i,j)时,用递推公式求L(i,j). 一个小技巧: 将矩阵height四周的值赋值为INF,你可以想想这是滑雪场四周非常非常高的围墙. 这样就避免了数组越界的判断,而且不会影响正确结果(因为我们找的是滑雪场内部的最

百练6255-单词反转-2016正式B题

百练 / 2016计算机学科夏令营上机考试 已经结束 题目 排名 状态 统计 提问 B:单词翻转 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 输入一个句子(一行),将句子中的每一个单词翻转后输出. 输入 只有一行,为一个字符串,不超过500个字符.单词之间以空格隔开. 输出 翻转每一个单词后的字符串,单词之间的空格需与原文一致. 样例输入 hello world 样例输出 olleh dlrow 1 #include <iostream> 2 #i

POJ百道水题列表

以下是poj百道水题,新手可以考虑从这里刷起 搜索1002 Fire Net1004 Anagrams by Stack1005 Jugs1008 Gnome Tetravex1091 Knight Moves1101 Gamblers1204 Additive equations 1221 Risk1230 Legendary Pokemon1249 Pushing Boxes 1364 Machine Schedule1368 BOAT1406 Jungle Roads1411 Annive

百练8216-分段函数-2016正式A题

百练 / 2016计算机学科夏令营上机考试 已经结束 题目 排名 状态 统计 提问 A:分段函数 查看 提交 统计 提问 总时间限制:  1000ms 内存限制:  65536kB 描述 编写程序,计算下列分段函数y=f(x)的值. y=-x+2.5; 0 <= x < 5 y=2-1.5(x-3)(x-3); 5 <= x < 10 y=x/2-1.5; 10 <= x < 20 输入 一个浮点数N,0 <= N < 20 输出 输出N对应的分段函数值:f