2019.2.27模拟

T1.数列

题目描述不贴了。
一眼秒,显然是一个模拟辗转相减的过程,因为辗转相减会T,所以我们用取模替代,就是做一个gcd。每次答案加上(lfloorfrac{a}{b} floor)即可,然后算上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('
')
#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
");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
",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('
')
#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
",ans);
	return 0;
}

原文地址:https://www.cnblogs.com/captain1/p/10444133.html