【Day1】一名菜鸟ACMer的暑假训练
Day1
板刷CF构造题
A. Nastia and Nearly Good Numbers
https://codeforces.com/problemset/problem/1521/A
构造\(z=abt_3,y=at_2,x=at_1\),根据等式\(z=x+y\),可以约去a
故可以构造 1 3b-1 3b (系数)
注意特判b==1,全部都是good,无法构造
#include<bits/stdc++.h>
using namespace std;
void solve () {
long long a, b;
cin >> a >> b;
if (b == 1) {
cout << "NO\n";
return ;
}
cout << "YES\n";
cout << a << ' ' << a * (3*b-1) << ' ' << a*3*b << endl;
}
int main () {
int t;
cin >> t;
while (t --) {
solve ();
}
}
//x+y=z
//一个%ab==0,另两个%a==0
//t1+t2==bt3
//1 b-1 b
A. K-divisible Sum
https://codeforces.com/problemset/problem/1476/A
懂抽屉原理的宝宝( ̄︶ ̄*))就可以秒解啦
小心格式的输出
#include <bits/stdc++.h>
using namespace std;
void solve () {
int n, k;
cin >> n >> k;
if (n == 1) cout << k << endl;
else if (n == k) cout << 1 << endl;
else if (n < k) cout << (int)ceil (1.0*k/n) << endl;
else {
double dx = n - k;
//cout << dx << ' ';
k = ceil(dx/k)*k+k;
//cout << k << ' ';
cout << (int)ceil (1.0*k/n) << endl;
}
}
int main () {
int t;
cin >> t;
while (t --) {
solve();
}
}
//抽屉原理
//啊啊啊气死我了!!输出要注意格式int
C. Not Adjacent Matrix
https://codeforces.com/problemset/problem/1520/C
灵光乍现,根据对角线来填充,这样子就不会有相邻的在上下左右了。
注意怎么构造:找规律+普适性规律
#include <bits/stdc++.h>
using namespace std;
const int N = 105;
int a[N][N];
void print (int n) {
for (int i = 1; i <= n; i ++) {
for (int j = 1; j <= n; j ++)
cout << a[i][j] << ' ';
cout << endl;
}
}
void solve () {
int n;
cin >> n;
if (n == 1) cout << "1\n";
else if (n == 2) cout << "-1\n";
else {
a[1][1] = 1;
//处理第一行, 增量为 1*n, 2*(n-i+2)...
a[1][2] = a[1][1] + n;
for (int i = 3; i <= n; i ++) a[1][i] = a[1][i-1] + 2*(n-i+2);
//处理第一列 增量为 ...9,7,5,3 通项:2*(n-i)+3
for (int i = 2; i <= n; i ++) a[i][1] = a[i-1][1] + 2*(n-i)+3;
//然后就可以根据对角线求啦
for (int i = 2; i <= n; i ++)
for (int j = 2; j <= n; j ++)
a[i][j] = a[i-1][j-1] + 1;
print (n);
}
}
int main () {
int t;
cin >> t;
while (t --) {
solve ();
}
}
//上下左右不得相邻
//按照对角线填
//从主对角线开始填,然后一上一下挨个填
B. Same Parity Summands
https://codeforces.com/problemset/problem/1352/B
奇偶性相同的k个数和为n
构造:
1 1 1 1...n-k+1
2 2 2 2...n-2*(k-1)
//构造没有问题,但是要记得判断末项是否大于0
#include <bits/stdc++.h>
using namespace std;
void solve () {
int n, k;
cin >> n >> k;
if (n % k == 0) {
cout << "YES" << endl;
for (int i = 0; i < k; i ++)
cout << n / k << ' ';
cout << endl;
}
else {
int res1 = n-k+1, res2 = n-2*(k-1);
if (res1 > 0 && res1 & 1) {
cout << "YES\n";
for (int i = 0; i < k - 1; i ++) cout << "1 ";
cout << n-k+1 << endl;
return ;
}
if (res2 > 0 && res2 % 2 == 0) {
cout << "YES\n";
for (int i = 0; i < k - 1; i ++) cout << "2 ";
cout << n - 2 * (k-1) << endl;
return ;
}
cout << "NO\n";
}
}
int main () {
int t;
cin >> t;
while (t --) {
solve ();
}
}
//奇偶性相同的k个数和为n
//构造:
// 1 1 1 1...n-k+1
// 2 2 2 2...n-2*(k-1)
//构造没有问题,但是要记得判断末项是否大于0