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:
- dr(n) = S(n), if S(n) < 10;
- dr(n) = dr( S(n) ), if S(n) ≥ 10.
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 <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 10000 + 50;
const int MAXS = 10000 + 50;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
const int inf = 1 << 30;
#define eps 1e-8
const long long MOD = 1000000000 + 7;
const int mod = 100000;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pii;
#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
int main()
{
int n;
int k , d;
scanf("%d%d" , &k , &d);
if(d == 0)
{
if(k != 1)puts("No solution");
else printf("0\n");
return 0;
}
if(k == d)
{
while(k--)putchar('1');
putchar('\n');
}
else if(k > d)
{
int m = k - d;
while(d--)putchar('1');
while(m--)putchar('0');
putchar('\n');
}
else
{
int m = d - k;
printf("%d" , m + 1);
k--;
while(k--)putchar('1');
putchar('\n');
}
return 0;
}
————————————————————————————————————————————————————
******************************************************************************************************************************************
————————————————————————————————————————————————————
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:
- A ticket for one ride on some bus or trolley. It costs c1 burles;
- A ticket for an unlimited number of rides on some bus or on some trolley. It costs c2 burles;
- A ticket for an unlimited number of rides on all buses or all trolleys. It costs c3 burles;
- A ticket for an unlimited number of rides on all buses and trolleys. It costs c4 burles.
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 <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 1000 + 50;
const int MAXS = 10000 + 50;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
const int inf = 1 << 30;
#define eps 1e-8
const long long MOD = 1000000000 + 7;
const int mod = 100000;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pii;
#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
int a[MAXN] , b[MAXN];
// int ha[MAXN] , hb[MAXN];
int main()
{
int n , m , c1 , c2 ,c3 , c4;
while(~scanf("%d%d%d%d" , &c1 , &c2 , &c3 , &c4))
{
// clr(ha , 0) , clr(hb , 0);
scanf("%d%d" , &n , &m);
for(int i = 0 ; i < n ; i++)
{
scanf("%d" , &a[i]);
// ha[a[i]] ++;
}
for(int i = 0 ; i < m ; i++)
{
scanf("%d" , &b[i]);
// hb[b[i]]++;
}
int d1 = 0;
for(int i = 0 ; i < n ; i++)d1 += min(a[i] * c1 , c2);
// d1 = min(d1 , n * c2);
d1 = min(d1 , c3);
int d2 = 0;
for(int i = 0 ; i < m ; i++)d2 += min(b[i] * c1 , c2);
// d2 = min(d2 , m * c2);
d2 = min(d2 , c3);
// cout << d1 << ' ' << d2 << endl;
printf("%d\n" , min(d1 + d2 , c4));
}
return 0;
}
————————————————————————————————————————————————————
******************************************************************************************************************************************————————————————————————————————————————————————————
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:
- Take the leftmost item with the left hand and spend wi · l energy units (wi is a weight of the leftmost item, l is some parameter). If the previous action was the same (left-hand), then the robot spends extra Ql energy units;
- Take the rightmost item with the right hand and spend wj · r energy units (wj is a weight of the rightmost item, r is some parameter). If the previous action was the same (right-hand), then the robot spends extra Qr energy units;
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 <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 100000 + 50;
const int MAXS = 10000 + 50;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
const int inf = 1 << 30;
#define eps 1e-8
const long long MOD = 1000000000 + 7;
const int mod = 100000;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pii;
#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
int a[MAXN] , suml[MAXN] ,sumr[MAXN] , n , l , r , Ql , Qr;
int main()
{
scanf("%d%d%d%d%d" , &n , &l , &r , &Ql , &Qr);
FORR(i , 1 , n)scanf("%d" , &a[i]);
suml[0] = sumr[0] = 0;
FORR(i , 1 , n)suml[i] = suml[i - 1] + a[i] * l;
int j = 1;
REPP(i , n , 1)
{
sumr[j] = sumr[j - 1] + a[i] * r;
j++;
}
int ans = min(suml[n] + (n - 1) * Ql , sumr[n] + (n - 1) * Qr);
FORR(i , 1 , n - 1)
{
int x = i , y = n - i;
if(x > y)
{
int t = suml[x] + (x - y - 1) * Ql + sumr[y];
ans = min(ans , t);
}
else if(x == y)
{
int t = suml[x] + sumr[y];
ans = min(ans , t);
}
else
{
int t = suml[x] + (y - x - 1) * Qr + sumr[y];
ans = min(ans , t);
}
}
printf("%d\n" ,ans);
return 0;
}
————————————————————————————————————————————————————
******************************************************************************************************************************************————————————————————————————————————————————————————
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 <cstdio>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <stack>
#include <cctype>
#include <vector>
#include <queue>
#include <set>
#include <utility>
#include <cassert>
using namespace std;
///#define Online_Judge
#define outstars cout << "***********************" << endl;
#define clr(a,b) memset(a,b,sizeof(a))
#define lson l , mid , rt << 1
#define rson mid + 1 , r , rt << 1 | 1
#define mk make_pair
#define FOR(i , x , n) for(int i = (x) ; i < (n) ; i++)
#define FORR(i , x , n) for(int i = (x) ; i <= (n) ; i++)
#define REP(i , x , n) for(int i = (x) ; i > (n) ; i--)
#define REPP(i ,x , n) for(int i = (x) ; i >= (n) ; i--)
const int MAXN = 300000 + 50;
const int MAXS = 10000 + 50;
const int sigma_size = 26;
const long long LLMAX = 0x7fffffffffffffffLL;
const long long LLMIN = 0x8000000000000000LL;
const int INF = 0x7fffffff;
const int IMIN = 0x80000000;
const int inf = 1 << 30;
#define eps 1e-8
const long long MOD = 1000000000 + 7;
const int mod = 100000;
typedef long long LL;
const double PI = acos(-1.0);
typedef double D;
typedef pair<int , int> pii;
#define Bug(s) cout << "s = " << s << endl;
///#pragma comment(linker, "/STACK:102400000,102400000")
int a[MAXN] , n , k;
int main()
{
scanf("%d%d" , &n ,&k);
FOR(i , 0 , n)scanf("%d" , &a[i]);
sort(a , a + n);
int ans = a[0], b;
while(1)
{
for(b = 0; b < n ; b++)
{
if(a[b] % ans > k)
{
ans = a[b] / (a[b] / ans + 1);
break;
}
}
if(b == n)break;
}
printf("%d\n" , ans);
return 0;
}