Codeforces Round #114 (Div. 1) E. Wizards and Bets 高斯消元
E. Wizards and Bets
题目连接:
http://www.codeforces.com/contest/167/problem/E
Description
In some country live wizards. They like to make weird bets.
Two wizards draw an acyclic directed graph with n vertices and m edges (the graph's vertices are numbered from 1 to n). A source is a vertex with no incoming edges, and a sink is the vertex with no outgoing edges. Note that a vertex could be the sink and the source simultaneously. In the wizards' graph the number of the sinks and the sources is the same.
Wizards numbered the sources in the order of increasing numbers of the vertices from 1 to k. The sinks are numbered from 1 to k in the similar way.
To make a bet, they, as are real wizards, cast a spell, which selects a set of k paths from all sources to the sinks in such a way that no two paths intersect at the vertices. In this case, each sink has exactly one path going to it from exactly one source. Let's suppose that the i-th sink has a path going to it from the ai's source. Then let's call pair (i, j) an inversion if i < j and ai > aj. If the number of inversions among all possible pairs (i, j), such that (1 ≤ i < j ≤ k), is even, then the first wizard wins (the second one gives him one magic coin). Otherwise, the second wizard wins (he gets one magic coin from the first one).
Our wizards are captured with feverish excitement, so they kept choosing new paths again and again for so long that eventually they have chosen every possible set of paths for exactly once. The two sets of non-intersecting pathes are considered to be different, if and only if there is an edge, which lies at some path in one set and doesn't lie at any path of another set. To check their notes, they asked you to count the total winnings of the first player for all possible sets of paths modulo a prime number p.
Input
The first line contains three space-separated integers n, m, p (1 ≤ n ≤ 600, 0 ≤ m ≤ 105, 2 ≤ p ≤ 109 + 7). It is guaranteed that p is prime number.
Next m lines contain edges of the graph. Each line contains a pair of space-separated integers, ai bi — an edge from vertex ai to vertex bi. It is guaranteed that the graph is acyclic and that the graph contains the same number of sources and sinks. Please note that the graph can have multiple edges.
Output
Print the answer to the problem — the total winnings of the first player modulo a prime number p. Please note that the winnings may be negative, but the modulo residue must be non-negative (see the sample).
Sample Input
4 2 1000003
1 3
2 4
Sample Output
1
Hint
题意
给你一个有向无环图,然后保证这个图里面有k个点的入度为0,k个点的出度为0
然后现在你需要在这个图里面找k条不相交的路径,使得入度为0的点和出度为0的点一一对应
如果连接的逆序对数为偶数,那么得到一块钱,如果为奇数,就失去一块钱
现在问你所有的情况都找到之和,问你最后你有多少钱
钱需要mod一个数
题解:
假设我们不考虑相交这个条件
那么我们用m[i][j]表示从第i个入度为0的点到第j个出度为0的点的路径数量的话
显然最后的答案就是m这个矩阵的行列式值
这个东西高斯消元就好了
然后我们考虑相交这个东西,其实相交这个东西没什么卵用
假设有1-2,2-3,2-5,4-2这四条边的话
显然答案是0,但是我们考虑相交也没关系,因为正反就抵消了
1-2-3,4-2-5;1-2-5,4-2-3 这样子就抵消了
所以相交这个条件没什么用。
然后搞一搞就完了,这道题
代码
#include<bits/stdc++.h>
using namespace std;
const int maxn = 650+5;
long long quickpow(long long m,long long n,long long k)//返回m^n%k
{
long long b = 1;
while (n > 0)
{
if (n & 1)
b = (b*m)%k;
n = n >> 1 ;
m = (m*m)%k;
}
return b;
}
int n,e,mod;
vector<int> E[maxn];
int in[maxn],out[maxn];
int f[maxn][maxn];
int m[maxn][maxn];
int q[maxn];
int S[maxn],T[maxn];
int k1=1,k2=1;
long long ans = 1;
void guess()
{
for(int i=1;i<k1;i++)
{
if(m[i][i]==0)
{
for(int j=i+1;j<k1;j++)
{
if(m[j][i])
{
for(int k=i;k<k1;k++)
m[i][k]=(m[i][k]+m[j][k])%mod;
break;
}
}
if(m[i][i]==0)
{
puts("0");
return;
}
}
long long inv = quickpow(m[i][i],mod-2,mod);
for(int j=i+1;j<k1;j++)
{
long long temp = m[j][i]*inv%mod;
if(temp==0)continue;
for(int k=i;k<k1;k++)
m[j][k]=(m[j][k]-m[i][k]*temp)%mod;
}
}
for(int i=1;i<k1;i++)
ans = ans*m[i][i]%mod;
if(ans<0)ans+=mod;
cout<<ans<<endl;
}
int main()
{
scanf("%d%d%d",&n,&e,&mod);
for(int i=1;i<=e;i++)
{
int x,y;
scanf("%d%d",&x,&y);
in[y]++,out[x]++;
E[x].push_back(y);
}
for(int i=1;i<=n;i++)
if(in[i]==0)
S[k1++]=i;
for(int i=1;i<=n;i++)
if(out[i]==0)
T[k2++]=i;
int l=1,r=1;
for(int i=1;i<=n;i++)
if(in[i]==0)
q[r++]=i;
while(l<r)
{
int now = q[l++];
for(int i=0;i<E[now].size();i++)
{
in[E[now][i]]--;
if(in[E[now][i]]==0)
q[r++]=E[now][i];
}
}
for(int i=1;i<k1;i++)
f[S[i]][S[i]]=1;
for(int i=1;i<=n;i++)
{
int u = q[i];
for(int j=1;j<k1;j++)
{
int s = S[j];
for(int k=0;k<E[u].size();k++)
f[s][E[u][k]]=(f[s][E[u][k]]+f[s][u])%mod;
}
}
for(int i=1;i<k1;i++)
for(int j=1;j<k1;j++)
m[i][j]=f[S[i]][T[j]];
guess();
}
Codeforces Round #114 (Div. 1) E. Wizards and Bets 高斯消元的更多相关文章
- Codeforces Round #114 (Div. 1) B. Wizards and Huge Prize 概率dp
B. Wizards and Huge Prize Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest ...
- Codeforces Round #114 (Div. 1) A. Wizards and Trolleybuses 物理题
A. Wizards and Trolleybuses Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/conte ...
- Codeforces Round #114 (Div. 1) D. Wizards and Roads 笛卡尔树+树贪心+阅读题
D. Wizards and Roads 题目连接: http://www.codeforces.com/contest/167/problem/D Description In some count ...
- Codeforces Round #114 (Div. 1) C. Wizards and Numbers 博弈论
C. Wizards and Numbers 题目连接: http://codeforces.com/problemset/problem/167/C Description In some coun ...
- Codeforces Round #114 (Div. 2)
Codeforces Round #114 (Div. 2) 代码 Codeforces Round #114 (Div. 2) C. Wizards and Trolleybuses 思路 每条车的 ...
- Codeforces Round #327 (Div. 2) A. Wizards' Duel 水题
A. Wizards' Duel Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/591/prob ...
- CodeForces - 24D :Broken robot (DP+三对角矩阵高斯消元 随机)
pro:给定N*M的矩阵,以及初始玩家位置. 规定玩家每次会等概率的向左走,向右走,向下走,原地不动,问走到最后一行的期望.保留4位小数. sol:可以列出方程,高斯消元即可,发现是三角矩阵,O(N* ...
- Codeforces Round #366 (Div. 2) ABC
Codeforces Round #366 (Div. 2) A I hate that I love that I hate it水题 #I hate that I love that I hate ...
- Codeforces Round #354 (Div. 2) ABCD
Codeforces Round #354 (Div. 2) Problems # Name A Nicholas and Permutation standard input/out ...
随机推荐
- Django 1.10中文文档-第一个应用Part5-测试
本教程上接教程Part4. 前面已经建立一个网页投票应用,现在将为它创建一些自动化测试. 自动化测试简介 什么是自动化测试 测试是检查你的代码是否正常运行的行为.测试也分为不同的级别.有些测试可能是用 ...
- 【Windows使用笔记】Windows日常使用软件
整理一些对于我来说日常使用的Windows软件. 排名不分先后,仅凭我想起来的顺序! 1 MadAppLauncher 这个对我来说非常需要了. 使用它可以快速启动日常常用的软件,非常快捷高效.一般来 ...
- 10.异步SRAM的FPGA读写操作
异步SRAM:正如其名,不是与特定的时钟信号同步运行,而是根据输入信号的状态运行的.因为没有信号表明读取时已确定了有效数据,也没有信号表明写入时已接收到数据,所以,需要获取制造商的数据手册,根据时序图 ...
- centos_7.1.1503_src_2
farstream02-0.2.3-3.el7.src.rpm 05-Jul-2014 12:59 1.2M fcoe-utils-1.0.29-9.el7.src.rpm 31-Mar-2015 ...
- 下载 LFS所需要的源码包的脚本程序及检验方法
下载 LFS所需要的源码包的脚本程序及检验方法 http://blog.csdn.net/yygydjkthh/article/details/45315143
- SqlServer开启CLR使用(C#)DLL实现实时Socket通知
--1.默认情况下,SQL Server中的CLR是关闭的,所以我们需要执行如下命令打开CLR: reconfigure GO -- DROP FUNCTION dbo.fnScoketSend -- ...
- CNN中千奇百怪的卷积方式大汇总
1.原始版本 最早的卷积方式还没有任何骚套路,那就也没什么好说的了. 见下图,原始的conv操作可以看做一个2D版本的无隐层神经网络. 附上一个卷积详细流程: [TensorFlow]tf.nn.co ...
- js-xlsx操作excel表格
1导入与导出功能实现 <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- 小知识-为什么Linux不需要磁盘碎片整理
转载至:http://beikeit.com/post-495.html 简单译文: 这段linux官方资料主要介绍了外部碎片(external fragmentation).内部碎片(inter ...
- SpringMVC调用过程
SpringMVC中的四大组件: 1.前端控制器(DispatcherServlet) =>[无需程序员开发] 主要是负责request和response对象的转发和响应. 2.处理器 ...