A 平方序列
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
int main()
{
int x, y;
for (int i = 2020; i; i ++ )
{
x = i;
int t = 2 * x * x - 2019 * 2019;
y = sqrt(t);
if (y * y == t)
break;
}
cout << x + y << endl;
return 0;
}
B 质数拆分
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std;
const int N = 2022;
typedef long long LL;
vector<int> res;
LL f[N];
bool is_prime(int x)
{
if (x < 2) return false;
for (int i = 2; i <= x / i; i ++ )
if (x % i == 0)
return false;
return true;
}
int main()
{
for (int i = 2; i <= 2019; i ++ )
if (is_prime(i))
res.push_back(i);
f[0] = 1;
for (int i = 0; i < res.size(); i ++ )
{
for (int j = 2019; j >= res[i]; j -- )
f[j] += f[j - res[i]];
}
cout << f[2019] << endl;
return 0;
}
C 拼接
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;
const int N = 10;
long long res;
bool st[N][N];
vector<pair<int, int >> ans;
int dx[4] = {-1, 0, 1, 0};
int dy[4] = {0, 1, 0, -1};
void dfs(int x, int y)
{
if (x == 7)
{
res ++;
return;
}
for (int i = 0; i < 4; i ++)
{
int xx = x + dx[i], yy = y + dy[i];
if ( (y == 0 && i == 1) || yy < 0 || st[xx][yy] || xx <= yy)
continue;
st[xx][yy] = true;
dfs(xx, yy);
st[xx][yy] = false;
}
}
int main()
{
for (int i = 1; i < 7; i ++)
{
st[i][i] = true;
dfs(i, i);
st[i][i] = false;
}
cout << res + 2 << endl;
return 0;
}
D 求值
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
int count(int x)
{
int cnt = 1;
for (int i = 2; i <= x / i; i ++ )
{
int s = 0;
while (x % i == 0)
{
x /= i;
s ++ ;
}
cnt *= 1 + s;
}
if (x > 1) cnt *= 2;
return cnt;
}
int main()
{
for (int i = 100; i; i ++ )
{
if (count(i) == 100)
{
cout << i << endl;
break;
}
}
return 0;
}
E 路径计数
这道题官网答案有误,比赛时是按照206这个答案几分的
#include <iostream>
#include <cstring>
#include <algorithm>
typedef long long LL;
using namespace std;
const int N = 10;
LL ans;
bool st[N][N];
int n = 6;
int dx[4] = {-1, 0, 1, 0}, dy[4] = {0, 1, 0, -1};
bool check(int x)
{
return x > 1 && x <= 11;
}
void dfs(int x, int y, int step)
{
if (x == 0 && y == 0 && check(step))
{
ans ++ ;
return ;
}
st[x][y] = true;
for (int i = 0; i < 4; i ++ )
{
int a = x + dx[i], b = y + dy[i];
if (a < 0 || a >= n || b < 0 || b >= n) continue;
if (st[a][b]) continue;
dfs(a, b, step + 1);
}
st[x][y] = false;
}
int main()
{
dfs(0, 1, 0);
cout << ans * 2 << endl;
return 0;
}
F 最优包含
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 1010;
char a[N], b[N];
int f[N][N];
int main()
{
scanf("%s%s", a + 1, b + 1);
int n = strlen(a + 1), m = strlen(b + 1);
memset(f, 0x3f, sizeof f);
for (int i = 0; i <= n; i ++ )
f[i][0] = 0;
for (int i = 1; i <= n; i ++ )
for (int j = 1; j <= i; j ++ )
{
if (a[i] == b[j])
{
f[i][j] = f[i - 1][j - 1];
}
else f[i][j] = min(f[i - 1][j], f[i - 1][j - 1] + 1);
}
cout << f[n][m] << endl;
return 0;
}
G 排列数
#include <iostream>
using namespace std;
#define mod 123456
#define N 505
int dp[N][N];
int main()
{
int n, k;
cin >> n >> k;
dp[1][0] = 1;
for(int i = 2; i <= n; i++){
dp[i][0] = 2;
for(int j = 0; j <= i-2; j++){
dp[i][j] %= mod;
dp[i+1][j] += dp[i][j] * (j+1);
dp[i+1][j+1] += dp[i][j] * 2;
dp[i+1][j+2] += dp[i][j] * (i-j-2);
}
}
cout << dp[n][k-1] % mod << endl;
return 0;
}
H 解谜游戏
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
int main()
{
string a, b, c;
int T;
scanf("%d", &T);
while (T -- )
{
cin >> c >> b >> a;
bool flag = true;
for (int i = 0; i < 4; i ++ )
{
map<char, int> mp;
mp[a[i]] ++ ;
mp[b[i]] ++ ;
mp[b[i + 4]] ++ ;
mp[c[i]] ++ ;
mp[c[i + 4]] ++ ;
mp[c[i + 8]] ++ ;
if (mp['Y'] != 1 || mp['R'] != 2 || mp['G'] != 3)
{
flag = false;
break;
}
}
if (flag) puts("YES");
else puts("NO");
}
return 0;
}
I 第八大奇迹
线段树模板题,比较简单但是没有学过,学完后更
在这里插入代码片
H 燃烧权杖
不会,抱歉
在这里插入代码片
|