链接: Codeforces Round #667 (Div. 3).
CF 1409 A~C
A - Yet Another Two Integers Problem
You are given two integers 𝑎 and 𝑏.In one move, you can choose some integer 𝑘 from 1 to 10 and add it to 𝑎 or subtract it from 𝑎. In other words, you choose an integer 𝑘∈[1;10] and perform 𝑎:=𝑎+𝑘. You may use different values of 𝑘 in different moves.
Your task is to find the minimum number of moves required to obtain 𝑏 from 𝑎.
You have to answer 𝑡 independent test cases.
Input
The first line of the input contains one integer 𝑡 (1≤𝑡≤2⋅104) — the number of test cases. Then 𝑡 test cases follow.
The only line of the test case contains two integers 𝑎 and 𝑏 (1≤𝑎,𝑏≤109).
Output
For each test case, print the answer: the minimum number of moves required to obtain 𝑏 from 𝑎.
Input
6
5 5
13 42
18 4
1337 420
123456789 1000000000
100500 9000
Output
0
3
2
92
87654322
9150
题意
给定a,b两个数,每次操作为a+k(1<= k <= 10),求最小操作数,从a到b。
求最小操作数,每次k取最大10,就是(a-b)/ 10 向上取整
int t;
scanf("%d",&t);
while (t--) {
int a,b,d,ans;
scanf("%d%d",&a,&b);
d=max(a,b)-min(a,b);
ans=d/10;
if (d%10) ans++;
printf("%d\n",ans);
}
B - Minimum Product
You are given four integers a, b, x and y. Initially, a≥x and b≥y. You can do the following operation no more than n times:
Choose either a or b and decrease it by one. However, as a result of this operation, value of acannot become less than x, and value of b cannot become less than y
.Your task is to find the minimum possible product of a and b (a⋅b) you can achieve by applying the given operation no more than n times.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1≤t≤2⋅104) — the number of test cases. Then t test cases follow.
The only line of the test case contains five integers a, b, x, y and n (1≤a,b,x,y,n≤109). Additional constraint on the input: a≥x and b≥y always holds.
Output
For each test case, print one integer: the minimum possible product of a and b (a⋅b) you can achieve by applying the given operation no more than n times.
Input
7
10 10 8 5 3
12 8 8 7 2
12343 43 4543 39 123212
1000000000 1000000000 1 1 1
1000000000 1000000000 1 1 1000000000
10 11 2 1 5
10 11 9 1 10
Output
70
77
177177
999999999000000000
999999999
55
10
题意
给定a、b、x、y、n,条件a≥x && b≥y,可以对a或b执行总共n次减1的操作,求最终a’*b’的最小值。
由于总共减n,所以最后两个数的和a’+b’固定为a+b-n记为sum,当a’-b’最大时,a’*b’最大。然后分两种情况:
a’为较小数
b’为较小数
以a’为较小数为例,条件为a’≥x,b’≤b,对sum进行分类:
sum≥b+x时,a’=sum-b, b’=b
sum≤b+x时,a’=x, b’=sum-x
最后将a’为最小数和b’为最小数得出的较小的一个就是答案
另外特判n > (a-x)+(b-y)的情况
int t;
scanf("%d",&t);
while (t--) {
ll a,b,x,y,n,sum1,sum2;
scanf("%lld%lld%lld%lld%lld",&a,&b,&x,&y,&n);
if (a-x+b-y <= n) {
sum1=x*y;
printf("%lld\n",sum1);
}
else {
sum1=a+b-n;
sum2=sum1;
if (sum1-x > b) sum1=b*(sum1-b);
else sum1=x*(sum1-x);
if (sum2-y > a) sum2=a*(sum2-a);
else sum2=y*(sum2-y);
printf("%lld\n",min(sum1,sum2));
}
}
C - Yet Another Array Restoration
We have a secret array. You don’t know this array and you have to restore it. However, you know some facts about this array:
The array consists of n distinct positive (greater than 0) integers.
The array contains two elements x and y (these elements are known for you) such that x<y.If you sort the array in increasing order (such that a1<a2<…<an), differences between all adjacent (consecutive) elements are equal (i.e. a2−a1=a3−a2=…=an−an−1).It can be proven that such an array always exists under the constraints given below.
Among all possible arrays that satisfy the given conditions, we ask you to restore one which has the minimum possible maximum element. In other words, you have to minimize max(a1,a2,…,an).You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1≤t≤100) — the number of test cases. Then t test cases follow.
The only line of the test case contains three integers n, x and y (2≤n≤50; 1≤x<y≤50) — the length of the array and two elements that are present in the array, respectively.
Output
For each test case, print the answer: n integers a1,a2,…,an (1≤ai≤109), where ai is the i-th element of the required array. If there are several answers, you can print any (it also means that the order of elements doesn’t matter).
It can be proven that such an array always exists under the given constraints.
Input
5
2 1 49
5 20 50
6 20 50
5 3 8
9 13 22
Output
1 49
20 40 30 50 10
26 32 20 38 44 50
8 23 18 13 3
1 10 13 4 19 22 25 16 7
题意
给定一个等差数组的长度n,和这个数组中的两个数x,y.求 元素最小的该数组。
由于是等差数列,所以只要找到公差和最小项就可以得到这个数列。公差通过假设两个数中的项数求,最小项就从最大项往前推得到,最小项要大于0。
int t;
scanf("%d",&t);
while (t--) {
int n,x,y,k,d;
scanf("%d%d%d",&n,&x,&y);
k=n-1;
d=y-x;
while (d%k != 0) {
k--;
}
d=d/k;
if ((n-1)*d < y) {
y=y-(n-1)*d;
}
else {
y=y%d;
if (y == 0) y+=d;
}
for (int i=0; i < n-1; ++i) {
printf("%d ",y+i*d);
}
printf("%d\n",y+n*d-d);
}
D - Decrease the Sum of Digits CodeForces - 1409D
You are given a positive integer n. In one move, you can increase n by one (i.e. make n:=n+1). Your task is to find the minimum number of moves you need to perform in order to make the sum of digits of n be less than or equal to s.
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1≤t≤2⋅104) — the number of test cases. Then t test cases follow.
The only line of the test case contains two integers n and s (1≤n≤1018; 1≤s≤162).
Output
For each test case, print the answer: the minimum number of moves you need to perform in order to make the sum of digits of n be less than or equal to s.
Example
Input
5
2 1
1 1
500 4
217871987498122 10
100000000000000001 1
Output
8
0
500
2128012501878
899999999999999999
lxh