poj1745

maksyuki 发表于 oj 分类,标签:
0

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.

Input

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.

Output

Write to the output file the word "Divisible" if given sequence of integers is divisible by K or "Not divisible" if it's not.

Sample Input

4 7

17 5 -21 15

Sample Output

Divisible

Source

Northeastern Europe 1999

 

题目类型:线性DP

算法分析:dp[i][j]表示是否能够使用前i个数字对K取模为j,初始化dp[1][a[1]%K] = true,状态转移方程为if:dp[i-1][j] == true,则dp[i][ (j+a[i])%K] = dp[i][(j-a[i])%K] = true,最后判断dp[N][0]的值即可

 

poj1844

maksyuki 发表于 oj 分类,标签: ,
0

Sum

Consider the natural numbers from 1 to N. By associating to each number a sign (+ or -) and calculating the value of this expression we obtain a sum S. The problem is to determine for a given sum S the minimum number N for which we can obtain S by associating signs for all numbers between 1 to N.

For a given S, find out the minimum value N in order to obtain S according to the conditions of the problem.

Input

The only line contains in the first line a positive integer S (0< S <= 100000) which represents the sum to be obtained.

Output

The output will contain the minimum number N for which the sum S can be obtained.

Sample Input

12

Sample Output

7

Hint

The sum 12 can be obtained from at least 7 terms in the following way: 12 = -1+2+3+4+5+6-7.

Source

Romania OI 2002

 

题目类型:线性DP

算法分析:dp[i][j]表示前1~i的数字通过加减运算是否能够表示成数字j,由于j可能会有负值,所以需要先加上一个偏移量,初始化dp[1][“0”+-1] = true,状态转移方程为dp[i][j] = dp[i-1][j-i] + dp[i-1][j+i],可以使用奇偶滚动数组来优化空间,写成“我为人人型”会TLE

 

题目类型:同余计算

算法分析:若所有的操作都是累加操作则最后的和是(1 + n) * n / 2,若还存在减法操作则记累减的和为s,则易知有(1 + n) * n / 2 - 2 * s = S,则可推得(1 + n) * n / 2和S关于2同余,则最后枚举n并判断即可

 

poj2392

maksyuki 发表于 oj 分类,标签:
0

Space Elevator

The cows are going to space! They plan to achieve orbit by building a sort of space elevator: a giant tower of blocks. They have K (1 <= K <= 400) different types of blocks with which to build the tower. Each block of type i has height h_i (1 <= h_i <= 100) and is available in quantity c_i (1 <= c_i <= 10). Due to possible damage caused by cosmic rays, no part of a block of type i can exceed a maximum altitude a_i (1 <= a_i <= 40000).

Help the cows build the tallest space elevator possible by stacking blocks on top of each other according to the rules.

Input

* Line 1: A single integer, K

* Lines 2..K+1: Each line contains three space-separated integers: h_i, a_i, and c_i. Line i+1 describes block type i.

Output

* Line 1: A single integer H, the maximum height of a tower that can be built

Sample Input

3

7 40 3

5 23 8

2 52 6

Sample Output

48

Hint

OUTPUT DETAILS:

From the bottom: 3 blocks of type 2, below 3 of type 1, below 6 of type 3. Stacking 4 blocks of type 2 and 3 of type 1 is not legal, since the top of the last type 1 block would exceed height 40.

Source

USACO 2005 March Gold

 

题目类型:多重背包

算法分析:这里先使用一个贪心思想是先按照限制高度从小到大排序,然后再直接计算一个多重背包即可