Count Path Pair

题目地址:点击打开链接


【分析】

我们设总的方案数为A,然后f,g相交的方案数为B。那么答案就是A-B了。

A很好计算,A=C(m+n,n)*C(m-p+q,q)


从图中我们可以看出,f,g在E点相交,等价于f从E点穿过。所以相交的方案数B就等价于“A到C,B到D的方案数”。

那么,B=C(m+q,q)*C(m-p+n,n)

至此,问题完美解决


【代码】

/****************************
    ID:Ciocio
    LANG:C++
    DATE:2014-1-26
    TASK:Count Path Pair
****************************/
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <iostream>
#include <algorithm>
#include <queue>

using namespace std;

#define MODER 100000007
#define MAXN 200000

typedef long long LL;
LL m,n,p,q,Plus,Sub;
LL f[MAXN+10];

LL _inv(LL a){
	int b=MODER-2;
	LL rt=1;
	a%=MODER;
	while(b){
		if(b&1) rt=(rt*a)%MODER;
		b>>=1;
		a=(a*a)%MODER;
	}
	return rt;
}

LL C(LL a,LL b){
	LL rt=f[a];
	rt=(rt*(_inv(f[b]*f[a-b]%MODER)))%MODER;
	return rt;
}

void _init(){
	Plus=C(m+n,n)*C(m-p+q,q)%MODER;
	Sub=C(m+q,q)*C(m-p+n,n)%MODER;
}

void _solve(){
	cout<<(Plus-Sub+MODER)%MODER<<endl;
}

int main(){
	f[1]=1;
	for(int i=2;i<=MAXN;i++)
	    f[i]=(f[i-1]*i)%MODER;
	while(cin>>m>>n>>p>>q){
	    _init();
	    _solve();
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,这是一个经典的分油问题,以下是C++代码实现: ```c++ #include <iostream> #include <queue> #include <vector> #include <set> using namespace std; struct Node { int a, b, c, step; vector<int> path; // 记录路径 }; int a1, b1, c1, a2, b2, c2; void bfs() { queue<Node> q; set<pair<int, int>> s; // 防止重复状态 vector<int> v; Node n = {a1, b1, c1, 0, v}; q.push(n); while (!q.empty()) { Node t = q.front(); q.pop(); if (t.a == a2 && t.b == b2 && t.c == c2) { // 找到目标状态 cout << t.step << endl; for (int i = 0; i < t.path.size(); i += 2) { cout << t.path[i] << " " << t.path[i + 1] << " "; if (i % 6 == 4) cout << endl; // 每输出3组换行 } if (t.path.size() % 6 != 4) cout << endl; // 补齐最后一行的换行 return; } // A -> B if (t.a > 0 && t.b < b2) { int v = min(t.a, b2 - t.b); Node nt = {t.a - v, t.b + v, t.c, t.step + 1, t.path}; nt.path.push_back(t.a); nt.path.push_back(t.b + v); if (!s.count(make_pair(nt.a, nt.b))) { q.push(nt); s.insert(make_pair(nt.a, nt.b)); } } // A -> C if (t.a > 0 && t.c < c2) { int v = min(t.a, c2 - t.c); Node nt = {t.a - v, t.b, t.c + v, t.step + 1, t.path}; nt.path.push_back(t.a); nt.path.push_back(t.c + v); if (!s.count(make_pair(nt.a, nt.c))) { q.push(nt); s.insert(make_pair(nt.a, nt.c)); } } // B -> A if (t.b > 0 && t.a < a2) { int v = min(t.b, a2 - t.a); Node nt = {t.a + v, t.b - v, t.c, t.step + 1, t.path}; nt.path.push_back(t.a + v); nt.path.push_back(t.b); if (!s.count(make_pair(nt.a, nt.b))) { q.push(nt); s.insert(make_pair(nt.a, nt.b)); } } // B -> C if (t.b > 0 && t.c < c2) { int v = min(t.b, c2 - t.c); Node nt = {t.a, t.b - v, t.c + v, t.step + 1, t.path}; nt.path.push_back(t.a); nt.path.push_back(t.c + v); if (!s.count(make_pair(nt.b, nt.c))) { q.push(nt); s.insert(make_pair(nt.b, nt.c)); } } // C -> A if (t.c > 0 && t.a < a2) { int v = min(t.c, a2 - t.a); Node nt = {t.a + v, t.b, t.c - v, t.step + 1, t.path}; nt.path.push_back(t.a + v); nt.path.push_back(t.b); if (!s.count(make_pair(nt.a, nt.c))) { q.push(nt); s.insert(make_pair(nt.a, nt.c)); } } // C -> B if (t.c > 0 && t.b < b2) { int v = min(t.c, b2 - t.b); Node nt = {t.a, t.b + v, t.c - v, t.step + 1, t.path}; nt.path.push_back(t.a); nt.path.push_back(t.b + v); if (!s.count(make_pair(nt.b, nt.c))) { q.push(nt); s.insert(make_pair(nt.b, nt.c)); } } } cout << "impossible" << endl; } int main() { cin >> a1 >> b1 >> c1 >> a2 >> b2 >> c2; bfs(); return 0; } ``` 上面代码中的 `Node` 结构体用于存储一个状态,包括三个瓶子的容量和当前步数,以及记录路径的 `vector` 类型的 `path` 成员。`set<pair<int, int>>` 用于防止重复状态。`bfs` 函数通过遍历所有可能的状态,直到找到目标状态为止。在遍历过程中,我们按照题目要求对每个状态做出相应的操作,计算出新的状态,并将其加入队列中。找到目标状态后,我们按照记录的路径输出结果。 以上就是C++实现分油问题的方法。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值