Vasya has recently found out what a digital root of a number is and he decided to share his knowledge with you.
Let's assume that S(n) is the sum of digits of number n, for example, S(4098) = 4 + 0 + 9 + 8 = 21. Then the digital root of number nequals to:
For example, dr(4098) = dr(21) = 3.
Vasya is afraid of large numbers, so the numbers he works with are at most 101000. For all such numbers, he has proved that dr(n) = S( S( S( S(n) ) ) ) (n ≤ 101000).
Now Vasya wants to quickly find numbers with the given digital root. The problem is, he hasn't learned how to do that and he asked you to help him. You task is, given numbers k and d, find the number consisting of exactly k digits (the leading zeroes are not allowed), with digital root equal to d, or else state that such number does not exist.
The first line contains two integers k and d (1 ≤ k ≤ 1000; 0 ≤ d ≤ 9).
In a single line print either any number that meets the requirements (without the leading zeroes) or "No solution" (without the quotes), if the corresponding number does not exist.
The chosen number must consist of exactly k digits. We assume that number 0 doesn't contain any leading zeroes.
4 4
5881
5 1
36172
1 0
0
For the first test sample dr(5881) = dr(22) = 4.
For the second test sample dr(36172) = dr(19) = dr(10) = 1.
A题就是简单的找规律,题意是让你构造一个k位的位之和为d的数,我们只要考虑最简单的情况,分清楚情况即可。因为没考虑No Solution的情况WA了一次。
#include
#include
#include
#include
#include
#include
******************************************************************************************************************************************
————————————————————————————————————————————————————
Vasya often uses public transport. The transport in the city is of two types: trolleys and buses. The city has n buses and m trolleys, the buses are numbered by integers from 1 to n, the trolleys are numbered by integers from 1 to m.
Public transport is not free. There are 4 types of tickets:
Vasya knows for sure the number of rides he is going to make and the transport he is going to use. He asked you for help to find the minimum sum of burles he will have to spend on the tickets.
The first line contains four integers c1, c2, c3, c4 (1 ≤ c1, c2, c3, c4 ≤ 1000) — the costs of the tickets.
The second line contains two integers n and m (1 ≤ n, m ≤ 1000) — the number of buses and trolleys Vasya is going to use.
The third line contains n integers ai (0 ≤ ai ≤ 1000) — the number of times Vasya is going to use the bus number i.
The fourth line contains m integers bi (0 ≤ bi ≤ 1000) — the number of times Vasya is going to use the trolley number i.
Print a single number — the minimum sum of burles Vasya will have to spend on the tickets.
1 3 7 19 2 3 2 5 4 4 4
12
4 3 2 1 1 3 798 1 2 3
1
100 100 8 100 3 5 7 94 12 100 1 47 0 42
16
In the first sample the profitable strategy is to buy two tickets of the first type (for the first bus), one ticket of the second type (for the second bus) and one ticket of the third type (for all trolleys). It totals to (2·1) + 3 + 7 = 12 burles.
In the second sample the profitable strategy is to buy one ticket of the fourth type.
In the third sample the profitable strategy is to buy two tickets of the third type: for all buses and for all trolleys.
#include
#include
#include
#include
#include
#include
******************************************************************************************************************************************————————————————————————————————————————————————————
Vasya has n items lying in a line. The items are consecutively numbered by numbers from 1 to n in such a way that the leftmost item has number 1, the rightmost item has number n. Each item has a weight, the i-th item weights wi kilograms.
Vasya needs to collect all these items, however he won't do it by himself. He uses his brand new robot. The robot has two different arms — the left one and the right one. The robot can consecutively perform the following actions:
Naturally, Vasya wants to program the robot in a way that the robot spends as little energy as possible. He asked you to solve this problem. Your task is to find the minimum number of energy units robot spends to collect all items.
The first line contains five integers n, l, r, Ql, Qr (1 ≤ n ≤ 105; 1 ≤ l, r ≤ 100; 1 ≤ Ql, Qr ≤ 104).
The second line contains n integers w1, w2, ..., wn (1 ≤ wi ≤ 100).
In the single line print a single number — the answer to the problem.
3 4 4 19 1 42 3 99
576
4 7 2 3 9 1 2 3 4
34
Consider the first sample. As l = r, we can take an item in turns: first from the left side, then from the right one and last item from the left. In total the robot spends 4·42 + 4·99 + 4·3 = 576 energy units.
The second sample. The optimal solution is to take one item from the right, then one item from the left and two items from the right. In total the robot spends (2·4) + (7·1) + (2·3) + (2·2 + 9) = 34 energy units.
一开始看这题长得一脸DP的样子,就去搞DP了,后来才得知可以直接贪心的。
思路是我先预处理出前缀和suml,后缀和sumr,然后去遍历一遍数组,贪心的原则就是尽量减少Ql和Qr,取最小值即可。
#include
#include
#include
#include
#include
#include
******************************************************************************************************************************************————————————————————————————————————————————————————
D题不会
————————————————————————————————————————————————————
******************************************************************************************************************************************————————————————————————————————————————————————————
Vasya's got a birthday coming up and his mom decided to give him an array of positive integers a of length n.
Vasya thinks that an array's beauty is the greatest common divisor of all its elements. His mom, of course, wants to give him as beautiful an array as possible (with largest possible beauty). Unfortunately, the shop has only one array a left. On the plus side, the seller said that he could decrease some numbers in the array (no more than by k for each number).
The seller can obtain array b from array a if the following conditions hold: bi > 0; 0 ≤ ai - bi ≤ k for all 1 ≤ i ≤ n.
Help mom find the maximum possible beauty of the array she will give to Vasya (that seller can obtain).
The first line contains two integers n and k (1 ≤ n ≤ 3·105; 1 ≤ k ≤ 106). The second line contains n integers ai (1 ≤ ai ≤ 106) — array a.
In the single line print a single number — the maximum possible beauty of the resulting array.
6 1 3 6 10 12 13 16
3
5 3 8 21 52 15 77
7
In the first sample we can obtain the array:
3 6 9 12 12 15
In the second sample we can obtain the next array:
7 21 49 14 77
思路就是先排个序,用最小的数字当作质因子,如果遇到一个不满足的就取该数的最大质因子从头判断,直到找到结果。
#include
#include
#include
#include
#include
#include