Day6:图论综合提高篇(2)

(几天的博客都没更了,现在过来补。。。゛(ノ><)ノ,)

T1:最大公约数

题目链接:最大公约数
题解:
直接开个桶,统计一下,在对于约数判断的就好了,(这题,我上来就打了gcd(),,,伤心死了o(╥﹏╥)o)

代码:
#include<bits/stdc++.h>
using namespace std;
inline int read()
{
	int s=0,w=1; char ch=getchar();
	while(ch<'0'||ch>'9'){if(ch=='-')w=-1;ch=getchar();}
	while(ch<='9'&&ch>='0')s=s*10+ch-'0',ch=getchar();
	return s*w;
}
const int sea=5e5+7; 
int tt[sea],n,k,mx;
int main()
{
	n=read(); k=read();
	if(k==0) {puts("0"); return 0;}
	for(int i=1;i<=n;i++) 
	{
		int x=read();
		++tt[x],mx=max(mx,x); 
	}
	for(int i=mx;i>=1;i--)
	{
		int cnt=0;
		for(int j=i;j<=mx;j+=i) cnt+=tt[j];
		if(cnt>=k){printf("%d\n",i); return 0;}
	}	
	
	return 0;
}

T2:计数

题目链接:计数
题解:
这个题啊,讲过一系列的推导,其实就是到结论题,最后的答案就是: A n s = C a a + b ⋅ C a − c a − c + d − C a a + d ⋅ C a − c a − c + b Ans=C^{a+b}_{a}·C^{a-c+d}_{a-c}-C^{a+d}_{a}·C^{a-c+b}_{a-c} Ans=Caa+bCacac+dCaa+dCacac+b
意思为:
(A1表示A的目的地,B1表示B的目的地)
ans = 从A到A1的方案数 × 从B到B1的方案数 - 从A到B1的方案数 × 从B到A1的方案数。
自己画一下图就可以脑补出来了,你在找一条从A到B1的路线时,你会发现,一定存在有哪条线是从B到A1的路线是与前者相交的。所以相减就行了。

代码:

(表示被取模卡了好长时间,,,,还是syk大佬帮我订正,再次感谢!ヾ(゚∀゚ゞ))

#include<bits/stdc++.h>
#define LL long long
using namespace std;
inline int read()
{
    int s=0,w=1; char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')w=-1; ch=getchar();}
    while(ch<='9'&&ch>='0')s=s*10+ch-'0',ch=getchar();
    return s*w;
}
const int mod=1e8+7;
int a,b,c,d;
LL ksm(LL a,LL b)
{
    LL s=1;
    while(b)
    {
        if(b&1) s=s*a%mod;
        b>>=1; a=a*a%mod;
    }
    return s%mod;
}
LL ny(LL a){return ksm(a,mod-2);}
LL C(int n,int m)
{
    LL s=1,t=1;
    for(int i=n;i>=n-m+1;i--) s=(s*i%mod)%mod; 
    for(int i=1;i<=m;i++) t=(t*i%mod)%mod;
    return s*ny(t)%mod;
}
int main()
{
    a=read(); b=read(); c=read(); d=read();
    LL ss=(C(a+b,a)*C(a-c+d,a-c)%mod-C(a+d,a)*C(a-c+b,a-c)%mod+mod)%mod;
    printf("%lld",ss);
    return 0;
}

T3:异色弧

表示这道题,看完题解,听完讲解后,一点都没有想做的欲望,就粘一下大佬的博客:Dream_Lolita(不要打我,,,)

代码:
//巧粘WY大佬的代码,,,,(是真的不会(〃>_<;〃))
#include <cstdio>
#include <algorithm>
using namespace std;
typedef long long ll;
const int MaxN = 1e5 + 5;
const int T = 400;
const int Mod = 1e9 + 7;
const int Ie = 5e8 + 4;
ll total, prefix, suffix, res, r, sigma[MaxN], sum[MaxN], bit[MaxN];
int n, a[MaxN], m, li[MaxN], cnt[MaxN], K, pre[MaxN], suf[MaxN], A[MaxN], nA, nex[MaxN], last[MaxN], tot;
void BitInsert(int pos) {
	for (int k = pos; k <= n; k += k & -k) ++bit[k];
	return;
}
ll BitQuery(int pos) {
	ll res = 0;
	for (int k = pos; k > 0; k -= k & -k) res += bit[k];
	return res;
}
int main() {
	freopen("arc.in", "r", stdin);
	freopen("arc.out", "w", stdout);
	scanf("%d", &n);
	for (int i = 1; i <= n; ++i) scanf("%d", &a[i]), li[++m] = a[i];
	sort(li + 1, li + m + 1), m = unique(li + 1, li + m + 1) - li - 1;
	for (int i = 1; i <= n; ++i) 
		++cnt[a[i] = lower_bound(li + 1, li + m + 1, a[i]) - li], nex[i] = last[a[i]], last[a[i]] = i;
	for (int i = 1; i <= m; ++i) {
		if (cnt[i] >= T) A[++nA] = i; suf[i] = cnt[i];
		if ((total += prefix * (r = ((ll)cnt[i] * (cnt[i] - 1) >> 1)) % Mod) >= Mod) total -= Mod;
		prefix += r;
	}
	
	//去除AABB的方案
	suffix = prefix, res = 0;
	for (int i = 1, c; c = a[i], i <= n; ++i) {
		if ((res += (((suffix -= --suf[c]) - ((ll)suf[c] * (suf[c] - 1) >> 1)) * pre[c]) % Mod) >= Mod) res -= Mod;
		++pre[c];
	}
	if ((total -= res) < 0) total += Mod;
	
	//去除ABBA的方案
	//task1:cntA >= T
	res = 0;
	for (int k = 1, c, cntA; prefix = 0, cntA = cnt[c = A[k]], k <= nA; ++k) {
		for (int w = 1; w <= m; ++w) sigma[w] = sum[w] = 0;
		for (int i = 1, w; w = a[i], i <= n; ++i) {
			if (w == c) ++prefix;
			else {
				if ((res += sum[w]) >= Mod) res -= Mod;
				if ((res -= sigma[w] * prefix % Mod) < 0) res += Mod;
				if ((sum[w] += prefix * cntA % Mod) >= Mod) sum[w] -= Mod;
				if ((sigma[w] += prefix) >= Mod) sigma[w] -= Mod;
			}
		}
	}
	if ((total -= res) < 0) total += Mod;
	
	//task2:cntA < T && cntB >= T 
	res = 0; 
	for (int k = 1, c; prefix = 0, c = A[k], k <= nA; ++k) {
		for (int w = 1; w <= m; ++w) sigma[w] = sum[w] = pre[w] = 0;
		for (int i = 1, w; w = a[i], i <= n; ++i)
			if (cnt[w] < T) {
				if ((res += (prefix * prefix - prefix) * (pre[w]++) % Mod) >= Mod) res -= Mod;
				if ((res += sum[w]) >= Mod) res -= Mod;
				if ((res -= 2ll * sigma[w] * prefix % Mod) < 0) res += Mod;
				if ((sum[w] += (prefix * prefix + prefix) % Mod) >= Mod) sum[w] -= Mod;
				if ((sigma[w] += prefix) >= Mod) sigma[w] -= Mod;
			}
			else prefix += (int)(w == c);
	}
	if ((total -= res * Ie % Mod) < 0) total += Mod;

	//task3:cntA < T && cntB < T
	res = 0;
	for (int i = 1; i <= n; ++i)
		if (cnt[a[i]] < T) {
			for (int k = nex[i]; k; k = nex[k]) 
				if ((res += tot - BitQuery(k)) >= Mod) res -= Mod;
			for (int k = nex[i]; k; k = nex[k]) ++tot, BitInsert(k);
		}
	if ((total -= res) < 0) total += Mod; //注意这样会多减去同一种颜色的两个点对
	
	for (int w = 1; w <= m; ++w) //再加上同一种颜色的两个点对
		if (cnt[w] > 3 && cnt[w] < T) {
			ll r = (ll)cnt[w] * (cnt[w] - 1) * (cnt[w] - 2) * (cnt[w] - 3) / 24;
			if ((total += r % Mod) >= Mod) total -= Mod;
		}
	
	printf("%I64d\n", total % Mod);
	return 0;
}

我站在绝境的悬崖边向下看,桃花烂漫 灼灼芬华 ,转过身,回首望,针针丛棘,枯鱼涸辙。我看了看那桃红芳华,毅然决然的调头,大步流星的向前走去。——Blng

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值