NEFU 628 Garden visiting (数论)
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 (数论)的更多相关文章
- nefu 628 Garden visiting
//yy:想到昨天一个神题整了几个小时,最后按题解把p拆了用孙子定理..今天这个简单,把C暴力拆了.. 题目链接:nefu 628 Garden visiting 1 <= n, m, p &l ...
- acm数学(转)
这个东西先放在这吧.做过的以后会用#号标示出来 1.burnside定理,polya计数法 这个大家可以看brudildi的<组合数学>,那本书的这一章写的很详细也很容易理解.最好能 ...
- [转] POJ数学问题
转自:http://blog.sina.com.cn/s/blog_6635898a0100magq.html 1.burnside定理,polya计数法 这个大家可以看brudildi的<组合 ...
- ACM数学
1.burnside定理,polya计数法 这个专题我单独写了个小结,大家可以简单参考一下:polya 计数法,burnside定理小结 2.置换,置换的运算 置换的概念还是比较好理解的,< ...
- 机器人走方格 V3
1120 . 机器人走方格 V3 基准时间限制:1 秒 空间限制:65536 KB 分值: 160 N * N的方格,从左上到右下画一条线.一个机器人从左上走到右下,只能向右或向下走.并要求只能在 ...
- 数论 - 算数基本定理的运用 --- nefu 118 : n!后面有多少个0
题目链接:http://acm.nefu.edu.cn/JudgeOnline/problemshow.php Mean: 略. analyse: 刚开始想了半天都没想出来,数据这么大,难道是有什么 ...
- NEFU 118 n!后面有多少个0【数论】
http://acm.nefu.edu.cn/JudgeOnline/problemShow.php?problem_id=118 求n!后面有多少个0(1<=n<=1000000000) ...
- (《数论及应用1.3》NEFU 116 两仪剑法(最小公倍数&&最大公约数))
#include <iostream> using namespace std; long long gcd(long long a, long long b){ if(b == 0){ ...
- 数论结论 nefu 702
Given a prime p (p<108),you are to find min{x2+y2},where x and y belongs to positive integer, so ...
随机推荐
- [Bzoj5179][Jsoi2011]任务调度(左偏树)
5179: [Jsoi2011]任务调度 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5 Solved: 4[Submit][Status][Di ...
- 设置eclipse默认用户名
在使用Eclipse自动生成注释时,用户名总会被设置成Windows的登陆用户名.但大多数时候Windows用户名并不是我们写到注释里的用户名. 为了不想一个文件一个文件地修改,可以在最初时就设设置好 ...
- DTRACE FOR MYSQL PHP
Using DTrace to troubleshoot Apache DTrace for MySQL , Inspecting MySQL with DTrace , MySQL DTrace P ...
- html页面中拍照和上传照片那些事儿(一)
本文为原创,转载请注明出处: cnzt 文章:cnzt-p http://www.cnblogs.com/zt-blog/p/6709037.html 一. 思路: <input type= ...
- 蓦然回首,Java 已经 24 岁了!
01.蓦然 真没想到,Java 竟然 24 岁了(算是 90 后)! 提起 Java,印象最深刻的当然就是: class Cmower { public static void main(Strin ...
- Android 四大组件学习之Service五
本节学习IntentService, 可能就有人问了. 什么是IntentService, IntentService有什么作用? 不是已经有了Service,那为什么还要引入IntentServic ...
- 理解Android进程创建流程(转)
/frameworks/base/core/java/com/android/internal/os/ - ZygoteInit.java - ZygoteConnection.java - Runt ...
- 异或巧用:Single Number
异或巧用:Single Number 今天刷leetcode,碰到了到题Single Number.认为解答非常巧妙,故记之... 题目: Given an array of integers, ev ...
- QC ALM 11创建域、项目和用户
一旦HP-ALM安装,我们仅仅能继续创建域.项目和用户使用后的ALM工作.以下是步骤来创建项目.域和用户. 一.创建域 1.对于创建域,第一步是进入站点管理员页面.开展QC使用URL - ...
- Maven实战(七,八)——经常使用Maven插件介绍
我们都知道Maven本质上是一个插件框架,它的核心并不运行不论什么详细的构建任务,全部这些任务都交给插件来完毕,比如编译源代码是由maven-compiler-plugin完毕的.进一步说,每一个任务 ...