The 2021 ICPC Asia Shanghai Regional Programming Contest

https://codeforces.com/gym/103446

D. Strange Fractions

题意:给你p,q,找出a,b满足\frac{p}{q} = \frac{a}{b}+\frac{b}{a} 

思路:解方程,注意判断不合法情况

#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/JCiyOicon-default.png?t=N7T8http://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]);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值