https://codeforces.com/gym/103446
D. Strange Fractions
题意:给你p,q,找出a,b满足 =
思路:解方程,注意判断不合法情况
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<set>
#define Max 0x7fffffff
#define LL long long
#define ULL unsigned long long
#define N 200005
#define int long long
int mod = 998244353;
using namespace std;
int gcd(int j, int k) {
return k > 0 ? gcd(k, j % k) : j;
}
signed main() {
int T; scanf("%lld", &T);
while (T--) {
int p, q; scanf("%lld%lld", &p, &q);
int k = gcd(p, q);
p /= k, q /= k;
int t = sqrt(p + 2*q),t1=sqrt(p-2*q);
if (t * t != p + 2 * q || t1 * t1 != p - 2 * q || (t + t1) &1) {
printf("0 0\n");
continue;
}
int a = (t + t1) / 2,b=t-a;
if (a <= 0 || b <= 0) {
printf("0 0\n");
continue;
}
printf("%lld %lld\n", a, b);
}
}
E. Strange Integers
题意:给你一个序列,选出一个子序列,满足这个序列中
求满足要求的序列的最大长度。
思路:差分
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<set>
#define Max 0x7fffffff
#define LL long long
#define ULL unsigned long long
#define N 200005
//#define int long long
int mod = 998244353;
using namespace std;
int a[N], d[N];
int main() {
int n,k; scanf("%d%d", &n,&k);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
sort(a, a + n);
int t=0,ans=0;
for (int i = 1; i < n; i++) {
d[i] = a[i] - a[i - 1];
t += d[i];
if (t >= k) {
ans++;
t = 0;
}
}
printf("%d", ans+1);
}
G. Edge Groups
题意:给你n个节点,n-1条边,其中n为奇数,求按每小组中有两条边 并且 两条边有一个相同的节点 分成(n-1)/ 2个组 有多少种分法。
思路:http://t.csdn.cn/JCiyOhttp://t.csdn.cn/JCiyO
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<queue>
#include<vector>
#include<map>
#include<stack>
#include<set>
#define Max 0x7fffffff
#define LL long long
#define ULL unsigned long long
#define N 200005
#define int long long
int mod = 998244353;
using namespace std;
vector<int>head[N]; int dp[N];
bool dfs(int u, int fa) {
int cnt = 0;
dp[u] = 1;
for (int i = 0; i < head[u].size(); i++) {
int v = head[u][i];
if (v == fa)continue;
if (!dfs(v, u))
cnt++;
dp[u] = dp[u] * dp[v] % mod;
}
for (int i = 1; i <= cnt; i += 2) {
dp[u] = dp[u] * i % mod;
}
return cnt & 1;
}
signed main() {
int n; scanf("%d", &n);
for (int i = 1; i < n; i++) {
int u, v;scanf("%lld%lld",&u,&v);
head[u].push_back(v);
head[v].push_back(u);
}
dfs(1, 0);
printf("%lld", dp[1]);
}