Garden visiting

    Problem:628  Time Limit:1000ms  Memory Limit:65536K

Description

There is a very big garden at Raven’s residence. We regard the garden as an n*m rectangle. Raven’s house is at the top left corner, and the exit of the garden is at the bottom right. He can choose to take one step to only one direction (up, down, left or right) each time. Raven wants to go out of the garden as quickly as possible, so he wonders how many routes he could choose.
Raven knows there are many possible routes, so he only wants to know the number, which is the result that the total number of possible routes modes a given value p. He knows it is a simple question, so he hopes you may help him to solve it.

Input

The first line of the input contains an integer T, which indicates the number of test cases.
Then it is followed by three positive integers n, m and p (1 <= n, m, p <= 10^5), showing the length and width of the garden and p to be the mod of the result.

Output

For each case, output one number to show the result (the sum modes p).

Sample Input

3
2 2 5
2 6 16
6 6 24

Sample Output

2
6
12

Hint

Sample 1: There are 2 routes in total.
Sample 2: There are 6 routes in total.
Sample 3: There are 252 routes in total.

题意:给定一个n*m的矩阵,让你求从左上角走到右下角有多少方法。

析:很明显一个组合问题,C(n+m-2, m-1),这就是答案,我们只要计算这个就好,所以暴力去分解分子和分母,然后再乘起来。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
//#include <unordered_map>
//#include <tr1/unordered_map>
//#define freopenr freopen("in.txt", "r", stdin)
//#define freopenw freopen("out.txt", "w", stdout)
using namespace std;
//using namespace std :: tr1; typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
//const double inf = 0x3f3f3f3f3f3f;
//const LL LNF = 0x3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 10005;
//const LL mod = 10000000000007;
const int N = 1e6 + 5;
const int dr[] = {-1, 0, 1, 0, 1, 1, -1, -1};
const int dc[] = {0, 1, 0, -1, 1, -1, 1, -1};
const char *Hex[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
inline LL gcd(LL a, LL b){ return b == 0 ? a : gcd(b, a%b); }
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline int Min(int a, int b){ return a < b ? a : b; }
inline int Max(int a, int b){ return a > b ? a : b; }
inline LL Min(LL a, LL b){ return a < b ? a : b; }
inline LL Max(LL a, LL b){ return a > b ? a : b; }
inline bool is_in(int r, int c){
return r >= 0 && r < n && c >= 0 && c < m;
}
vector<int> prime;
bool a[200050]; void init(){
int m = sqrt(200050+0.5);
memset(a, false, sizeof a);
for(int i = 2; i <= m; ++i) if(!a[i])
for(int j = i*i; j < 200050; j += i) a[j] = true;
for(int i = 2; i < 200050; ++i) if(!a[i]) prime.push_back(i);
}
int p; LL quick_pow(LL a, int n){
LL ans = 1;
while(n){
if(n & 1) ans = ans * a % p;
a = a * a % p;
n >>= 1;
}
return ans;
} int cal(int x, int n){
int ans = 0;
while(n){
ans += n / x;
n /= x;
}
return ans;
} LL solve(int n, int m){
LL ans = 1;
for(int i = 0; i < prime.size() && prime[i] <= n; ++i){
int x = cal(prime[i], n);
int y = cal(prime[i], n-m);
int z = cal(prime[i], m);
x -= y + z;
ans = ans * quick_pow((LL)prime[i], x) % p;
}
return ans;
} int main(){
init();
int T; cin >> T;
while(T--){
scanf("%d %d %d", &n, &m, &p);
n += m-2;
--m;
printf("%lld\n", solve(n, m));
}
return 0;
}

NEFU 628 Garden visiting (数论)的更多相关文章

  1. nefu 628 Garden visiting

    //yy:想到昨天一个神题整了几个小时,最后按题解把p拆了用孙子定理..今天这个简单,把C暴力拆了.. 题目链接:nefu 628 Garden visiting 1 <= n, m, p &l ...

  2. acm数学(转)

    这个东西先放在这吧.做过的以后会用#号标示出来 1.burnside定理,polya计数法    这个大家可以看brudildi的<组合数学>,那本书的这一章写的很详细也很容易理解.最好能 ...

  3. [转] POJ数学问题

    转自:http://blog.sina.com.cn/s/blog_6635898a0100magq.html 1.burnside定理,polya计数法 这个大家可以看brudildi的<组合 ...

  4. ACM数学

     1.burnside定理,polya计数法 这个专题我单独写了个小结,大家可以简单参考一下:polya 计数法,burnside定理小结 2.置换,置换的运算 置换的概念还是比较好理解的,< ...

  5. 机器人走方格 V3

    1120 . 机器人走方格 V3   基准时间限制:1 秒 空间限制:65536 KB 分值: 160 N * N的方格,从左上到右下画一条线.一个机器人从左上走到右下,只能向右或向下走.并要求只能在 ...

  6. 数论 - 算数基本定理的运用 --- nefu 118 : n!后面有多少个0

     题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemshow.php Mean: 略. analyse: 刚开始想了半天都没想出来,数据这么大,难道是有什么 ...

  7. NEFU 118 n!后面有多少个0【数论】

    http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=118 求n!后面有多少个0(1<=n<=1000000000) ...

  8. (《数论及应用1.3》NEFU 116 两仪剑法(最小公倍数&&最大公约数))

    #include <iostream> using namespace std; long long gcd(long long a, long long b){ if(b == 0){ ...

  9. 数论结论 nefu 702

    Given a prime p (p<108),you are to find min{x2+y2},where x and y belongs to positive integer, so ...

随机推荐

  1. type和metaclass元类

    元类type 1. 创建类的两种方式 (都是由type元类创建) 方式一: class Foo(object): # 默认metaclass = type, 当前类, 由type类创建 a = 'aa ...

  2. 网络安全法与LogSec日志安全大数据审计平台

    https://blog.csdn.net/chengpeng1144/article/details/73555331 https://blog.csdn.net/dcbeyond/article/ ...

  3. PAT (Advanced Level) 1035. Password (20)

    简单题. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> ...

  4. PAT (Advanced Level) 1032. Sharing (25)

    简单题,不过数据中好像存在有环的链表...... #include<iostream> #include<cstring> #include<cmath> #inc ...

  5. java类中资源加载顺序

    根据优先级别从高到低依次为:1.父类中的静态代码块(static);2.自身的静态代码块;3.父类中的的普通代码块;4.父类的构造方法;5.自身的普通代码块;6.自身的构造方法; 下面是一个测试 结果 ...

  6. eclipse发布项目到tomcat部署目录

    1.在eclipse下建立Dynamic Web Project工程zhgy,在使用eclipse中new一个tomcat,通过启动该tomcat来发布Dynamic Web Project的时候,其 ...

  7. 利用NSA的MS17-010漏洞利用工具实现Win 7和Win Server 2008系统入侵

    影子经纪人(Shadow Brokers)最近陆续曝光的NSA网络武器令人震惊,尽管这些工具是否出自国家级别黑客团队之手尚不清楚,但至少存在一个可以说明问题的事实:这些漏洞利用工具都能有效运行,且具有 ...

  8. ZOJ 3316 Game 一般图最大匹配带花树

    一般图最大匹配带花树: 建图后,计算最大匹配数. 假设有一个联通块不是完美匹配,先手就能够走那个没被匹配到的点.后手不论怎么走,都必定走到一个被匹配的点上.先手就能够顺着这个交错路走下去,最后一定是后 ...

  9. 纯C语言实现简单封装继承机制

    0 继承是OO设计的基础 继承是OO设计中的基本部分,也是实现多态的基础,C++,C#,Objective-C.Java.PHP.JavaScript等为OO而设计的语言,其语言本身对实现继承提供了直 ...

  10. 【内存数据库】OracleTimesten连接DSN创建用户

    ************************************************************************ ****原文:blog.csdn.net/clark_ ...