@Dmaxiya
2018-08-17T02:20:33.000000Z
字数 3790
阅读 1318
Codeforces
Contests 链接:Codeforces Round #447 (Div. 2)
过题数:2
排名:995/11398
给定一个字符串 ,询问这个字符串中有多少个字符子序列是 "QAQ"。
输入只包含一个字符串 ,且字符串中只含有大写字母。
输出 "QAQ" 子序列的个数。
| 输入 | 输出 | 提示 |
|---|---|---|
| QAQAQYSYIOIWIN | 4 | 四个子 "QAQ" 串分别为: |
| QAQQQZZYNOIWIN | 4 |
对于每一个字符 'A',能够形成的 "QAQ" 子序列的个数,等于 'A' 左边的 'Q' 字符的数量乘上右边的 'Q' 字符的数量,'Q' 字符的数量可以预处理。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cfloat>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <algorithm>using namespace std;#define LL long longconst int maxn = 100 + 100;int ans;char str[maxn];int Left[maxn], Right[maxn];int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);while(scanf("%s", str + 1) != EOF) {ans = 0;int len = strlen(str + 1);Left[0] = Right[len + 1] = 0;for(int i = 1; i <= len; ++i) {Left[i] = Left[i - 1];if(str[i] == 'Q') {++Left[i];}}for(int i = len; i >= 1; --i) {Right[i] = Right[i + 1];if(str[i] == 'Q') {++Right[i];}}for(int i = 1; i <= len; ++i) {if(str[i] == 'A') {ans += Left[i] * Right[i];}}printf("%d\n", ans);}return 0;}
在一个 的网格中填上整数,要求每一行和每一列的整数的乘积都等于 ,问有多少种填数的方案。
输入只包含三个整数 。
输出总的方案数对 取模后的结果。
| 输入 | 输出 | 提示 |
|---|---|---|
| 1 1 -1 | 1 | 只能将 填入唯一的格子中。 |
| 1 3 1 | 1 | 三格都只能填 。 |
| 3 3 -1 | 16 |
由 只等于 和 可以知道,填入的整数只能是 或者 ,其次,不论前面的填入的乘积是多少,最后一行或者一列的数字总能填上一个 或者 来得到 ,因此 行 列的方格可以填 和 的任意一种组合方式,总的方案数为 ,考虑最后一个方格 填入的数字的情况,这个位置填入的数字由第 行的前 个数字和第 列的前 行数字同时决定,当 时没有任何问题,当 ,必须保证 ,否则这个位置不论填什么数字都无法满足条件。最后由于 和 都很大,如果直接相乘会爆
long long,所以要用个欧拉降幂公式 。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cfloat>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <algorithm>using namespace std;#define LL long longconst LL MOD = 1000000007;const LL phi = MOD - 1;LL n, m, k, p;LL fast_pow(LL res, LL n) {LL ans;for(ans = 1; n != 0; n >>= 1) {if((n & 1) == 1) {ans = (ans * res) % MOD;}res = (res * res) % MOD;}return ans;}int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);while(scanf("%I64d%I64d%I64d", &n, &m, &k) != EOF) {if(k == -1 && n % 2 != m % 2) {printf("0\n");continue;}if(n >= phi || m >= phi || n * m >= phi) {p = ((n - 1) % phi) * ((m - 1) % phi) % phi + phi;} else {p = (n - 1) * (m - 1);}printf("%I64d\n", fast_pow(2, p));}return 0;}
对于任意一个长度为 的整数序列,将这个序列的任意一个区间 放入一个集合 中,在这个集合中,如果有两个相同的区间 的值,在集合中也只保留一个,现在给定集合 ,要求构造出一个满足条件的序列。
第一行为一个整数 ,表示集合的大小,第二行为 个整数 ,给出的数字一定是按升序排列的。
如果可以构造出这样的序列,就输出这个序列,第一行为一个整数 ,第二行为 个整数 ,如果有多解输出任意一个,否则输出 。数据保证如果存在解,解的序列个数一定有不超过 的,且 一定可以不超过 .
| 输入 | 输出 |
|---|---|
| 4 2 4 6 12 |
3 4 6 12 |
| 2 2 3 |
-1 |
首先判断给出的所有数字的最大公约数是否等于最小的数字,如果不等于,则输出 ,否则将原序列的所有数字输出,并且隔一个插入一个最小的数字,就能满足任意一个长度大于 的区间的 都等于最小的数字。
#include <iostream>#include <cstdio>#include <cstdlib>#include <cmath>#include <climits>#include <cfloat>#include <cstring>#include <string>#include <vector>#include <list>#include <queue>#include <stack>#include <map>#include <set>#include <algorithm>using namespace std;#define LL long longconst int maxn = 1000000 + 100;int n, cnt;int ans[maxn], num[maxn];int gcd(int x, int y) {return (y == 0? x: gcd(y, x % y));}int main() {#ifdef Dmaxiyafreopen("test.txt", "r", stdin);#endif // Dmaxiyaios::sync_with_stdio(false);while(scanf("%d", &n) != EOF) {cnt = 0;for(int i = 1; i <= n; ++i) {scanf("%d", &num[i]);}int Gcd = num[1];for(int i = 2; i <= n; ++i) {Gcd = gcd(Gcd, num[i]);}if(Gcd != num[1]) {printf("-1\n");continue;}for(int i = 1; i <= n; ++i) {ans[cnt++] = num[i];ans[cnt++] = num[1];}printf("%d\n", cnt);for(int i = 0; i < cnt; ++i) {if(i != 0) {printf(" ");}printf("%d", ans[i]);}printf("\n");}return 0;}