ll d(int x){ if (f[x]) return f[x]; if (x == 1 || x == 0) return1; return f[x] = x * d(x - 1) % 97; }
intmain(){ string s; getline(cin, s); istringstream ss(s); int x; bool f = false; while (ss >> x) { if (!f) { cout << d(x); f = true; } else cout << " " << d(x); } return0; }
当然也可以直接先初始化一下也行。
B.选择排序
模板
#include<bits/stdc++.h> usingnamespace std; typedeflonglong ll; constint N = 210; ll a[N]; int n, m; intmain(){ string s; getline(cin, s); istringstream ss(s); while (ss >> a[n]) n ++; scanf("%d", &m); // m次循环 for (int i = 0; i < m; i ++ ) { int m = i; // 每次循环找到最大值,并和这次循环的第一个元素进行交换 for (int j = i + 1; j < n; j ++ ) if (a[m] > a[j]) m = j; if (m != i) swap(a[i], a[m]); } // 打印结果 for (int i = 0; i < n; i ++ ) { if (i) cout << " " << a[i]; else cout << a[i]; } return0; }
ll mul(ll a, ll b){ ll res = 1; while (b) { if (b & 1) res = (res * a) % c; a = (a * a) % c; b >>= 1; } return res; }
intmain(){ cin >> a >> b >> c; a = a % c; cout << mul(a, b); return0; }
E.飞机上升或者下降
两种思路,一种是递归,另外一种是动态规划(就是递归+记忆化搜索)
递归:
#include<bits/stdc++.h> #define int long long usingnamespace std; int ans; //int f[N][N][N]; voiddfs(int a, int b, int h){ // 不能在上升,并且还可以下降一次,并且当前高度是1,则满足题意 if (h == 1 && a == 0 && b == 1) { ans ++; return; } if (h < 0) return; // 如果当前高度已经是负数了, if (h > b) return; // 如果当前高度就算一直下降,也不能降到0 // cout << a << " - " << b << " - "<< h << endl; // 如果一直上升,最后下降还剩余一些下降的次数,那么就不满足 // 要么上升,要么下降 if (a > 0) dfs(a - 1, b, h * 2); if (b > 0) dfs(a, b - 1, h - 1); }
signedmain(){ int a, b, h; cin >> a >> b >> h; dfs(a, b, h); cout << ans << endl; return0; }
动态规划:
#include<bits/stdc++.h> usingnamespace std; constint N = 100; int f[N][N][N]; // 检查当前如果一直上升的话 // 如果高度还是小于可以下降的次数 // 那么一定不行 boolcheck(int a, int b, int c){ int res = c; for (int i = 0; i < a; i ++ ) res *= 2; return res >= b; }
intmain(){ int a, b, h; cin >> a >> b >> h; int max_k = h; for (int i = 0; i <= a; i ++ ) { // 上升 次数 for (int j = 1; j <= b; j ++ ) { // 下降 次数 for (int k = 1; k <= j; k ++ ) { // 高度, 当前高度不能超过剩余的下降次数 // 如果当前只能下降则答案为 1 if (i == 0 && j == k) f[i][j][k] = 1; // 当前状态一共有两个操作,一个是上升,一个是下降, // 先看下降,如果check下降的操作没有问题,并且当前不只是只能下降 // 那么下降的操作就是合法的,下降的后的状态应该+=当前状态的答案 if (check(i, j + 1, k + 1) && !(i == 0 && j == k)) f[i][j + 1][k + 1] += f[i][j][k]; // 如果当前k != 1(k/2!=0) // 并且上升之前的状态合法 // 并且当前是由 某一个状态上升过来的(n*2 一定是偶数) if (k / 2 != 0 && check(i + 1, j, k / 2) && k % 2 == 0) f[i + 1][j][k / 2] += f[i][j][k]; } } } cout << f[a][b][h] << endl; return0; }
#include<bits/stdc++.h> usingnamespace std; typedeflonglong ll; ll n; ll a[15]; intc(ll n){ int res = 0; while (n) { res ++; n /= 10; } return res; }
ll f(ll n){ if (n < 0) n = -n; int len = c(n); // 长度 if (n <= 0) return0; if (len == 1) return n >= 2; int l = (n / pow(10, len - 1)); // 最高位 ll res = a[len - 1] * l; ll r = n - l * pow(10, len - 1); // 去掉最高位 if (l == 2) res += r + 1; elseif (l != 1) res += pow(10, len - 1); return res + f(r); }
intmain(){ for (int i = 0; i <= 15; i ++ ) a[i] = i * pow(10, i - 1); string s; getline(cin, s); istringstream ss(s); bool f1 = false; while (ss >> n) if (f1) cout << " " << f(n); else { cout << f(n); f1 = true; } return0; }
#include<bits/stdc++.h> usingnamespace std; //typedef long long ll; //const int N = 1e5 + 10; int n, t, d[30], avg; bool vis[30]; bool f = false; voiddfs(int a, int b, int c, int sum){ if (f) return; if (sum == n && (!a || !b || !c)) return; if (sum == n - 1) { // 如果是最后一次了,并且 有两个为0 if ((a && !b && !c) || (!a && b && !c) || (!a && !b && c)) return; } if (a == b && b == c && a != 0 && sum == n) { f = true; return; } for (int i = 0; i < n; i ++ ) { if (!vis[i]) { if (3 * (a + d[i]) <= avg) { vis[i] = true; dfs(a + d[i], b, c, sum + 1); vis[i] = false; } if (3 * (b + d[i]) <= avg) { vis[i] = true; dfs(a, b + d[i], c, sum + 1); vis[i] = false; } if (3 * (c + d[i]) <= avg) { vis[i] = true; dfs(a, b, c + d[i], sum + 1); vis[i] = false; } } } } intmain(){ scanf("%d", &t); while (t -- ) { f = false; avg = 0; scanf("%d", &n); memset(vis, 0, sizeof vis); for (int i = 0; i < n; i ++ ) { scanf("%d", d + i); avg += d[i]; } sort(d, d + n, greater<int>()); dfs(0, 0, 0, 0); if (f) puts("yes"); elseputs("no"); } return0; }