https://codeforces.ml/contest/1352
A. Sum of Round Numbers
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
A positive (strictly greater than zero) integer is called round if it is of the form d00...0. In other words, a positive integer is round if all its digits except the leftmost (most significant) are equal to zero. In particular, all numbers from 11 to 99 (inclusive) are round.
For example, the following numbers are round: 40004000, 11, 99, 800800, 9090. The following numbers are not round: 110110, 707707, 222222, 10011001.
You are given a positive integer nn (1≤n≤1041≤n≤104). Represent the number nn as a sum of round numbers using the minimum number of summands (addends). In other words, you need to represent the given number nn as a sum of the least number of terms, each of which is a round number.
Input
The first line contains an integer tt (1≤t≤1041≤t≤104) — the number of test cases in the input. Then tt test cases follow.
Each test case is a line containing an integer nn (1≤n≤1041≤n≤104).
Output
Print tt answers to the test cases. Each answer must begin with an integer kk — the minimum number of summands. Next, kk terms must follow, each of which is a round number, and their sum is nn. The terms can be printed in any order. If there are several answers, print any of them.
Example
input
Copy
5 5009 7 9876 10000 10
output
Copy
2 5000 9 1 7 4 800 70 6 9000 1 10000 1 10
#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
using namespace std;
ll n,m,k,l,t,s;
ll a[1000001];
int main()
{
cin>>t;
while(t--)
{
cin>>n;
int j=1;
k=1;
//memset(a,0,sizeof(a));
while(n)
{
if(n%10)
{
a[j]=k*(n%10);
j++;
}
n/=10;
k*=10;
}
j--;
cout<<j<<endl;
for(int i=j;i>=1;i--)
{
cout<<a[i]<<" ";
}
cout<<endl;
}
}
B. Same Parity Summands
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given two positive integers nn (1≤n≤1091≤n≤109) and kk (1≤k≤1001≤k≤100). Represent the number nn as the sum of kk positive integers of the same parity (have the same remainder when divided by 22).
In other words, find a1,a2,…,aka1,a2,…,ak such that all ai>0ai>0, n=a1+a2+…+akn=a1+a2+…+ak and either all aiai are even or all aiai are odd at the same time.
If such a representation does not exist, then report it.
Input
The first line contains an integer tt (1≤t≤10001≤t≤1000) — the number of test cases in the input. Next, tt test cases are given, one per line.
Each test case is two positive integers nn (1≤n≤1091≤n≤109) and kk (1≤k≤1001≤k≤100).
Output
For each test case print:
- YES and the required values aiai, if the answer exists (if there are several answers, print any of them);
- NO if the answer does not exist.
The letters in the words YES and NO can be printed in any case.
Example
input
Copy
8 10 3 100 4 8 7 97 2 8 8 3 10 5 3 1000000000 9
output
Copy
YES 4 2 4 YES 55 5 5 35 NO NO YES 1 1 1 1 1 1 1 1 NO YES 3 1 1 YES 111111110 111111110 111111110 111111110 111111110 111111110 111111110 111111110 111111120
#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
using namespace std;
ll n,m,k,l,t,s1,s0;
ll a[1000001];
int main()
{
cin>>t;
while(t--)
{
cin>>n>>k;
s1=k;
s0=k*2;
if((n-s1)%2==0&&n>=s1)
{
cout<<"YES"<<endl;
cout<<1+(n-s1)<<" ";
for(int i=2;i<=k;i++)
{
cout<<1<<' ';
}
cout<<endl;
}
else if((n-s0)%2==0&&n>=s0)
{
cout<<"YES"<<endl;
cout<<2+(n-s0)<<" ";
for(int i=2;i<=k;i++)
{
cout<<2<<' ';
}
cout<<endl;
}
else
{
cout<<"NO"<<endl;
}
}
}
C. K-th Not Divisible by n
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
You are given two positive integers nn and kk. Print the kk-th positive integer that is not divisible by nn.
For example, if n=3n=3, and k=7k=7, then all numbers that are not divisible by 33 are: 1,2,4,5,7,8,10,11,13…1,2,4,5,7,8,10,11,13…. The 77-th number among them is 1010.
Input
The first line contains an integer tt (1≤t≤10001≤t≤1000) — the number of test cases in the input. Next, tt test cases are given, one per line.
Each test case is two positive integers nn (2≤n≤1092≤n≤109) and kk (1≤k≤1091≤k≤109).
Output
For each test case print the kk-th positive integer that is not divisible by nn.
Example
input
Copy
6 3 7 4 12 2 1000000000 7 97 1000000000 1000000000 2 1
output
Copy
10 15 1999999999 113 1000000001 1
#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
using namespace std;
ll n,m,k,l,t,s,s0;
ll a[1000001];
int main()
{
cin>>t;
while(t--)
{
cin>>n>>k;
s=0;
s=n-1;
l=floor(k*1.0/s)*n+k%s;
if(k%s==0)
l--;
cout<<l<<endl;
}
}
D. Alice, Bob and Candies
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
There are nn candies in a row, they are numbered from left to right from 11 to nn. The size of the ii-th candy is aiai.
Alice and Bob play an interesting and tasty game: they eat candy. Alice will eat candy from left to right, and Bob — from right to left. The game ends if all the candies are eaten.
The process consists of moves. During a move, the player eats one or more sweets from her/his side (Alice eats from the left, Bob — from the right).
Alice makes the first move. During the first move, she will eat 11 candy (its size is a1a1). Then, each successive move the players alternate — that is, Bob makes the second move, then Alice, then again Bob and so on.
On each move, a player counts the total size of candies eaten during the current move. Once this number becomes strictly greater than the total size of candies eaten by the other player on their previous move, the current player stops eating and the move ends. In other words, on a move, a player eats the smallest possible number of candies such that the sum of the sizes of candies eaten on this move is strictly greater than the sum of the sizes of candies that the other player ate on the previous move. If there are not enough candies to make a move this way, then the player eats up all the remaining candies and the game ends.
For example, if n=11n=11 and a=[3,1,4,1,5,9,2,6,5,3,5]a=[3,1,4,1,5,9,2,6,5,3,5], then:
- move 1: Alice eats one candy of size 33 and the sequence of candies becomes [1,4,1,5,9,2,6,5,3,5][1,4,1,5,9,2,6,5,3,5].
- move 2: Alice ate 33 on the previous move, which means Bob must eat 44 or more. Bob eats one candy of size 55 and the sequence of candies becomes [1,4,1,5,9,2,6,5,3][1,4,1,5,9,2,6,5,3].
- move 3: Bob ate 55 on the previous move, which means Alice must eat 66 or more. Alice eats three candies with the total size of 1+4+1=61+4+1=6 and the sequence of candies becomes [5,9,2,6,5,3][5,9,2,6,5,3].
- move 4: Alice ate 66 on the previous move, which means Bob must eat 77 or more. Bob eats two candies with the total size of 3+5=83+5=8 and the sequence of candies becomes [5,9,2,6][5,9,2,6].
- move 5: Bob ate 88 on the previous move, which means Alice must eat 99 or more. Alice eats two candies with the total size of 5+9=145+9=14 and the sequence of candies becomes [2,6][2,6].
- move 6 (the last): Alice ate 1414 on the previous move, which means Bob must eat 1515 or more. It is impossible, so Bob eats the two remaining candies and the game ends.
Print the number of moves in the game and two numbers:
- aa — the total size of all sweets eaten by Alice during the game;
- bb — the total size of all sweets eaten by Bob during the game.
Input
The first line contains an integer tt (1≤t≤50001≤t≤5000) — the number of test cases in the input. The following are descriptions of the tt test cases.
Each test case consists of two lines. The first line contains an integer nn (1≤n≤10001≤n≤1000) — the number of candies. The second line contains a sequence of integers a1,a2,…,ana1,a2,…,an (1≤ai≤10001≤ai≤1000) — the sizes of candies in the order they are arranged from left to right.
It is guaranteed that the sum of the values of nn for all sets of input data in a test does not exceed 2⋅1052⋅105.
Output
For each set of input data print three integers — the number of moves in the game and the required values aa and bb.
Example
input
Copy
7 11 3 1 4 1 5 9 2 6 5 3 5 1 1000 3 1 1 1 13 1 2 3 4 5 6 7 8 9 10 11 12 13 2 2 1 6 1 1 1 1 1 1 7 1 1 1 1 1 1 1
output
Copy
6 23 21 1 1000 0 2 1 2 6 45 46 2 2 1 3 4 2 4 4 3
#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
using namespace std;
ll n,m,k,t,s1,s0,ss;
ll a[1000001];
int main()
{
cin>>t;
while(t--)
{
cin>>n;
s1=0;
s0=0;
k=0;
rep(i,1,n) cin>>a[i];
int l=1,r=n,j=0;
while(l<=r)
{
j++;
ss=0;
while(ss<=k)
{
if(l>r)
break;
ss+=a[l];
l++;
}
if(ss<=k)
{
s0+=ss;
break;
}
s0+=ss;
k=ss;
if(l>r)
break;
j++;
ss=0;
while(ss<=k)
{
if(l>r)
break;
ss+=a[r];
r--;
}
if(ss<=k)
{
s1+=ss;
break;
}
s1+=ss;
k=ss;
}
cout<<j<<' '<<s0<<' '<<s1<<endl;
}
}
E. Special Elements
time limit per test
1 second
memory limit per test
64 megabytes
input
standard input
output
standard output
Pay attention to the non-standard memory limit in this problem.
In order to cut off efficient solutions from inefficient ones in this problem, the time limit is rather strict. Prefer to use compiled statically typed languages (e.g. C++). If you use Python, then submit solutions on PyPy. Try to write an efficient solution.
The array a=[a1,a2,…,an]a=[a1,a2,…,an] (1≤ai≤n1≤ai≤n) is given. Its element aiai is called special if there exists a pair of indices ll and rr (1≤l<r≤n1≤l<r≤n) such that ai=al+al+1+…+arai=al+al+1+…+ar. In other words, an element is called special if it can be represented as the sum of two or more consecutive elements of an array (no matter if they are special or not).
Print the number of special elements of the given array aa.
For example, if n=9n=9 and a=[3,1,4,1,5,9,2,6,5]a=[3,1,4,1,5,9,2,6,5], then the answer is 55:
- a3=4a3=4 is a special element, since a3=4=a1+a2=3+1a3=4=a1+a2=3+1;
- a5=5a5=5 is a special element, since a5=5=a2+a3=1+4a5=5=a2+a3=1+4;
- a6=9a6=9 is a special element, since a6=9=a1+a2+a3+a4=3+1+4+1a6=9=a1+a2+a3+a4=3+1+4+1;
- a8=6a8=6 is a special element, since a8=6=a2+a3+a4=1+4+1a8=6=a2+a3+a4=1+4+1;
- a9=5a9=5 is a special element, since a9=5=a2+a3=1+4a9=5=a2+a3=1+4.
Please note that some of the elements of the array aa may be equal — if several elements are equal and special, then all of them should be counted in the answer.
Input
The first line contains an integer tt (1≤t≤10001≤t≤1000) — the number of test cases in the input. Then tt test cases follow.
Each test case is given in two lines. The first line contains an integer nn (1≤n≤80001≤n≤8000) — the length of the array aa. The second line contains integers a1,a2,…,ana1,a2,…,an (1≤ai≤n1≤ai≤n).
It is guaranteed that the sum of the values of nn for all test cases in the input does not exceed 80008000.
Output
Print tt numbers — the number of special elements for each of the given arrays.
Example
input
Copy
5 9 3 1 4 1 5 9 2 6 5 3 1 1 2 5 1 1 1 1 1 8 8 7 6 5 4 3 2 1 1 1
output
Copy
5 1 0 4 0
#include<bits/stdc++.h>
#define ll int
#define rep(i,a,n) for(int i=a;i<=n;i++)
using namespace std;
ll n,k,t,s;
int main()
{
cin>>t;
while(t--)
{
cin>>n;
ll a[8001],b[8001],p[8001];
rep(i,0,8000) b[i]=0,p[i]=0;
rep(i,1,n)
{
cin>>a[i];
b[a[i]]++;
}
s=0;
for(int i=1;i<=n;i++)
{
k=a[i];
for(int j=i+1;j<=n;j++)
{
k+=a[j];
if(k>n)
break;
if(p[k]==0)
{
s+=b[k];
p[k]=1;
}
}
}
cout<<s<<endl;
}
}
F. Binary String Reconstruction
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output
For some binary string ss (i.e. each character sisi is either '0' or '1'), all pairs of consecutive (adjacent) characters were written. In other words, all substrings of length 22 were written. For each pair (substring of length 22), the number of '1' (ones) in it was calculated.
You are given three numbers:
- n0n0 — the number of such pairs of consecutive characters (substrings) where the number of ones equals 00;
- n1n1 — the number of such pairs of consecutive characters (substrings) where the number of ones equals 11;
- n2n2 — the number of such pairs of consecutive characters (substrings) where the number of ones equals 22.
For example, for the string s=s="1110011110", the following substrings would be written: "11", "11", "10", "00", "01", "11", "11", "11", "10". Thus, n0=1n0=1, n1=3n1=3, n2=5n2=5.
Your task is to restore any suitable binary string ss from the given values n0,n1,n2n0,n1,n2. It is guaranteed that at least one of the numbers n0,n1,n2n0,n1,n2 is greater than 00. Also, it is guaranteed that a solution exists.
Input
The first line contains an integer tt (1≤t≤10001≤t≤1000) — the number of test cases in the input. Then test cases follow.
Each test case consists of one line which contains three integers n0,n1,n2n0,n1,n2 (0≤n0,n1,n2≤1000≤n0,n1,n2≤100; n0+n1+n2>0n0+n1+n2>0). It is guaranteed that the answer for given n0,n1,n2n0,n1,n2 exists.
Output
Print tt lines. Each of the lines should contain a binary string corresponding to a test case. If there are several possible solutions, print any of them.
Example
input
Copy
7 1 3 5 1 1 1 3 9 3 0 1 0 3 1 2 0 0 3 2 0 0
output
Copy
1110011110 0011 0110001100101011 10 0000111 1111 000
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include<iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#define ll long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
using namespace std;
ll n0,n1,n2,m,k,t,s,l,r,mod=1e9+7,p;
int main()
{
cin>>t;
while(t--)
{
cin>>n0>>n1>>n2;
if(n1==0)
{
if(n0!=0)
{
for(int i=0;i<=n0;i++)
{
cout<<0;
}
}
if(n2!=0)
{
for(int i=0;i<=n2;i++)
{
cout<<1;
}
}
}
else
{
for(int i=0;i<=n2;i++)
{
cout<<1;
}
for(int i=0;i<=n0;i++)
{
cout<<0;
}
for(int i=1;i<n1;i++)
{
if(i%2==1)
cout<<1;
else
cout<<0;
}
}
cout<<endl;
}
}
G. Special Permutation
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
A permutation of length nn is an array p=[p1,p2,…,pn]p=[p1,p2,…,pn], which contains every integer from 11 to nn (inclusive) and, moreover, each number appears exactly once. For example, p=[3,1,4,2,5]p=[3,1,4,2,5] is a permutation of length 55.
For a given number nn (n≥2n≥2), find a permutation pp in which absolute difference (that is, the absolute value of difference) of any two neighboring (adjacent) elements is between 22 and 44, inclusive. Formally, find such permutation pp that 2≤|pi−pi+1|≤42≤|pi−pi+1|≤4 for each ii (1≤i<n1≤i<n).
Print any such permutation for the given integer nn or determine that it does not exist.
Input
The first line contains an integer tt (1≤t≤1001≤t≤100) — the number of test cases in the input. Then tt test cases follow.
Each test case is described by a single line containing an integer nn (2≤n≤10002≤n≤1000).
Output
Print tt lines. Print a permutation that meets the given requirements. If there are several such permutations, then print any of them. If no such permutation exists, print -1.
Example
input
Copy
6 10 2 4 6 7 13
output
Copy
9 6 10 8 4 7 3 1 5 2 -1 3 1 4 2 5 3 6 2 4 1 5 1 3 6 2 4 7 13 9 7 11 8 4 1 3 5 2 6 10 12
#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,n) for(int i=a;i<=n;i++)
using namespace std;
ll n,k,t,s;
map<ll,ll>m;
int main()
{
cin>>t;
while(t--)
{
cin>>n;
m.clear();
if(n<=3)
cout<<-1<<endl;
else
{
if(n%2==1)
{
k=n;
for(;k>=1;k-=2)
{
cout<<k<<' ';
}
cout<<"4 2 ";
for(int i=6;i<=n;i+=2)
{
cout<<i<<' ';
}
cout<<endl;
}
else
{
k=n-1;
for(;k>=1;k-=2)
{
cout<<k<<' ';
}
cout<<"4 2 ";
for(int i=6;i<=n;i+=2)
{
cout<<i<<' ';
}
cout<<endl;
}
}
}
}