ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

2019.2.27模拟

2019-02-27 15:52:48  阅读:225  来源: 互联网

标签:ch int sum 2019.2 dep 27 ans 模拟 define


T1.数列

题目描述不贴了。
一眼秒,显然是一个模拟辗转相减的过程,因为辗转相减会T,所以我们用取模替代,就是做一个gcd。每次答案加上\(\lfloor\frac{a}{b}\rfloor\)即可,然后算上0要+1.

T2.最短距离

这题好像是什么雀巢杯的题。在一位dalao博客中找到了。传送门

不难想到,我们先把两个数的gcd除去,之后因为对于任意的\(a,b != 1,a*b >= a+b\),所以我们只要把x不断的分解成为一个质数,之后跳过去就行。

#include<bits/stdc++.h>
#define rep(i,a,n) for(int i = a;i <= n;i++)
#define per(i,n,a) for(int i = n;i >= a;i--)
#define enter putchar('\n')
#define I inline

using namespace std;
typedef long long ll;
const int M = 5005;

ll read()
{
    ll ans = 0,op = 1;char ch = getchar();
    while(ch < '0' || ch > '9') {if(ch == '-') op = -1;ch = getchar();}
    while(ch >= '0' && ch <= '9') ans = ans * 10 + ch - '0',ch = getchar();
    return ans * op;
}

ll n,q,x,y,p[M],tot;
bool np[M];

ll gcd(ll a,ll b){return (!b) ? a : gcd(b,a%b);}

void euler()
{
    np[1] = 1;
    rep(i,2,M-2)
    {
        if(!np[i]) p[++tot] = i;
        for(int j = 1;i * p[j] <= M-2;j++)
        {
            np[i * p[j]] = 1;
            if(!(i%p[j])) break;
        }
    }
}

int main()
{
    euler();
    n = read(),q = read();
    rep(i,1,q)
    {
        ll ans = 0;
        x = read(),y = read();
        if(x == y) {printf("0\n");continue;}
        int g = gcd(x,y);
        x /= g,y /= g;
        int t = x;
        rep(j,1,tot)
        {
            if(p[j] >= t) break;
            while(!(t%p[j])) t /= p[j],ans += p[j];
            if(t == 1) {t = p[j],ans -= p[j];break;}
        }
        ans += t / gcd(y,t);
        printf("%lld\n",ans);
    }
    return 0;
}

T3.电压
这题是bzoj4238.
我们相当于去掉一条边使得剩下的图可以二分染色。因为奇环不能二分染色而偶环可以,所以我们要取所有奇环的交集,而且不能有偶环经过。
实现方法使用树上差分。

#include<bits/stdc++.h>
#define rep(i,a,n) for(int i = a;i <= n;i++)
#define per(i,n,a) for(int i = n;i >= a;i--)
#define enter putchar('\n')
#define I inline

using namespace std;
typedef long long ll;
const int M = 400005;

bool vis[M];
int ecnt = 1,head[M],n,m,tmp,ans,tot[2],fa[M],dep[M],sum[M][2],x,y;

int read()
{
    int ans = 0,op = 1;char ch = getchar();
    while(ch < '0' || ch > '9') {if(ch == '-') op = -1;ch = getchar();}
    while(ch >= '0' && ch <= '9') ans = ans * 10 + ch - '0',ch = getchar();
    return ans * op;
}

struct edge
{
    int next,to,from;
}e[M];

void add(int x,int y)
{
    e[++ecnt].to = y;
    e[ecnt].from = x;
    e[ecnt].next = head[x];
    head[x] = ecnt;
}

void dfs(int x)
{
    vis[x] = 1;
    for(int i = head[x];i;i = e[i].next)
    {
        if(fa[x] == i || fa[x] == (i^1)) continue;
        int y = e[i].to;
        if(vis[y])
        {
            if(dep[y] > dep[x]) continue;
            int tmp = (dep[x] - dep[y]) & 1;
            sum[x][tmp]++,sum[y][tmp]--,tot[tmp]++;
        }
        else
        {
            fa[y] = i,dep[y] = dep[x] + 1;
            dfs(y),sum[x][0] += sum[y][0],sum[x][1] += sum[y][1];
        }
    }
}

int main()
{
    n = read(),m = read();
    rep(i,1,m) x = read(),y = read(),add(x,y),add(y,x);
    rep(i,1,n) if(!vis[i]) dfs(i);
    rep(i,1,n) if(sum[i][0] == tot[0] && !sum[i][1] && fa[i]) ans++;
    if(tot[0] == 1) ans++;
    printf("%d\n",ans);
    return 0;
}

标签:ch,int,sum,2019.2,dep,27,ans,模拟,define
来源: https://www.cnblogs.com/captain1/p/10444133.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有