这个周学习了数论的有关内容,费马小定理,欧拉函数φ,裴蜀定理
这周的cf比赛也挺多的,不过后两场时间挺奇怪的,一场忘记打了,成绩开始有稳定的迹象了,上上下下的,一些打dive 2的大佬真变态,三分钟ac第一题,20分钟打完,自己扣扣搜搜的abc都做不完
Two Arrays And Swaps
time limit per test1 second
memory limit per test256 megabytes
inputstandard input
outputstandard output
You are given two arrays a and b both consisting of n positive (greater than zero) integers. You are also given an integer k.
In one move, you can choose two indices i and j (1≤i,j≤n) and swap ai and bj (i.e. ai becomes bj and vice versa). Note that i and j can be equal or different (in particular, swap a2 with b2 or swap a3 and b9 both are acceptable moves).
Your task is to find the maximum possible sum you can obtain in the array a if you can do no more than (i.e. at most) k such moves (swaps).
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1≤t≤200) — the number of test cases. Then t test cases follow.
The first line of the test case contains two integers n and k (1≤n≤30;0≤k≤n) — the number of elements in a and b and the maximum number of moves you can do. The second line of the test case contains n integers a1,a2,…,an (1≤ai≤30), where ai is the i-th element of a. The third line of the test case contains n integers b1,b2,…,bn (1≤bi≤30), where bi is the i-th element of b.
Output
For each test case, print the answer — the maximum possible sum you can obtain in the array a if you can do no more than (i.e. at most) k swaps.
Example
inputCopy
5
2 1
1 2
3 4
5 5
5 5 6 6 5
1 2 5 4 3
5 3
1 2 3 4 5
10 9 10 10 9
4 0
2 2 4 3
2 4 2 3
4 4
1 2 2 1
4 4 5 4
outputCopy
6
27
39
11
17
Note
In the first test case of the example, you can swap a1=1 and b2=4, so a=[4,2] and b=[3,1].
In the second test case of the example, you don’t need to swap anything.
In the third test case of the example, you can swap a1=1 and b1=10, a3=3 and b3=10 and a2=2 and b4=10, so a=[10,10,10,4,5] and b=[1,9,3,2,9].
In the fourth test case of the example, you cannot swap anything.
In the fifth test case of the example, you can swap arrays a and b, so a=[4,4,5,4] and b=[1,2,2,1].
#include<iostream>
#include<cmath>
#include <cstdio>
#include <cstring>
#include<algorithm>
using namespace std;
bool cmp1(int a,int b)
{
return a<b;
}
bool cmp2(int a,int b)
{
return a>b;
}
int main()
{
int t;
cin>>t;
while(t--)
{
int n,k,sum=0;
int a[31],b[31];
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
cin>>n>>k;
for(int i=0; i<n; i++)
cin>>a[i];
for(int i=0; i<n; i++)
cin>>b[i];
sort(a,a+n,cmp1);
sort(b,b+n,cmp2);
for(int i=0,j=0; i<k; i++,j++)
{
if(a[i]<b[j])
swap(a[i],b[j]);
else
break;
}
for(int i=0; i<n; i++)
sum+=a[i];
cout<<sum<<endl;
}
}
You are given a board of size n×n, where n is odd (not divisible by 2). Initially, each cell of the board contains one figure.
In one move, you can select exactly one figure presented in some cell and move it to one of the cells sharing a side or a corner with the current cell, i.e. from the cell (i,j) you can move the figure to cells:
(i−1,j−1);
(i−1,j);
(i−1,j+1);
(i,j−1);
(i,j+1);
(i+1,j−1);
(i+1,j);
(i+1,j+1);
Of course, you can not move figures to cells out of the board. It is allowed that after a move there will be several figures in one cell.
Your task is to find the minimum number of moves needed to get all the figures into one cell (i.e. n2−1 cells should contain 0 figures and one cell should contain n2 figures).
You have to answer t independent test cases.
Input
The first line of the input contains one integer t (1≤t≤200) — the number of test cases. Then t test cases follow.
The only line of the test case contains one integer n (1≤n<5⋅105) — the size of the board. It is guaranteed that n is odd (not divisible by 2).
It is guaranteed that the sum of n over all test cases does not exceed 5⋅105 (∑n≤5⋅105).
Output
For each test case print the answer — the minimum number of moves needed to get all the figures into one cell.
Example
inputCopy
3
1
5
499993
outputCopy
0
40
41664916690999888
#include<iostream>
#include<cmath>
#include <cstdio>
#include <cstring>
#include<algorithm>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
long long n,k=2,sum=0;
cin>>n;
if(n==1)
sum=0;
if(n==3)
sum+=8;
if(n>3)
{
sum+=8;
for(int i=3;i<n;i+=2)
sum+=(i*4+4)*(k++);
}
cout<<sum<<endl;
}
}
Let’s define the following recurrence:
an+1=an+minDigit(an)⋅maxDigit(an).
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 aK for given a1 and K.
Input
The first line contains one integer t (1≤t≤1000) — the number of independent test cases.
Each test case consists of a single line containing two integers a1 and K (1≤a1≤1018, 1≤K≤1016) separated by a space.
Output
For each test case print one integer aK on a separate line.
Example
inputCopy
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=487
a2=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=528
a4=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=564
a6=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=628
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define N 18
const int M=1e8;
int a[M],b[N];
bool cmp(int a,int b)
{
return a<b;
}
int main()
{
int t;
cin>>t;
while(t--)
{
long long int k;
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
cin>>a[1]>>k;
for(int i=1;i<=k;i++)
{
int cnt=a[i];
int num=0;
for(int j=1;cnt!=0;j++)
{
int n=cnt;
cnt=cnt/10;
b[j]=n-cnt*10;
num++;
}
sort(b+1,b+1+num,cmp);
a[i+1]=a[i]+b[1]*b[num];
}
cout<<a[k]<<endl;
}
return 0;
}