A:A - Sum of 2050
A number is called 2050-number if it is 2050, 20500, …, (2050⋅10k for integer k≥0).Given a number n
, you are asked to represent n as the sum of some (not necessarily distinct) 2050-numbers. Compute the minimum number of 2050-numbers required for that.
Input
The first line contains a single integer T
(1≤T≤1000
) denoting the number of test cases.
The only line of each test case contains a single integer n
(1≤n≤1018
) denoting the number to be represented.
Output
For each test case, output the minimum number of 2050-numbers in one line.
If n
cannot be represented as the sum of 2050-numbers, output −1
instead.
Example
Input
6
205
2050
4100
20500
22550
25308639900
Output
-1
1
2
1
2
36
主要思路:
类似于高精度除法的计算过程
#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
typedef long long ll;
int main()
{
int t;
cin>>t;
while(t--)
{
ll x=0;
string s;
cin>>s;
ll ans=0;
for(int i=0;i<s.size();i++)
{
x=x*10+s[i]-'0';
}
if(x<2050)
{
cout<<-1<<endl;
}
else
{
ll xx=0;
ll ans=0;
for(int i=0;i<s.size();i++)//从最高位开始除
{
xx=xx*10+s[i]-'0';
if(xx>=2050)
{
ans+=xx/2050;
xx=xx%2050;
}
}
if(xx==0)
cout<<ans<<endl;
else cout<<-1<<endl;
}
}
return 0;
}
B - Morning Jogging
The 2050 volunteers are organizing the “Run! Chase the Rising Sun” activity. Starting on Apr 25 at 7:30 am, runners will complete the 6km trail around the Yunqi town.
There are n+1
checkpoints on the trail. They are numbered by 0, 1, …, n. A runner must start at checkpoint 0 and finish at checkpoint n. No checkpoint is skippable — he must run from checkpoint 0 to checkpoint 1, then from checkpoint 1 to checkpoint 2
and so on. Look at the picture in notes section for clarification.
Between any two adjacent checkpoints, there are m
different paths to choose. For any 1≤i≤n, to run from checkpoint i−1 to checkpoint i, a runner can choose exactly one from the m possible paths. The length of the j-th path between checkpoint i−1 and i is bi,j for any 1≤j≤m and 1≤i≤n
.
To test the trail, we have m
runners. Each runner must run from the checkpoint 0 to the checkpoint n once, visiting all the checkpoints. Every path between every pair of adjacent checkpoints needs to be ran by exactly one runner. If a runner chooses the path of length li between checkpoint i−1 and i (1≤i≤n), his tiredness is
mini=1nli,
i. e. the minimum length of the paths he takes.
Please arrange the paths of the m
runners to minimize the sum of tiredness of them.
Input
Each test contains multiple test cases. The first line contains the number of test cases t
(1≤t≤10000
). Description of the test cases follows.
The first line of each test case contains two integers n
and m (1≤n,m≤100
).
The i
-th of the next n lines contains m integers bi,1, bi,2, …, bi,m (1≤bi,j≤109
).
It is guaranteed that the sum of n⋅m
over all test cases does not exceed 104
.
Output
For each test case, output n
lines. The j-th number in the i-th line should contain the length of the path that runner j chooses to run from checkpoint i−1 to checkpoint i. There should be exactly m integers in the i-th line and these integers should form a permuatation of bi,1, …, bi,m for all 1≤i≤n
.
If there are multiple answers, print any.
Example
Input
2
2 3
2 3 4
1 3 5
3 2
2 3
4 1
3 5
Output
2 3 4
5 3 1
2 3
4 1
3 5
主要思路:
主要利用swap函数来交换
找到就跳出降低时间复杂度
一个位置仅交换一次,如果有相等的数,可能会把排好的顺序打乱
#include<iostream>
#include<algorithm>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int a[101][101]={0},b[100001]={0},c[101][101]={0};
int n,m,k=0;
cin>>n>>m;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
cin>>a[i][j];
b[k++]=a[i][j];
}
}
sort(b,b+k);//方便查询第k小的数
k=m;
while(k--)
{
int flag=0;
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
{
if(a[i][j]==b[k]&&!c[i][j])
{
swap(a[i][j],a[i][k]);
c[i][k]=1;
flag=1;
break;
}
}
if(flag) break;
}
}
for(int i=0;i<n;i++)
{
for(int j=0;j<m;j++)
cout<<a[i][j]<<' ';
cout<<endl;
}
}
return 0;
}
C - Perfectly Imperfect Array
从数组中选数做积,问是不是有不能恰好开平方的时候
主要思路:
其实只要判断是不是存在不能开方的数即可
因为两个可以开方的数的积一定可以开方
49=66
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
int n,a[100001];
cin>>n;
bool book=0;
for(int i=0;i<n;i++)
{
cin>>a[i];
double xx=sqrt(a[i]);
if(xx!=(int)xx)
book=1;
}
if(book) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}
D:D - AND 0, Sum Big
#include<iostream>
#include<algorithm>
using namespace std;
int mod=1e9+7;
int main()
{
int t;
cin>>t;
while(t--)
{
long long n,k,ans=1;
cin>>n>>k;
for(int i=0;i<k;i++)
ans=(ans*n)%mod;
cout<<ans<<endl;
}
}
E- Product 1 Modulo N
主要思路:
裴蜀定理:它的一个重要推论是:a,b互质的充分必要条件是存在整数x,y使ax+by=1。
逆元:是指一个可以取消另一给定元素运算的元素,在数学里,逆元素广义化了加法中的加法逆元和乘法中的倒数。
只有与n互质的数%n才有可能为0,所以将所有的互质的数放入,若乘积mul%n!=1
mul=nk+p;
mul/p=nk/p+1;
所以将p从ans中移除即可
注意爆int
#include<iostream>
#include<map>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
int main()
{
ll n;
cin>>n;
map<ll,ll> book;
int ans=1;
vector<ll> a;
for(ll i=1;i<n;i++)
{
if(__gcd(i,n)!=1)
book[i]=1;
else ans=(ans*i)%n;
}
if(ans!=1)
book[ans]=1;
for(ll i=1;i<n;i++)
{
if(!book[i])
a.push_back(i);
}
cout<<a.size()<<endl;
for(ll i=0;i<a.size();i++)
cout<<a[i]<<' ';
return 0;
}
F - Min Cost String
Let’s define the cost of a string s as the number of index pairs i and j (1≤i<j<|s|) such that si=sj and si+1=sj+1
.
You are given two positive integers n
and k. Among all strings with length n that contain only the first k
characters of the Latin alphabet, find a string with minimum possible cost. If there are multiple such strings with minimum cost — find any of them.
Input
The only line contains two integers n
and k (1≤n≤2⋅105;1≤k≤26
).
Output
Print the string s
such that it consists of n characters, each its character is one of the k
first Latin letters, and it has the minimum possible cost among all these strings. If there are multiple such strings — print any of them.
Examples
Input
9 4
Output
aabacadbb
Input
5 1
Output
aaaaa
Input
10 26
Output
codeforces
主要思路:
构造加欧拉函数
aabacad…时有最少的对数
#include<iostream>
#include<cstring>
using namespace std;
int main()
{
int n,k;
cin>>n>>k;
while(1)//防止一轮完了以后n不够
{
for(int i=0;i<k;i++)
{
for(int j=i;j<k;j++)
{
cout<<(char)('a'+i);
n--;
if(n==0) return 0;
if(j!=i)//防止aaa
{
cout<<(char)('a'+j);
n--;
if(n==0) return 0;
}
}
}
}
return 0;
}