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. Borg Maze-POJ3026(bfs+最小生成树)

    http://poj.org/problem?id=3026 如果一个一个普通搜处理不好的话会超时  可以连到一块搜 我觉得这个方法特别好 #include<stdio.h> #inclu ...

  2. HUST 1328 String

    11: KMP next 的强大 题意求前缀在S中出现的次数之和 next[j] 表示 S[0....NEXT[J]]==S[J-NEXT[J].....J]; 于是我们得到..后加入一个字符所得到新 ...

  3. codeforces 892E(离散化+可撤销并查集)

    题意 给出一个n个点m条边的无向联通图(n,m<=5e5),有q(q<=5e5)个询问 每个询问询问一个边集{Ei},回答这些边能否在同一个最小生成树中 分析 要知道一个性质,就是权值不同 ...

  4. Hive安装中遇到过的坑

    实现说明每一个用户的环境都有细微的不一致,所以这里只是个人经过这些坑的处理,但是不意味着所有处理都是这样的操作,仅作为参考. 第一个坑 数据库安装,数据库最好装在Linux上,一直出了很多错,这里有一 ...

  5. GAN Generative Adversarial Network 生成式对抗网络-相关内容

    参考: https://baijiahao.baidu.com/s?id=1568663805038898&wfr=spider&for=pc Generative Adversari ...

  6. linux中的线程局部存储(TLS)

    http://blog.csdn.net/cywosp/article/details/26469435

  7. Office EXCEL 如何为宏命令指定快捷键或者重新设置快捷键

    1 工具-宏-宏,打开宏窗口   2 鼠标单击任意宏将其选中,点击宏选项,即可修改或为他增加快捷键.   3 注意,你直接在Visual Basci编辑器里面改是不行的,因为加了'的只是注释而已  

  8. 成员函数指针 C++ FAQ LITE — Frequently Asked Questions

    http://www.sunistudio.com/cppfaq/pointers-to-members.html C++ FAQ LITE — Frequently Asked Questions ...

  9. OSChinaclient源代码学习(2)--缓存的设计

    一.缓存的作用 请求数据的时候,首先进行推断,能否够从缓存中获取数据,假设满足条件,则直接从缓存中获取数据.否则请求新的数据.这样比没有缓存的情况下.每次都要从server请求数据要快,并且.没有网的 ...

  10. hdu 3183 A Magic Lamp(给一个n位的数,从中删去m个数字,使得剩下的数字组成的数最小(顺序不能变),然后输出)

    1.题目大意是,给你一个1000位的数,要你删掉m个为,求结果最小数. 思路:在n个位里面删除m个位.也就是找出n-m个位组成最小数 所以在区间 [0, m]里面找最小的数.相应的下标标号i 接着找区 ...