zoj 3604 Tunnel Network(数字快速幂)

来源:互联网 发布:上海黑桃互动 知乎 编辑:程序博客网 时间:2024/06/17 01:29

转载请注明出处:http://blog.csdn.net/u012860063

题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4707



Country Far-Far-Away is a big country with N cities. But it is now under a civil war. The rebel uses the ancient tunnel network which connects allN cities withN-1 inter-city tunnels for transportation. The government army want to destroy these tunnels one by one. After several months fighting, some tunnels have been destoryed. According to the intel, the tunnel network have excatlyS connected components now. And what government army further knows is that city 1, 2, ... ,S belong to each of theS connected components. Since the government have little knowledge about the remaining tunnels, they ask you to calculate the number of possible networks of remaining tunnels.

Input

There are multiple test cases. The first line of input contains an integer T (T ≤ 500) indicating the number of test cases. Then T test cases follow.

Each case contains one line containing two integer N (2 ≤ N ≤ 2000) andS (2 ≤S ≤ N), as described above.

Output

The number of possible networks now. Since the number might be very large, you should output the answer after modulo 1000000007.

Sample Input

43 24 25 3100 50

Sample Output

2815113366355

代码如下(申明此代码纯属巧过):

#include <iostream>#include <sstream>#include <algorithm>#include <vector>#include <queue>#include <stack>#include <map>#include <set>#include <bitset>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <climits>typedef long long LL;using namespace std ;#define mod 1000000007LL quickmod (LL a,LL b) {LL ans = 1;while(b) {if(b&1){ans = (ans * a)%mod;b--;}b >>= 1;a = (a * a)%mod;}return ans;}LL n, s;int main(){LL T;scanf("%lld",&T);while(T--){scanf("%lld %lld",&n,&s);if(n==s){puts("1");continue;}LL ans  = quickmod(n,n - s - 1);ans = (ans * s)%mod;printf("%lld\n", ans);}return 0;}


0 0