A. Opponents
求最长连续不全为0的字符串所组成的子串长度。随便搞搞就行了。
#include <set>
#include <map>
#include <cmath>
#include <stack>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
#define FIN freopen("in.txt", "r", stdin);
#define FOUT freopen("out.txt", "w", stdout);
int n, d, isWin[105];
string t;
int main()
{
while (~scanf("%d%d", &n, &d))
{
t = "";
for (int i = 0; i < n; i++)
t += "1";
string in;
for (int i = 0; i < d; i++)
{
cin >> in;
if (in == t)
isWin[i] = 1;
else
isWin[i] = 0;
}
int ans = 0, len = 0;
for (int i = 0; i < d; i++)
{
if (isWin[i] == 1)
len = 0;
else if (i == 0 || isWin[i - 1] == 1)
len = 1;
else
len++;
ans = max(ans, len);
}
printf("%d\n", ans);
}
return 0;
}
B. Lovely Palindromes
求第n小长度为偶数、仅由数字构成的回文串。
把n和翻转后的n拼接后刚好满足题意。
#include <set>
#include <map>
#include <cmath>
#include <stack>
#include <queue>
#include <cstdio>
#include <vector>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long LL;
#define FIN freopen("in.txt", "r", stdin);
#define FOUT freopen("out.txt", "w", stdout);
string t;
int main()
{
while (cin >> t)
{
int len = t.size();
for (int i = len - 1; i >= 0; i--)
t += t[i];
cout << t << endl;
}
return 0;
}
C. NP-Hard Problem
二分图染色。
/*
::
:;J7, :, ::;7:
,ivYi, , ;LLLFS:
:iv7Yi :7ri;j5PL
,:ivYLvr ,ivrrirrY2X,
:;r@Wwz.7r: :ivu@kexianli.
:iL7::,:::iiirii:ii;::::,,irvF7rvvLujL7ur
ri::,:,::i:iiiiiii:i:irrv177JX7rYXqZEkvv17
;i:, , ::::iirrririi:i:::iiir2XXvii;L8OGJr71i
:,, ,,: ,::ir@mingyi.irii:i:::j1jri7ZBOS7ivv,
,::, ::rv77iiiriii:iii:i::,rvLq@huhao.Li
,, ,, ,:ir7ir::,:::i;ir:::i:i::rSGGYri712:
::: ,v7r:: ::rrv77:, ,, ,:i7rrii:::::, ir7ri7Lri
, 2OBBOi,iiir;r:: ,irriiii::,, ,iv7Luur:
,, i78MBBi,:,:::,:, :7FSL: ,iriii:::i::,,:rLqXv::
: iuMMP: :,:::,:ii;2GY7OBB0viiii:i:iii:i:::iJqL;::
, ::::i ,,,,, ::LuBBu BBBBBErii:i:i:i:i:i:i:r77ii
, : , ,,:::rruBZ1MBBqi, :,,,:::,::::::iiriri:
, ,,,,::::i: @arqiao. ,:,, ,:::ii;i7:
:, rjujLYLi ,,:::::,:::::::::,, ,:i,:,,,,,::i:iii
:: BBBBBBBBB0, ,,::: , ,:::::: , ,,,, ,,:::::::
i, , ,8BMMBBBBBBi ,,:,, ,,, , , , , , :,::ii::i::
: iZMOMOMBBM2::::::::::,,,, ,,,,,,:,,,::::i:irr:i:::,
i ,,:;u0MBMOG1L:::i:::::: ,,,::, ,,, ::::::i:i:iirii:i:i:
: ,iuUuuXUkFu7i:iii:i:::, :,:,: ::::::::i:i:::::iirr7iiri::
: :rk@Yizero.i:::::, ,:ii:::::::i:::::i::,::::iirrriiiri::,
: 5BMBBBBBBSr:,::rv2kuii:::iii::,:i:,, , ,,:,:i@petermu.,
, :r50EZ8MBBBBGOBBBZP7::::i::,:::::,: :,:,::i;rrririiii::
:jujYY7LS0ujJL7r::,::i::,::::::::::::::iirirrrrrrr:ii:
,: :@kevensun.:,:,,,::::i:i:::::,,::::::iir;ii;7v77;ii;i,
,,, ,,:,::::::i:iiiii:i::::,, ::::iiiir@xingjief.r;7:i,
, , ,,,:,,::::::::iiiiiiiiii:,:,:::::::::iiir;ri7vL77rrirri::
:,, , ::::::::i:::i:::i:i::,,,,,:,::i:i:::iir;@Secbone.ii:::
*****************************************************************************
Singing my song for my dream.
*****************************************************************************
*/
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
typedef __int64 LL;
typedef pair<int,int> PII;
#define mp make_pair
#define pb push_back
#define FIN freopen("in.txt", "r", stdin);
#define FOUT freopen("out.txt", "w", stdout);
#define lson l, mid, cur << 1
#define rson mid + 1, r, cur << 1 | 1
#define lowbit(x) ((x)&(-x))
#define bitcnt(x) __builtin_popcount(x)
#define bitcntll(x) __builtin_popcountll(x)
#define debug puts("-------------");
//#pragma comment(linker, "/STACK:1024000000,1024000000")
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const double ERR = 1e-8;
const int MOD = 1e9 + 7;
const int MAXN = 1e5 + 50;
const int MAXM = 2e5 + 50;
int n, m;
bool ok;
struct Edge {
int v, nxt;
} E[MAXM];
int Head[MAXN], tot;
void edge_init() {
tot = 0;
memset(Head, -1, sizeof(Head));
}
void edge_add(int u, int v) {
E[tot].v = v;
E[tot].nxt = Head[u];
Head[u] = tot++;
}
int color[MAXN];
void dfs(int u, int c) {
color[u] = c;
for (int i = Head[u]; ~i; i = E[i].nxt) {
if (!ok)
return;
int v = E[i].v;
if (color[v] == 0)
dfs(v, -c);
else if (color[v] == -c) {}
else {
ok = false;
return;
}
}
}
int main() {
#ifdef LOCAL_NORTH
FIN;
#endif // LOCAL_NORTH
while (~scanf("%d%d", &n, &m)) {
edge_init();
memset(color, 0, sizeof(color));
for (int i = 0; i < m; i++) {
int u, v;
scanf("%d%d", &u, &v);
edge_add(u, v);
edge_add(v, u);
}
ok = true;
for (int i = 1; i <= n; i++) {
if (!color[i] && ok) {
dfs(i, 1);
}
}
if (!ok) {
printf("-1\n");
} else {
vector<int> a, b;
for (int i = 1; i <= n; i++) {
if (~color[i]) {
a.pb(i);
} else {
b.pb(i);
}
}
int asz = a.size(), bsz = b.size();
printf("%d\n", asz);
for (int i = 0; i < asz; i++)
printf("%d%c", a[i], " \n"[i == asz - 1]);
printf("%d\n", bsz);
for (int i = 0; i < bsz; i++)
printf("%d%c", b[i], " \n"[i == bsz - 1]);
}
}
#ifdef LOCAL_NORTH
cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif // LOCAL_NORTH
return 0;
}
D. Remainders Game
题目意思是:你知道x%c[1]、x%c[2]……x%c[n],问你x%k的结果是否唯一。
中国剩余定理:
设正整数两两互素,则同余方程组
有整数解。并且在模下的解是唯一的,解为…………………………………....①
…………….…②
其中,而
为
模
的逆元。(点击打开链接)…………………………………...③
由①②③可以得出,若x%c[1]、x%c[2]……x%c[n]能够提供以上信息的话,就可以唯一确定x%k。
现在把k质因子分解成为p[1]^k[1]*p[2]^k[2]*....,若对于每个i,c的集合中都有p[i]^k[i]出现的话,x%k就有唯一解。
所以就是判断lcm(c[1]*c[2]*..*c[n])%k是否等于0。
/*
::
:;J7, :, ::;7:
,ivYi, , ;LLLFS:
:iv7Yi :7ri;j5PL
,:ivYLvr ,ivrrirrY2X,
:;r@Wwz.7r: :ivu@kexianli.
:iL7::,:::iiirii:ii;::::,,irvF7rvvLujL7ur
ri::,:,::i:iiiiiii:i:irrv177JX7rYXqZEkvv17
;i:, , ::::iirrririi:i:::iiir2XXvii;L8OGJr71i
:,, ,,: ,::ir@mingyi.irii:i:::j1jri7ZBOS7ivv,
,::, ::rv77iiiriii:iii:i::,rvLq@huhao.Li
,, ,, ,:ir7ir::,:::i;ir:::i:i::rSGGYri712:
::: ,v7r:: ::rrv77:, ,, ,:i7rrii:::::, ir7ri7Lri
, 2OBBOi,iiir;r:: ,irriiii::,, ,iv7Luur:
,, i78MBBi,:,:::,:, :7FSL: ,iriii:::i::,,:rLqXv::
: iuMMP: :,:::,:ii;2GY7OBB0viiii:i:iii:i:::iJqL;::
, ::::i ,,,,, ::LuBBu BBBBBErii:i:i:i:i:i:i:r77ii
, : , ,,:::rruBZ1MBBqi, :,,,:::,::::::iiriri:
, ,,,,::::i: @arqiao. ,:,, ,:::ii;i7:
:, rjujLYLi ,,:::::,:::::::::,, ,:i,:,,,,,::i:iii
:: BBBBBBBBB0, ,,::: , ,:::::: , ,,,, ,,:::::::
i, , ,8BMMBBBBBBi ,,:,, ,,, , , , , , :,::ii::i::
: iZMOMOMBBM2::::::::::,,,, ,,,,,,:,,,::::i:irr:i:::,
i ,,:;u0MBMOG1L:::i:::::: ,,,::, ,,, ::::::i:i:iirii:i:i:
: ,iuUuuXUkFu7i:iii:i:::, :,:,: ::::::::i:i:::::iirr7iiri::
: :rk@Yizero.i:::::, ,:ii:::::::i:::::i::,::::iirrriiiri::,
: 5BMBBBBBBSr:,::rv2kuii:::iii::,:i:,, , ,,:,:i@petermu.,
, :r50EZ8MBBBBGOBBBZP7::::i::,:::::,: :,:,::i;rrririiii::
:jujYY7LS0ujJL7r::,::i::,::::::::::::::iirirrrrrrr:ii:
,: :@kevensun.:,:,,,::::i:i:::::,,::::::iir;ii;7v77;ii;i,
,,, ,,:,::::::i:iiiii:i::::,, ::::iiiir@xingjief.r;7:i,
, , ,,,:,,::::::::iiiiiiiiii:,:,:::::::::iiir;ri7vL77rrirri::
:,, , ::::::::i:::i:::i:i::,,,,,:,::i:i:::iir;@Secbone.ii:::
*****************************************************************************
Singing my song for my dream.
*****************************************************************************
*/
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
typedef __int64 LL;
typedef pair<int,int> PII;
#define mp make_pair
#define pb push_back
#define FIN freopen("in.txt", "r", stdin);
#define FOUT freopen("out.txt", "w", stdout);
#define lson l, mid, cur << 1
#define rson mid + 1, r, cur << 1 | 1
#define lowbit(x) ((x)&(-x))
#define bitcnt(x) __builtin_popcount(x)
#define bitcntll(x) __builtin_popcountll(x)
#define debug puts("-------------");
//#pragma comment(linker, "/STACK:1024000000,1024000000")
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const double ERR = 1e-8;
const int MOD = 1e9 + 7;
const int MAXN = 1e6 + 50;
const int MAXM = 2e5 + 50;
LL n, k;
int main() {
#ifdef LOCAL_NORTH
FIN;
#endif // LOCAL_NORTH
while (~scanf("%I64d%I64d", &n, &k)) {
LL t, lcm = 1;
bool ok = false;
for (int i = 0; i < n; i++) {
scanf("%I64d", &t);
lcm = lcm * t / __gcd(lcm, t);
lcm = __gcd(lcm, k);
if (lcm == k) {
ok = true;
}
}
printf("%s\n", ok ? "Yes" : "No");
}
#ifdef LOCAL_NORTH
cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif // LOCAL_NORTH
return 0;
}
E. The Values You Can Make
给n个硬币,输出能组成k的硬币能组成哪些面额。
dp[i][j][k]表示前i个组成j硬币是否能组成k。状态转移方程为dp[i][j][k] = dp[i - 1][j][k](不拿第i个硬币) || dp[i - 1][j - c[i]][k](拿第i个硬币但不用来组合) || dp[i - 1][j - c[i]][k - c[i]](拿了第i个硬币,且用来组合)。最后找出dp[n][k][i]==true的i。
/*
::
:;J7, :, ::;7:
,ivYi, , ;LLLFS:
:iv7Yi :7ri;j5PL
,:ivYLvr ,ivrrirrY2X,
:;r@Wwz.7r: :ivu@kexianli.
:iL7::,:::iiirii:ii;::::,,irvF7rvvLujL7ur
ri::,:,::i:iiiiiii:i:irrv177JX7rYXqZEkvv17
;i:, , ::::iirrririi:i:::iiir2XXvii;L8OGJr71i
:,, ,,: ,::ir@mingyi.irii:i:::j1jri7ZBOS7ivv,
,::, ::rv77iiiriii:iii:i::,rvLq@huhao.Li
,, ,, ,:ir7ir::,:::i;ir:::i:i::rSGGYri712:
::: ,v7r:: ::rrv77:, ,, ,:i7rrii:::::, ir7ri7Lri
, 2OBBOi,iiir;r:: ,irriiii::,, ,iv7Luur:
,, i78MBBi,:,:::,:, :7FSL: ,iriii:::i::,,:rLqXv::
: iuMMP: :,:::,:ii;2GY7OBB0viiii:i:iii:i:::iJqL;::
, ::::i ,,,,, ::LuBBu BBBBBErii:i:i:i:i:i:i:r77ii
, : , ,,:::rruBZ1MBBqi, :,,,:::,::::::iiriri:
, ,,,,::::i: @arqiao. ,:,, ,:::ii;i7:
:, rjujLYLi ,,:::::,:::::::::,, ,:i,:,,,,,::i:iii
:: BBBBBBBBB0, ,,::: , ,:::::: , ,,,, ,,:::::::
i, , ,8BMMBBBBBBi ,,:,, ,,, , , , , , :,::ii::i::
: iZMOMOMBBM2::::::::::,,,, ,,,,,,:,,,::::i:irr:i:::,
i ,,:;u0MBMOG1L:::i:::::: ,,,::, ,,, ::::::i:i:iirii:i:i:
: ,iuUuuXUkFu7i:iii:i:::, :,:,: ::::::::i:i:::::iirr7iiri::
: :rk@Yizero.i:::::, ,:ii:::::::i:::::i::,::::iirrriiiri::,
: 5BMBBBBBBSr:,::rv2kuii:::iii::,:i:,, , ,,:,:i@petermu.,
, :r50EZ8MBBBBGOBBBZP7::::i::,:::::,: :,:,::i;rrririiii::
:jujYY7LS0ujJL7r::,::i::,::::::::::::::iirirrrrrrr:ii:
,: :@kevensun.:,:,,,::::i:i:::::,,::::::iir;ii;7v77;ii;i,
,,, ,,:,::::::i:iiiii:i::::,, ::::iiiir@xingjief.r;7:i,
, , ,,,:,,::::::::iiiiiiiiii:,:,:::::::::iiir;ri7vL77rrirri::
:,, , ::::::::i:::i:::i:i::,,,,,:,::i:i:::iir;@Secbone.ii:::
*****************************************************************************
Singing my song for my dream.
*****************************************************************************
*/
#include <map>
#include <set>
#include <cmath>
#include <ctime>
#include <queue>
#include <stack>
#include <string>
#include <vector>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <algorithm>
using namespace std;
typedef __int64 LL;
typedef pair<int,int> PII;
#define mp make_pair
#define pb push_back
#define FIN freopen("in.txt", "r", stdin);
#define FOUT freopen("out.txt", "w", stdout);
#define lson l, mid, cur << 1
#define rson mid + 1, r, cur << 1 | 1
#define lowbit(x) ((x)&(-x))
#define bitcnt(x) __builtin_popcount(x)
#define bitcntll(x) __builtin_popcountll(x)
#define debug puts("-------------");
//#pragma comment(linker, "/STACK:1024000000,1024000000")
const int INF = 0x3f3f3f3f;
const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
const double ERR = 1e-8;
const int MOD = 1e9 + 7;
const int MAXN = 500 + 50;
const int MAXM = 2e5 + 50;
int n, k;
bool dp[MAXN][MAXN];
int main() {
#ifdef LOCAL_NORTH
FIN;
#endif // LOCAL_NORTH
while (~scanf("%d%d", &n, &k)) {
memset(dp, false, sizeof(dp));
dp[0][0] = true;
for (int i = 0; i < n; i++) {
int c;
scanf("%d", &c);
for (int j = 500; j >= c; j--) {
for (int l = 500; l >= 0; l--) {
dp[j][l] |= dp[j - c][l];
dp[j][l] |= (l < c ? 0 : dp[j - c][l - c]);
}
}
}
vector<int> ans;
for (int i = 0; i <= 500; i++) {
if (dp[k][i]) {
ans.pb(i);
}
}
printf("%d\n", ans.size());
for (int i = 0; i < ans.size(); i++)
printf("%d%c", ans[i], " \n"[i == ans.size() - 1]);
}
#ifdef LOCAL_NORTH
cout << "Time elapsed: " << 1.0 * clock() / CLOCKS_PER_SEC * 1000 << " ms." << endl;
#endif // LOCAL_NORTH
return 0;
}