@Dmaxiya
2018-08-17T02:18:18.000000Z
字数 3483
阅读 1253
Codeforces
Contests 链接:Codeforces Round #463 (Div. 1 + Div. 2)
过题数:2
排名:2093/8251
给定一个字符串 ,构造一个字符串 ,使得字符串 是回文串,且 是 的子序列。
输入只包含一个字符串 。
输出字符串 ,字符串 的长度不必是最短的,只需要在 以内即可。
| 输入 | 输出 | 提示 |
|---|---|---|
| aba | aba | "aba" 是 "aba" 的子序列,且 "aba" 是一个回文串。 |
| ab | aabaa | "ab" 是 "aabaa" 的子序列,且 "aabaa" 是一个回文串。 |
正着输出一遍 再倒着输出一遍 ,就是答案,因为 ,所以答案最多只有 个字符。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <bitset>#include <algorithm>using namespace std;#define LL long longconst int maxn = 1000 + 100;int len;char str[maxn];int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyawhile(scanf("%s", str) != EOF) {len = strlen(str);printf("%s", str);for(int i = len - 1; i >= 0; --i) {printf("%c", str[i]);}printf("\n");}return 0;}
定义函数 ,表示数字 的所有数位上非零数值的乘积,定义函数 如下:
有 次询问,每次询问区间 内, 的 的个数。
第一行为一个整数 ,表示询问的次数,接下去 行,每行三个整数 。
对于每次询问输出所求答案。
| 输入 | 输出 | 提示 |
|---|---|---|
| 4 22 73 9 45 64 6 47 55 7 2 62 4 |
1 4 0 8 |
1.; 2.; 3.在 到 之间没有 的数字; 4. |
| 4 82 94 6 56 67 4 28 59 9 39 74 4 |
3 1 1 5 |
预处理出 内所有的 ,对 的个数分别做前缀和,就可以 地查询。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <bitset>#include <algorithm>using namespace std;#define LL long longconst int maxn = 1000000 + 100;int T, l, r, k;int g[maxn];int sum[maxn][10];int f(int x) {int ret = 1;while(x != 0) {if(x % 10 != 0) {ret *= x % 10;}x /= 10;}return ret;}int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyafor(int i = 1; i < maxn; ++i) {if(i < 10) {g[i] = i;} else {g[i] = g[f(i)];}for(int j = 1; j <= 9; ++j) {sum[i][j] = sum[i - 1][j];}++sum[i][g[i]];}while(scanf("%d", &T) != EOF) {while(T--) {scanf("%d%d%d", &l, &r, &k);printf("%d\n", sum[r][k] - sum[l - 1][k]);}}return 0;}
对于一个排列 ,定义函数 为:
定义函数 为使 的最小的 的值,对于给定的整数 ,构造一种排列,使得 。
输入只包含三个整数 。
输出 的一种满足题意的排列。
| 输入 | 输出 | 提示 |
|---|---|---|
| 9 2 5 | 6 5 8 3 4 1 9 2 7 | 且 。 |
| 3 2 1 | 1 2 3 | 。 |
将每一个数字 与其对应的下标连一条边,在全排列中就是一个个环,每一个 都属于其中一个环,可以发现 就是第 个数字所在环的节点数量,因此就是要构造一个序列,使这个序列中环的数量要么是 要么是 ,所以相当于是要解一个方程,使得 ,且 ,其中 表示环上节点个数等于 的环的数量, 表示环上节点个数等于 的环的数量,由于数据范围比较小,可以暴力跑出 的值,最后构造出这些环即可。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <bitset>#include <algorithm>using namespace std;#define LL long longconst int maxn = 1000000 + 100;int n, a, b, cnta, cntb;int num[maxn], ans[maxn];void Move(int Index, int cnt, int x) {for(int i = 0; i < cnt; ++i) {ans[Index] = num[Index + x - 1];for(int j = 1; j < x; ++j) {ans[Index + j] = num[Index + j - 1];}Index += x;}}int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyawhile(scanf("%d%d%d", &n, &a, &b) != EOF) {cnta = cntb = -1;for(int i = 0; i <= n; i += a) {if((n - i) % b == 0) {cnta = i / a;cntb = (n - i) / b;}}if(cnta == -1) {printf("-1\n");continue;}for(int i = 1; i <= n; ++i) {num[i] = i;}Move(1, cnta, a);Move(1 + cnta * a, cntb, b);for(int i = 1; i <= n; ++i) {if(i != 1) {printf(" ");}printf("%d", ans[i]);}printf("\n");}return 0;}