A - Candies
Recently Vova found nn candy wrappers. He remembers that he bought xx candies during the first day, 2x2x candies during the second day, 4x4x candies during the third day, ……, 2k−1x2k−1x candies during the kk-th day. But there is an issue: Vova remembers neither xx nor kk but he is sure that xx and kk are positive integers and k>1k>1.
Vova will be satisfied if you tell him any positive integer xx so there is an integer k>1k>1 that x+2x+4x+⋯+2k−1x=nx+2x+4x+⋯+2k−1x=n. It is guaranteed that at least one solution exists. Note that k>1k>1.
You have to answer tt independent test cases.
Input
The first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases. Then tt test cases follow.
The only line of the test case contains one integer nn (3≤n≤1093≤n≤109) — the number of candy wrappers Vova found. It is guaranteed that there is some positive integer xx and integer k>1k>1 that x+2x+4x+⋯+2k−1x=nx+2x+4x+⋯+2k−1x=n.
Output
Print one integer — any positive integer value of xx so there is an integer k>1k>1 that x+2x+4x+⋯+2k−1x=n
题意
t个样例,给出n根据已知的数学关系,求x,k。
思路
将x提出根据关系3<=n<=10^9。
代码
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--){
int n,k,x,s;
cin>>n;
s=3; //从第三项开始,前两项和为3.
k=1;
while(n%s!=0){
k++;
s=s+pow(2,k);
}
x=n/s;
cout<<x<<endl;
}
}
B - Balanced Array
You are given a positive integer nn, it is guaranteed that nn is even (i.e. divisible by 22).
You want to construct the array aa of length nn such that:
The first n2n2 elements of aa are even (divisible by 22);
the second n2n2 elements of aa are odd (not divisible by 22);
all elements of aa are distinct and positive;
the sum of the first half equals to the sum of the second half (∑i=1n2ai=∑i=n2+1nai∑i=1n2ai=∑i=n2+1nai).
If there are multiple answers, you can print any. It is not guaranteed that the answer exists.
You have to answer tt independent test cases.
Input
The first line of the input contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases. Then tt test cases follow.
The only line of the test case contains one integer nn (2≤n≤2⋅1052≤n≤2⋅105) — the length of the array. It is guaranteed that that nn is even (i.e. divisible by 22).
It is guaranteed that the sum of nn over all test cases does not exceed 2⋅1052⋅105 (∑n≤2⋅105∑n≤2⋅105).
Output
For each test case, print the answer — “NO” (without quotes), if there is no suitable answer for the given test case or “YES” in the first line and any suitable array a1,a2,…,ana1,a2,…,an (1≤ai≤1091≤ai≤109) satisfying conditions from the problem statement on the second line.
Example
Input
5
2
4
6
8
10
Output
NO
YES
2 4 1 5
NO
YES
2 4 6 8 1 3 5 11
NO
题意
t个样例,给出一个偶数n,前n/2个数为偶数后n/2为奇数,前后两部分和相等。如果存在这样的数组输出YES。
思路
如果n/2是奇数前一部分为偶数后部分为奇数不成立。反之从2开始将前n/2个偶数写满,接着对后半部分从1输入奇数前n/2-1最后一项用偶数和减去其它奇数和。
代码
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--){
int n,i,j;
cin>>n;
int a[n+1],sum1=2,sum2=1;
a[1]=2;
a[n/2+1]=1;
if(n%4!=0)cout<<"NO"<<endl;
else{
for(i=2;i<=n/2;i++){
a[i]=a[i-1]+2;
sum1+=a[i];
}
for(j=n/2+2;j<n;j++){
a[j]=a[j-1]+2;
sum2+=a[j];
}
a[n]=sum1-sum2;
cout<<"YES"<<endl;
cout<<a[1];
for(i=2;i<n+1;i++){
cout<<" "<<a[i];
}
cout<<endl;
}
}
}
C - Ichihime and Triangle
Ichihime is the current priestess of the Mahjong Soul Temple. She claims to be human, despite her cat ears.
These days the temple is holding a math contest. Usually, Ichihime lacks interest in these things, but this time the prize for the winner is her favorite — cookies. Ichihime decides to attend the contest. Now she is solving the following problem.
You are given four positive integers aa, bb, cc, dd, such that a≤b≤c≤da≤b≤c≤d.
Your task is to find three integers xx, yy, zz, satisfying the following conditions:
a≤x≤ba≤x≤b.
b≤y≤cb≤y≤c.
c≤z≤dc≤z≤d.
There exists a triangle with a positive non-zero area and the lengths of its three sides are xx, yy, and zz.
Ichihime desires to get the cookie, but the problem seems too hard for her. Can you help her?
Input
The first line contains a single integer tt (1≤t≤10001≤t≤1000) — the number of test cases.
The next tt lines describe test cases. Each test case is given as four space-separated integers aa, bb, cc, dd (1≤a≤b≤c≤d≤1091≤a≤b≤c≤d≤109).
Output
For each test case, print three integers xx, yy, zz — the integers you found satisfying the conditions given in the statement.
It is guaranteed that the answer always exists. If there are multiple answers, print any.
Example
Input
4
1 3 5 7
1 5 5 7
100000 200000 300000 400000
1 1 977539810 977539810
Output
3 4 5
5 5 5
182690 214748 300999
1 977539810 977539810
题意
在a<=b<=c<=d,x,y,z属于(a,b)(b,c)(c,d),求一个三角形边长为x,y,z。
思路
只要构造一个等腰三角形,并且两个腰相加一定大于底。
代码
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--){
int x,y,z,a,b,c,d;
cin>>a>>b>>c>>d;
cout<<b<<" "<<c<<" "<<c<<endl; //如果采用bbc,有可能b+b<c。
}
}
D - Kana and Dragon Quest game
Kana was just an ordinary high school girl before a talent scout discovered her. Then, she became an idol. But different from the stereotype, she is also a gameholic. One day Kana gets interested in a new adventure game called Dragon Quest. In this game, her quest is to beat a dragon. The dragon has a hit point of xx
initially. When its hit point goes to 00or under 00, it will be defeated. In order to defeat the dragon, Kana can cast the two following types of spells. Void Absorption Assume that the dragon’s current hit point is hh, after casting this spell its hit point will become ⌊h2⌋+10⌊h2⌋+10. Here ⌊h2⌋⌊h2⌋denotes hh divided by two, rounded down. Lightning Strike This spell will decrease the dragon’s hit point by 1010
. Assume that the dragon’s current hit point is hh
, after casting this spell its hit point will be lowered to h−10h−10
.Due to some reasons Kana can only cast no more than nnVoid Absorptions and mm
Lightning Strikes. She can cast the spells in any order and doesn’t have to cast all the spells. Kana isn’t good at math, so you are going to help her to find out whether it is possible to defeat the dragon.
Input
The first line contains a single integer tt
(1≤t≤10001≤t≤1000) — the number of test cases.The next tt
lines describe test cases. For each test case the only line contains three integers xx
, nn, mm(1≤x≤1051≤x≤105, 0≤n,m≤300≤n,m≤30) — the dragon’s intitial hit point, the maximum number of Void Absorptions and Lightning Strikes Kana can cast respectively.
Output
If it is possible to defeat the dragon, print “YES” (without quotes). Otherwise, print “NO” (without quotes).You can print each letter in any case (upper or lower).
Example
Input
7
100 3 4
189 3 4
64 2 3
63 2 3
30 27 7
10 9 1
69117 21 2
Output
YES
NO
NO
YES
YES
YES
YES
题意
巨龙血量h,有两个技能,一种巨龙血量变为h/2+10可用n次,另一种h-10可用m次。
思路
考虑两种情况一种h足够小第二种技能足够杀死,否则考虑先用一技能消耗最后判断二技能能否终结。
代码
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--){
int x,n,m;
cin>>x>>n>>m;
if(x<=m*10)cout<<"YES"<<endl; //判断是否二技能直接击败巨龙。
else{
while(n--){
x=x/2+10;
}
if(x>m*10)cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}}
}
E - Candies and Two Sisters
There are two sisters Alice and Betty. You have nn
candies. You want to distribute these nn
candies between two sisters in such a way that: Alice will get aa
(a>0a>0
) candies; Betty will get bb
(b>0b>0
) candies; each sister will get some integer number of candies; Alice will get a greater amount of candies than Betty (i.e. a>ba>b
); all the candies will be given to one of two sisters (i.e. a+b=na+b=n
). Your task is to calculate the number of ways to distribute exactly nn
candies between sisters in a way described above. Candies are indistinguishable.Formally, find the number of ways to represent nn
as the sum of n=a+bn=a+b
, where aa
and bb
are positive integers and a>ba>b
.You have to answer tt
independent test cases.
Input
The first line of the input contains one integer tt
(1≤t≤1041≤t≤104
) — the number of test cases. Then tt
test cases follow.The only line of a test case contains one integer nn
(1≤n≤2⋅1091≤n≤2⋅109
) — the number of candies you have.
Output
For each test case, print the answer — the number of ways to distribute exactly nn
candies between two sisters in a way described in the problem statement. If there is no way to satisfy all the conditions, print 00.
Example
Input
6
7
1
2
3
2000000000
763243547
Output
3
0
0
1
999999999
381621773
题意
t个样例,两姐妹分糖果,a>b,a+b=n.a!=0,b!=0。总共多少种分法。
思路
如果n是1,2无法分输出0,除此之外之需要考虑平分妹妹的糖果最多有多少。就有几种分法。
代码
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--){
int n;
cin>>n;
if(n==1||n==2)cout<<"0"<<)ndl; //1,2特例。
else if(n%2!=0)cout<<n/2<<endl; //n为奇数直接除二得出结果(小数保留整数)
else cout<<n/2-1<<endl; //偶数平分除去相等的情况。
}
}
F - Construct the String
You are given three positive integers nn
, aa
and bb
. You have to construct a string ss
of length nn
consisting of lowercase Latin letters such that each substring of length aa
has exactly bb
distinct letters. It is guaranteed that the answer exists.You have to answer tt
independent test cases.Recall that the substring s[l…r]s[l…r]
is the string sl,sl+1,…,srsl,sl+1,…,sr
and its length is r−l+1r−l+1
. In this problem you are only interested in substrings of length aa.
Input
The first line of the input contains one integer tt
(1≤t≤20001≤t≤2000
) — the number of test cases. Then tt
test cases follow.The only line of a test case contains three space-separated integers nn
, aa
and bb
(1≤a≤n≤2000,1≤b≤min(26,a)1≤a≤n≤2000,1≤b≤min(26,a)
), where nn
is the length of the required string, aa
is the length of a substring and bb
is the required number of distinct letters in each substring of length aa
.It is guaranteed that the sum of nn
over all test cases does not exceed 20002000
(∑n≤2000∑n≤2000
).
Output
For each test case, print the answer — such a string ss
of length nn
consisting of lowercase Latin letters that each substring of length aa
has exactly bb
distinct letters. If there are multiple valid answers, print any of them. It is guaranteed that the answer exists.
Example
Input
4
7 5 3
6 1 1
6 6 1
5 2 2
Output
tleelte
qwerty
vvvvvv
abcde
题意
t个样例,字符串长度为n,取连续a个字符,字符种类有b个。
思路
创建循环为b长度的子字符串连续输出n长度。
代码
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
int t;
cin>>t;
while(t--){
int n,a,b,i;
char sim='a';
cin>>n>>a>>b;
char model[b+1],s[n+1];
for(i=1;i<=b;i++){
model[i]=sim; //循环子字符串。
sim++;
}
for(i=1;i<=n;i++){
if(i%b==0){
s[i]=model[b]; //取余b为0等于b项。
}
else{
s[i]=model[i%b];
}
}
for(i=1;i<=n;i++){
cout<<s[i];
}
cout<<endl;
}
}