算法基础训练营2
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-qqeyiHha-1628349890332)(C:\Users\86198\AppData\Roaming\Typora\typora-user-images\image-20210205071956366.png)]
H牛牛与棋盘
part 1 双重for
就两重for循环打印矩阵就好了,这个黑白格子的划分可以用位运算&1,或者模2这样比较取巧的方式实现。
代码
int n; char a[2][1010]; void work() { cin >> n; for (int i = 0; i < n; i ++ ) if (i & 1) a[0][i] = '1'; else a[0][i] = '0'; for (int i = 0; i < n; i ++ ) if (i & 1) a[1][i] = '0'; else a[1][i] = '1'; for (int i = 0; i < n; i ++ ) cout << a[i&1] << endl; }
牛牛与整除分块
先打个表
int a[N];
void work() {
for (int i = 1; i <= 50; i ++ ) {
cout << "n = " << i << "------";
for (int j = 1; j <= i; j ++ ){
//cout << i / j << ' ';
a[j] = i / j;
}
//cout << endl;
sort(a + 1, a + 1 + i);
int t = unique(a + 1, a + 1 + i) - (a + 1);
for (int j = 1; j <= t; j ++ )
cout << a[j] << ' ';
cout << endl;
}
/*
n = 1------1
n = 2------1 2
n = 3------1 3
n = 4------1 2 4
n = 5------1 2 5
n = 6------1 2 3 6
n = 7------1 2 3 7
n = 8------1 2 4 8
n = 9------1 2 3 4 9
n = 10------1 2 3 5 10
n = 11------1 2 3 5 11
n = 12------1 2 3 4 6 12
n = 13------1 2 3 4 6 13
n = 14------1 2 3 4 7 14
n = 15------1 2 3 5 7 15
n = 16------1 2 3 4 5 8 16
n = 17------1 2 3 4 5 8 17
n = 18------1 2 3 4 6 9 18
n = 19------1 2 3 4 6 9 19
n = 20------1 2 3 4 5 6 10 20
n = 21------1 2 3 4 5 7 10 21
n = 22------1 2 3 4 5 7 11 22
n = 23------1 2 3 4 5 7 11 23
n = 24------1 2 3 4 6 8 12 24
n = 25------1 2 3 4 5 6 8 12 25
n = 26------1 2 3 4 5 6 8 13 26
n = 27------1 2 3 4 5 6 9 13 27
n = 28------1 2 3 4 5 7 9 14 28
n = 29------1 2 3 4 5 7 9 14 29
n = 30------1 2 3 4 5 6 7 10 15 30
n = 31------1 2 3 4 5 6 7 10 15 31
n = 32------1 2 3 4 5 6 8 10 16 32
n = 33------1 2 3 4 5 6 8 11 16 33
n = 34------1 2 3 4 5 6 8 11 17 34
n = 35------1 2 3 4 5 7 8 11 17 35
n = 36------1 2 3 4 5 6 7 9 12 18 36
n = 37------1 2 3 4 5 6 7 9 12 18 37
n = 38------1 2 3 4 5 6 7 9 12 19 38
n = 39------1 2 3 4 5 6 7 9 13 19 39
n = 40------1 2 3 4 5 6 8 10 13 20 40
n = 41------1 2 3 4 5 6 8 10 13 20 41
n = 42------1 2 3 4 5 6 7 8 10 14 21 42
n = 43------1 2 3 4 5 6 7 8 10 14 21 43
n = 44------1 2 3 4 5 6 7 8 11 14 22 44
n = 45------1 2 3 4 5 6 7 9 11 15 22 45
n = 46------1 2 3 4 5 6 7 9 11 15 23 46
n = 47------1 2 3 4 5 6 7 9 11 15 23 47
n = 48------1 2 3 4 5 6 8 9 12 16 24 48
n = 49------1 2 3 4 5 6 7 8 9 12 16 24 49
n = 50------1 2 3 4 5 6 7 8 10 12 16 25 50
*/
用笔枚举,或者打表找规律。
我们发现,规律是以
t = sqr(n)
轴对称的。当x小于或等于t的时候,他是第几小,就是x本身
当x大于t时,答案为S集合的长度减去n / x n + 1
集合的长度,注意分为t * 2 和t * 2 - 1两种情况
代码
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; int n; int main() { int T; cin >> T; while (T -- ) { int n, x; scanf("%d%d", &n, &x); int t = sqrt(n); int m; if (n - t * t >= t) m = 2 * t; else m = 2 * t - 1; if (x <= t) printf("%d\n", x); else printf("%d\n", m - n / x + 1); } return 0; }
C牛牛与字符串border
[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-VadBPUmh-1628349890335)(C:\Users\86198\AppData\Roaming\Typora\typora-user-images\image-20210205091512166.png)]
代码
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> using namespace std; int main() { int T; cin >> T; while (T -- ) { int n, l; cin >> n >> l; string s; cin >> s; //确定循环节的周期 int t = __gcd(n, l); if (n < 2 * l && n != l) t = n - l; //确定循环节的元素 string str = ""; for (int i = 0; i < t; i ++ ) { int tong[26] = {0}; for (int j = i; j < n; j += t ) tong[s[j] - 'a'] ++; int mx = 0, id = 0; for (int j = 0; j < 26; j ++ ) if (tong[j] > mx) mx = tong[j], id = j; str += id + 'a'; } for (int i = 0; i < n / t; i ++ ) cout << str; for (int i = 0; i < n % t; i ++ ) cout << str[i]; cout << endl; } return 0; }