A - Candies
1.题意
给定一个正整数n,求一个正整数x使得x + 2 * x + 4 * x + ······ + 2 ^ (k - 1) * x = n(k > 1)。
2.题解
用等比数列的求和公式化简为 (2 ^ k - 1)* x = n,乘方可以用快速幂,再从2开始枚举k即可。
3.代码
#include<bits/stdc++.h> #define ll long long using namespace std; ll qpow(ll a, ll n){ ll ans = 1; while(n) { if(n & 1) ans *= a; a *= a; n >>= 1; } return ans; } int main() { int t; cin >> t; while(t--) { ll n; cin >> n; for(int i = 2; ; i++) { int qp = (qpow(2, i) - 1); if(n % qp == 0) { cout <<n / qp << endl; break; } } } return 0; }
B - Balanced Array
1.题意
给定一个偶数n,要求构造一个长度为n的数列满足:前n / 2的数字之和等于后n / 2的数字之和,且前n / 2的数字全为偶数,后n / 2的数字全为奇数。
2.题解
举例发现,当n % 4 = 2时,无解。因为n是偶数,n % 4 只能等于0或2,当等于2时,说明n / 2是奇数,前n / 2个偶数之和是偶数,后n / 2个奇数之和为奇数,显然不满足条件。当n % 4 = 0时,n / 2是偶数,让数列前半部分每个数都比后半部分对应的每个数大1,最后一个数补回来即可。
3.代码
#include<bits/stdc++.h> #define ll long long using namespace std; int t; int main() { int t; cin >> t; while(t--) { int n; cin >> n; if(n % 4 == 2) { cout << "NO" <<endl; } else { cout << "YES" <<endl; for(int i = 1; i <= n / 2; i++) { if(i == 1) { cout << 2; } else { cout << ' ' << 2 * i; } } for(int i = 1; i < n / 2; i++) { cout << ' ' << (2 * i - 1); } int fin = 2 * (n / 2) + (n / 2 - 1); cout << ' ' << fin << endl; } } return 0; }
C - Ichihime and Triangle
1.题意
给定a,b,c,d四个正整数,满足a <= b <= c <= d,要求挑出x,y,z三个数(a <= x <= b,b <= y <= c,c <= z <= d)作为边长组成一个三角形。
2.题解
三角形满足任意两边之和大于第三边,所以让x = b,y = c,z = c即可。
3.代码
#include<bits/stdc++.h> #define ll long long using namespace std; int t; int main() { int t; cin >> t; while(t--) { ll a, b, c, d; cin >> a >> b >> c >> d; cout<< b << ' ' << c << ' ' << c << endl; } return 0; }
D - Kana and Dragon Quest game
1.题意
有一条血量为x的龙需要被消灭(血量降为0或以下),有两个技能A和B,设龙当前血量为hp,技能A能使得龙的血量变为hp / 2 + 10,技能B使得龙的血量变为hp - 10,技能A最多使用n次,技能B最多使用m次,问是否能消灭掉龙。
2.题解
只要当前血量不小于20,就优先用技能A(因为当hp = 20时,用技能A后hp不变,如果hp小于20,用技能A会使血量增加),之后用B技能,判断最后血量是否小于0。
3.代码
#include<bits/stdc++.h> #define ll long long using namespace std; int t; int main() { cin >> t; while(t--) { int hp, n, m; cin >> hp >> n >> m; for(int i = 1; i <= n; i++) { if(hp <= 20) { break; } hp = hp / 2 + 10; } if(hp > 10 * m) { cout << "NO" <<endl; } else { cout << "YES" <<endl; } } return 0; }
E - Candies and Two Sisters
1.题意
有n颗糖要分给姐妹两人,要求n颗糖必须分完,每人至少分得一颗且分给姐姐的糖要多于妹妹,求分糖方案数。
2.题解
如果n <= 2,则无法分糖,如果n是偶数,方案数为n / 2 - 1,如果n是奇数,方案数为n / 2。
3.代码
#include<bits/stdc++.h> #define ll long long using namespace std; int t; int main() { cin >> t; while(t--) { ll n; cin >> n; if(n < 3) { cout << 0 << endl; } else if(n % 2) { cout << n / 2 << endl; } else{ cout << n / 2 - 1 << endl; } } return 0; }
F - Construct the String
1.题意
给定三个正整数n,a,b,要求构造一个由小写字母构成长度为n的字符串,满足每个长度为a的子串含有b个不同的字母。
2.题解
要满足每a个字母中都含有b个不同的字母,只需将b个不同字母循环至字符串长度为n即可,与a无关。
3.代码
#include<bits/stdc++.h> #define ll long long using namespace std; int t; int main() { cin >> t; while(t--) { int n, a, b; cin >> n >> a >> b; if(b == 1) { for(int i = 0; i < n; i++) { cout << 'a'; } cout << endl; } else { for(int i = 0; i < n; i++) { printf("%c", i % b + 97); } cout << endl; } } return 0; }