Let’s define the following recurrence:an+1=an+minDigit(an)⋅maxDigit(an).an+1=an+minDigit(an)⋅maxDigit(an).
Here minDigit(x)minDigit(x) and maxDigit(x)maxDigit(x) are the minimal and maximal digits in the decimal representation of xx without leading zeroes. For examples refer to notes.
Your task is calculate aKaK for given a1a1 and KK.Input
The first line contains one integer tt (1≤t≤10001≤t≤1000) — the number of independent test cases.
Each test case consists of a single line containing two integers a1a1 and KK (1≤a1≤10181≤a1≤1018, 1≤K≤10161≤K≤1016) separated by a space.Output
For each test case print one integer aKaK 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
a1=487a1=487
a2=a1+minDigit(a1)⋅maxDigit(a1)=487+min(4,8,7)⋅max(4,8,7)=487+4⋅8=519a2=a1+minDigit(a1)⋅maxDigit(a1)=487+min(4,8,7)⋅max(4,8,7)=487+4⋅8=519
a3=a2+minDigit(a2)⋅maxDigit(a2)=519+min(5,1,9)⋅max(5,1,9)=519+1⋅9=528a3=a2+minDigit(a2)⋅maxDigit(a2)=519+min(5,1,9)⋅max(5,1,9)=519+1⋅9=528
a4=a3+minDigit(a3)⋅maxDigit(a3)=528+min(5,2,8)⋅max(5,2,8)=528+2⋅8=544a4=a3+minDigit(a3)⋅maxDigit(a3)=528+min(5,2,8)⋅max(5,2,8)=528+2⋅8=544
a5=a4+minDigit(a4)⋅maxDigit(a4)=544+min(5,4,4)⋅max(5,4,4)=544+4⋅5=564a5=a4+minDigit(a4)⋅maxDigit(a4)=544+min(5,4,4)⋅max(5,4,4)=544+4⋅5=564
a6=a5+minDigit(a5)⋅maxDigit(a5)=564+min(5,6,4)⋅max(5,6,4)=564+4⋅6=588a6=a5+minDigit(a5)⋅maxDigit(a5)=564+min(5,6,4)⋅max(5,6,4)=564+4⋅6=588
a7=a6+minDigit(a6)⋅maxDigit(a6)=588+min(5,8,8)⋅max(5,8,8)=588+5⋅8=628a7=a6+minDigit(a6)⋅maxDigit(a6)=588+min(5,8,8)⋅max(5,8,8)=588+5⋅8=628
题意:给一个数,下一个数为这个数+(这个数分解的最大数)*(这个数分解的最小数)
技巧:考虑到分解的数出0,当有0的时候及时break,就不会超时了
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 eiei — his inexperience. Russell decided that an explorer with inexperience ee can only join the group of ee 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 TT(1≤T≤2⋅1051≤T≤2⋅105). Next 2T2T lines contain description of test cases.
The first line of description of each test case contains the number of young explorers NN (1≤N≤2⋅1051≤N≤2⋅105).
The second line contains NN integers e1,e2,…,eNe1,e2,…,eN (1≤ei≤N1≤ei≤N), where eiei is the inexperience of the ii-th explorer.
It’s guaranteed that sum of all NN doesn’t exceed 3⋅1053⋅105.Output
Print TT numbers, each number on a separate line.
In ii-th line print the maximum number of groups Russell can form in ii-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 11, so it’s not less than the size of his group.
In the second example we can organize two groups. Explorers with inexperience 11, 22 and 33 will form the first group, and the other two explorers with inexperience equal to 22 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 22, and the second group using only one explorer with inexperience equal to 11. In this case the young explorer with inexperience equal to 33 will not be included in any group.
题意:每个人有经验值,要分组,这个要求是:每个人去的组要求经验值<=人数
思路:贪心,从小到大排序,然后一个一个拉进去,当枚举到的这个人的经验值<=当前的组的人数,就可以多加一组。总的来说就是让组尽可能的多,所以要”小”组
#include
#include
#include
#include
#include
using namespace std;
const int maxn=2e5+10;
typedef long long LL;
LL a[maxn];
int main(void)
{
LL t;cin>>t;
while(t--)
{
LL n;cin>>n;
for(LL i=1;i<=n;i++)
cin>>a[i];
sort(a+1,a+1+n);
LL sum=0;LL ans=0;//sum表示最终组,ans表示枚举的人数
for(LL i=1;i<=n;i++)
{
ans++;
if(ans>=a[i])
{
sum++;
ans=0;
}
}
cout<
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 NN positive integers. Sum of all elements in his array should be equal to SS. Then Petya has to select an integer KK such that 0≤K≤S0≤K≤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 KK or S−KS−K. Otherwise Vasya loses.
You are given integers NN and SS. 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 NN and SS (1≤N≤S≤1061≤N≤S≤106) — 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 NN positive integers with sum equal to SS. In the third line print KK. 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
题意:给一个N,S,构造一个长为N的数组,使得总和为S;问你能不能构造存在一个数组使K和S-K不等于这个数组的非空子集。
思路:构造题,要尽量简单地去想,可以找找平均值,端点值,就极端值,那就取1,S-1,那么构造的只要(S/N)>=2,就说明每个值都>=2,那么1不会出现,除去最后一个值的总和为S-X,X>=2,也不会取得S-1.所以成立
#include
#include
#include
#include
#include
using namespace std;
const int maxn=1e5;
typedef long long LL;
int main(void)
{
LL n,s;cin>>n>>s;
if(s/n>=2)
{
cout<<"YES"<
You have to restore the wall. The wall consists of NN pillars of bricks, the height of the ii-th pillar is initially equal to hihi, the height is measured in number of bricks. After the restoration all the NN pillars should have equal heights.
You are allowed the following operations:
You cannot create additional pillars or ignore some of pre-existing pillars even if their height becomes 00.
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 NN, AA, RR, MM (1≤N≤1051≤N≤105, 0≤A,R,M≤1040≤A,R,M≤104) — the number of pillars and the costs of operations.
The second line contains NN integers hihi (0≤hi≤1090≤hi≤109) — 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
题意:有三堆砖头高度,有加上一个砖头的A,有减去一个砖头的B,有转移一个砖头的R。求一个高度使得他们变成同一个高度花费最小
思考:别问我为什么是凹函数,我不会证明。我单纯把这题当成一个典型题。
同时有一个之前遇到的类似(cf之前遇到的两个:一个雷击,另一个忘了)的贪心想法:移一个和加一个减一个什么时候相等?当R=A+B的时候相等,所以当R
#include