CSP - J 2022 题解
Carey_chen · · 算法·理论
警告:抄代码者 \text{CE}
0x01 乘方
题目大意
给定
思路
使用 int 即可,如果 int 范围会得到负数,这点需要特判。
\mathfrak{Code\,Here}
#include <bits/stdc++.h>
int main()
{
int a, b, ans;
scanf("%d %d", &a, &b);
ans = pow(a, b);
if(ans >= 1 && ans <= 1e9)
{
printf("%d", ans);
}
else
{
printf("%d", -1);
}
return 0;
}
0x02 解密
题目大意
题面已经很简洁了。
思路
以下
带入
我们考虑题目中的
现在我们有两个式:
-
p-q=\sqrt{(n-ed+2)^2-4n} -
p+q=n-ed+2
两个加起来除以
注意会炸 long long, 请使用 long double
\mathfrak{Code\,Here}
#include<bits/stdc++.h>
int k;
int main()
{
scanf("%d", &k);
while(k --)
{
long double p, q, g, n, e, d, j;
scanf("%Lf %Lf %Lf", &n, &d, &e);
g = 1.0 * n - e * d + 2;
j = sqrt(g * g - 4 * n);
p = 1.0 * (g + j) / 2;
q = 1.0 * g - p;
if(((long long)p) != p || ((long long)q) != q)
{
printf("NO\n");
}
else
{
printf("%lld %lld\n", (long long)(p < q ? p : q), (long long)(p > q ? p : q));
}
}
return 0;
}
T3 逻辑表达式
本人蒟蒻,不会做……
T4 上升点列
题目大意
给你
数据范围:保证对于所有数据满足:
思路
设状态
易得如下方程。
最终答案为
\mathfrak{Code\,Here}
#include<bits/stdc++.h>
using namespace std;
struct node
{
int x, y;
bool operator < (const node &w)
{
if(x == w.x)
{
return y < w.y;
}
return x < w.x;
}
};
int n,k;
int f[510][110];
node a[510];
int muan()
{
scanf("%d %d",&n,&k);
for(int i = 1; i <= n; i++)
{
scanf("%d %d", &a[i].x, &a[i].y);
}
sort(a + 1, a + 1 + n);
for(int i = 1; i <= n; i++)
{
f[i][k] = 1;
for(int j = 0; j <= k; j++)
{
for(int t = 1; t < i; t++)
{
if(a[t].x > a[i].x || a[t].y > a[i].y)
{
continue;
}
int d = abs(a[i].x - a[t].x) + abs(a[i].y - a[t].y) - 1;
if(j + d > k)
{
continue;
}
f[i][j] = max(f[i][j], f[t][j+d]+d+1);
}
}
}
int ans = 0;
for(int i = 1; i <= n; i++)
{
for(int j = 0; j <= k; j++)
{
ans = max(ans, j + f[i][j]);
}
}
cout << ans;
return 0;
}