# Codeforces Round #643 (Div. 2)

## A. Sequence with Digits

time limit per test1 second memory limit per test256 megabytes

Let's define the following recurrence:$$a_{n+1} = a_{n} + minDigit(a_{n}) \cdot maxDigit(a_{n})$$.

Here minDigit(x) and maxDigit(x) are the minimal and maximal digits in the decimal representation of x without leading zeroes. For examples refer to notes.

Your task is calculate a_{K} for given a_{1} and K.Input

The first line contains one integer t $$(1 \le t \le 1000)$$ — the number of independent test cases.

Each test case consists of a single line containing two integers $$a_{1} and K (1 \le a_{1} \le 10^{18}, 1 \le K \le 10^{16})$$ separated by a space.Output

For each test case print one integer a_{K} on a separate line.ExampleinputCopy

8
1 4
487 1
487 2
487 3
487 4
487 5
487 6
487 7


outputCopy

42
487
519
528
544
564
588
628


Note

a_{1} = 487

a_{2} = a_{1} + minDigit(a_{1}) \cdot maxDigit(a_{1}) = 487 + \min (4, 8, 7) \cdot \max (4, 8, 7) = 487 + 4 \cdot 8 = 519

a_{3} = a_{2} + minDigit(a_{2}) \cdot maxDigit(a_{2}) = 519 + \min (5, 1, 9) \cdot \max (5, 1, 9) = 519 + 1 \cdot 9 = 528

a_{4} = a_{3} + minDigit(a_{3}) \cdot maxDigit(a_{3}) = 528 + \min (5, 2, 8) \cdot \max (5, 2, 8) = 528 + 2 \cdot 8 = 544

a_{5} = a_{4} + minDigit(a_{4}) \cdot maxDigit(a_{4}) = 544 + \min (5, 4, 4) \cdot \max (5, 4, 4) = 544 + 4 \cdot 5 = 564

a_{6} = a_{5} + minDigit(a_{5}) \cdot maxDigit(a_{5}) = 564 + \min (5, 6, 4) \cdot \max (5, 6, 4) = 564 + 4 \cdot 6 = 588

a_{7} = a_{6} + minDigit(a_{6}) \cdot maxDigit(a_{6}) = 588 + \min (5, 8, 8) \cdot \max (5, 8, 8) = 588 + 5 \cdot 8 = 628

#### 显然不会过几轮minDigit就变成0了

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(auto i=(a);i<=(b);++i)
#define dep(i,a,b) for(auto i=(a);i>=(b);--i)
#define pb push_back
typedef long long ll;
const int maxn=(int)1e6+100;
const int mod=(int)1e9+7;
int fun(ll x){
ll mn=10,mx=0;
while(x){
mn=min(mn,x%10);mx=max(mx,x%10);
x/=10;
}
return mn*mx;
}
void solve(){
ll x,k;scanf("%lld%lld",&x,&k);
rep(i,2,k){
int tmp=fun(x);
x+=tmp;
if(tmp==0) break;
}
printf("%lld\n",x);
}
int main(){
int T;cin>>T;
while(T--) solve();
}


## B. Young Explorers

time limit per test2 seconds memory limit per test256 megabytes

Young wilderness explorers set off to their first expedition led by senior explorer Russell. Explorers went into a forest, set up a camp and decided to split into groups to explore as much interesting locations as possible. Russell was trying to form groups, but ran into some difficulties...

Most of the young explorers are inexperienced, and sending them alone would be a mistake. Even Russell himself became senior explorer not long ago. Each of young explorers has a positive integer parameter e_i — his inexperience. Russell decided that an explorer with inexperience e can only join the group of e or more people.

Now Russell needs to figure out how many groups he can organize. It's not necessary to include every explorer in one of the groups: some can stay in the camp. Russell is worried about this expedition, so he asked you to help him.Input

The first line contains the number of independent test cases $$T(1 \leq T \leq 2 \cdot 10^5)$$. Next 2T lines contain description of test cases.

The first line of description of each test case contains the number of young explorers $$N (1 \leq N \leq 2 \cdot 10^5)$$.

The second line contains N integers $$e_1, e_2, \ldots, e_N (1 \leq e_i \leq N)$$, where e_i is the inexperience of the i-th explorer.

It's guaranteed that sum of all N doesn't exceed $$3 \cdot 10^5$$.Output

Print T numbers, each number on a separate line.

In i-th line print the maximum number of groups Russell can form in i-th test case.ExampleinputCopy

2
3
1 1 1
5
2 3 1 2 2


outputCopy

3
2


Note

In the first example we can organize three groups. There will be only one explorer in each group. It's correct because inexperience of each explorer equals to 1, so it's not less than the size of his group.

In the second example we can organize two groups. Explorers with inexperience 1, 2 and 3 will form the first group, and the other two explorers with inexperience equal to 2 will form the second group.

This solution is not unique. For example, we can form the first group using the three explorers with inexperience equal to 2, and the second group using only one explorer with inexperience equal to 1. In this case the young explorer with inexperience equal to 3 will not be included in any group.

#### 签到题，秒了，没什么好说的

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(auto i=(a);i<=(b);++i)
#define dep(i,a,b) for(auto i=(a);i>=(b);--i)
#define pb push_back
typedef long long ll;
const int maxn=(int)1e6+100;
const int mod=(int)1e9+7;
int n,a[maxn];
void solve(){
scanf("%d",&n);
rep(i,1,n) scanf("%d",&a[i]);
sort(a+1,a+1+n);
int ans=0,cnt=0;
rep(i,1,n){
cnt++;
if(a[i]<=cnt) ans++,cnt=0;
}printf("%d\n",ans);
}
int main(){
int T;cin>>T;
while(T--) solve();
}


## C. Count Triangles

time limit per test1 second memory limit per test256 megabytes

Like any unknown mathematician, Yuri has favourite numbers: A, B, C, and D, where $$A \leq B \leq C \leq D$$. Yuri also likes triangles and once he thought: how many non-degenerate triangles with integer sides x, y, and z exist, such that $$A \leq x \leq B \leq y \leq C \leq z \leq D$$ holds?

Yuri is preparing problems for a new contest now, so he is very busy. That's why he asked you to calculate the number of triangles with described property.

The triangle is called non-degenerate if and only if its vertices are not collinear.Input

The first line contains four integers: A, B, C and D$$(1 \leq A \leq B \leq C \leq D \leq 5 \cdot 10^5)$$ — Yuri's favourite numbers.Output

Print the number of non-degenerate triangles with integer sides x, y, and z such that the inequality $$A \leq x \leq B \leq y \leq C \leq z \leq D$$ holds.ExamplesinputCopy

1 2 3 4


outputCopy

4


inputCopy

1 2 2 5


outputCopy

3


inputCopy

500000 500000 500000 500000


outputCopy

1


Note

In the first example Yuri can make up triangles with sides (1, 3, 3), (2, 2, 3), (2, 3, 3) and (2, 3, 4).

In the second example Yuri can make up triangles with sides (1, 2, 2), (2, 2, 2) and (2, 2, 3).

In the third example Yuri can make up only one equilateral triangle with sides equal to 5 \cdot 10^5.

#### 前缀和搞一下；$$p[i]$$表示三角形小的两边和为i时第三条边的合法方案数，然后前缀和；

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(auto i=(a);i<=(b);++i)
#define dep(i,a,b) for(auto i=(a);i>=(b);--i)
#define pb push_back
typedef long long ll;
const int maxn=(int)1e6+100;
const int mod=(int)1e9+7;
ll p[maxn];
int main(){
cin.tie(0)->sync_with_stdio(0);
ll a,b,c,d,ans=0;cin>>a>>b>>c>>d;
rep(i,1,maxn-100) p[i]=max(0ll,min(d+1,1ll*i)-c);
rep(i,1,b+c) p[i]+=p[i-1];
rep(i,a,b) ans+=p[i+c]-p[i+b-1];
cout<<ans;
}


## D. Game With Array

time limit per test1 second memory limit per test256 megabytes

Petya and Vasya are competing with each other in a new interesting game as they always do.

At the beginning of the game Petya has to come up with an array of N positive integers. Sum of all elements in his array should be equal to S. Then Petya has to select an integer K such that $$0 \leq K \leq S$$.

In order to win, Vasya has to find a non-empty subarray in Petya's array such that the sum of all selected elements equals to either K or S - K. Otherwise Vasya loses.

You are given integers N and S. You should determine if Petya can win, considering Vasya plays optimally. If Petya can win, help him to do that.Input

The first line contains two integers N and $$S (1 \leq N \leq S \leq 10^{6})\$$ — the required length of the array and the required sum of its elements.Output

If Petya can win, print "YES" (without quotes) in the first line. Then print Petya's array in the second line. The array should contain N positive integers with sum equal to S. In the third line print K. If there are many correct answers, you can print any of them.

If Petya can't win, print "NO" (without quotes).

You can print each letter in any register (lowercase or uppercase).ExamplesinputCopy

1 4


outputCopy

YES
4
2

inputCopy

3 4


outputCopy

NO

inputCopy

3 8


outputCopy

YES
2 1 5
4

#### 显然如代码那样构造是最优的（我也不知道为什么）

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(auto i=(a);i<=(b);++i)
#define dep(i,a,b) for(auto i=(a);i>=(b);--i)
#define pb push_back
typedef long long ll;
const int maxn=(int)1e6+100;
const int mod=(int)1e9+7;
int main(){
int n,s;scanf("%d%d",&n,&s);
if(s>1&&s-n+1-n>0){
puts("YES");
rep(i,1,n) printf(i==n?"%d\n":"1 ",s-n+1);
printf("%d\n",n);
}else puts("NO");
}


## E. Restorer Distance

time limit per test1 second memory limit per test256 megabytes

You have to restore the wall. The wall consists of N pillars of bricks, the height of the i-th pillar is initially equal to h_{i}, the height is measured in number of bricks. After the restoration all the N pillars should have equal heights.

You are allowed the following operations:

• put a brick on top of one pillar, the cost of this operation is A;
• remove a brick from the top of one non-empty pillar, the cost of this operation is R;
• move a brick from the top of one non-empty pillar to the top of another pillar, the cost of this operation is M.

You cannot create additional pillars or ignore some of pre-existing pillars even if their height becomes 0.

What is the minimal total cost of restoration, in other words, what is the minimal total cost to make all the pillars of equal height?Input

The first line of input contains four integers $$N, A, R, M (1 \le N \le 10^{5}, 0 \le A, R, M \le 10^{4})$$ — the number of pillars and the costs of operations.

The second line contains N integers $$h_{i} (0 \le h_{i} \le 10^{9})$$ — initial heights of pillars.Output

Print one integer — the minimal cost of restoration.ExamplesinputCopy

3 1 100 100
1 3 8


outputCopy

12


inputCopy

3 100 1 100
1 3 8


outputCopy

9


inputCopy

3 100 100 1
1 3 8


outputCopy

4


inputCopy

5 1 2 4
5 5 3 6 5


outputCopy

4


inputCopy

5 1 2 2
5 5 3 6 5


outputCopy

3


#### 画一个（高度-花费）的图大致就是一个凹函数，虽然没有很严谨的证明过但是二分肯定是不对的（这个很好找反例），那么三分套一下就过了；

#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(auto i=(a);i<=(b);++i)
#define dep(i,a,b) for(auto i=(a);i>=(b);--i)
#define pb push_back
typedef long long ll;
const int maxn=(int)1e6+100;
const int mod=(int)1e9+7;
const ll inf=(ll)1ll<<62;
int n,a,r,m,h[maxn];
ll ans=inf;
ll check(int x){
ll s1=0,s2=0;
rep(i,1,n){
if(h[i]<x) s1+=x-h[i];
else s2+=h[i]-x;
}
ll tmp=min(s1,s2)*min(m,a+r)+abs(s1-s2)*(s1>s2?a:r);
ans=min(ans,tmp);
return tmp;
}
int main(){
cin.tie(0)->sync_with_stdio(0);
cin>>n>>a>>r>>m;
rep(i,1,n) cin>>h[i];
sort(h+1,h+1+n);
int l=h[1],r=h[n];
while(l<r){
int lmid=l+(r-l)/3,rmid=r-(r-l)/3;
if(check(rmid)>=check(lmid)) r=rmid-1;
else l=lmid+1;
}
if(ans==inf) ans=0;
cout<<ans;
}


0 评论