UVA 11174 Stand in a Line——组合数+取模
来源:互联网 发布:淘宝男t恤店推荐 知乎 编辑:程序博客网 时间:2024/06/03 22:59
要求(b / c) % mod,
令a = b / c;则a % mod = b * (c ^ (mod - 2)) % mod;
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#include <cmath>using namespace std;const long long mod = 1000000007;const int maxn = 400005;int T, n, m, tot, head[maxn], num[maxn], fa[maxn];long long f[maxn];struct Edge { int to, next;}edge[maxn<<1];void init() { tot = 0; memset(head, -1, sizeof(head));}void addedge(int u, int v) { edge[tot].to = v; edge[tot].next = head[u]; head[u] = tot++;}void dfs(int u, int p) { for (int i = head[u]; ~i; i = edge[i].next) { int v = edge[i].to; if (v == p) continue; dfs(v, u); num[u] += num[v]; }}long long mpow(long long x, long long y) { long long ans = 1; while (y) { if (y & 1) ans = ans * x % mod; x = x * x % mod; y >>= 1; } return ans;}int main() { f[0] = 1; for (int i = 1; i < maxn; i++) { f[i] = (f[i-1] * i) % mod; } scanf("%d", &T); for (int kase = 1; kase <= T; kase++) { scanf("%d %d", &n, &m); init(); num[0] = 0; for (int i = 1; i <= n; i++) num[i] = 1; memset(fa, -1, sizeof(fa)); for (int i = 1; i <= m; i++) { int u, v; scanf("%d %d", &u, &v); fa[u] = v; addedge(v, u); } for (int i = 1; i <= n; i++) { if (fa[i] == -1) addedge(0, i); } dfs(0, -1); long long ans = 1; for (int i = 1; i <= n; i++) { ans = (ans * num[i]) % mod; } ans = (f[n] * mpow(ans, mod-2)) % mod; printf("%lld\n", ans); } return 0;}
阅读全文
1 0
- UVA 11174 Stand in a Line——组合数+取模
- UVA 11174 - Stand in a Line(组合递归计数)
- UVA 11174 - Stand in a Line (数学基础+除法取模)
- UVA - 11174 Stand in a Line
- uva 11174 Stand in a Line
- UVa 11174 Stand in a Line
- UVA 11174 Stand in a Line
- uva 11174 - Stand in a Line(逆元+递推)
- UVA 11174 Stand in a Line 树形dp+计数
- uvaoj 11174 - Stand in a Line
- Stand in a Line UVA11174
- UVA11174 Stand in a Line
- uva11174 Stand in a Line
- Uva 11174 Stand in a Line 解题报告(递推+逆元)
- uva 11174 - Stand in a Line(求乘法逆元)
- ACM: uva 11174 - Stand in a Line
- 数学,乘法逆元(Stand in a Line,uva 11174)
- 【UVA11174】Stand in a Line——逆元+树形Dp
- JS基础知识
- mt6735 [CTS Fail]BuildFingerprint
- Lua函数
- 如何获取系统时间,年月日时分秒?
- Python3之shutil模块
- UVA 11174 Stand in a Line——组合数+取模
- PHP5.6 配置文件php.ini详解(译版)
- 解决XCode编译异常clang: error: linker command failed with exit code 1
- 跟着示例学Oozie
- HP elitebook 840 G3 蓝牙无法使用问题 + 触摸板无法关闭
- DES算法简介及android中的实现
- Spring注解详解
- 使用eclipse提交项目到GitHub
- linux系统上启动mysql报错2002:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/var/l