nefuoj Garden visiting 628 (组合数学&&Lucas定理)
2015-12-16 22:28
513 查看
Garden visiting | |||
| |||
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. #include<stdio.h>#include<string.h>#include<algorithm>#define ll long long#define N 200010using namespace std;int n,m,p;int P;bool pp;int pnum;void getp(){int i,j;for(i=2;i<N;i++){if(!pp[i]){P[pnum++]=i;for(j=i+i;j<N;j+=i)pp[j]=true;}}}int getzs(int n,int k){int ans=0;while(n>=k){n/=k;ans+=n;}return ans;}ll ksm(ll x,ll y)//快速幂{ll ans=1;while(y){if(y&1)ans=(ans*x)%p;x*=x;x%=p;y>>=1;}return ans;}int main(){getp();int t,i;scanf("%d",&t);while(t--){scanf("%d%d%d",&n,&m,&p);n+=m-2;m--;ll ans=1;for(i=0;i<pnum&&P[i]<=n;i++){int num=getzs(n,P[i])-getzs(m,P[i])-getzs(n-m,P[i]);ans=ans*ksm(P[i],num)%p;}printf("%d\n",(int)ans);}return 0;} |
相关文章推荐
- 前端开发入门:前端开发基础成长指南
- 内存映射
- 16、osg中使用Imposter设置一个视矩,超过这个视距模型会变为一张动态图
- 动态刷新页面(滚动页面)
- 蓝牙核心技术概述(一):蓝牙概述
- svn win
- ByteBuffer小结
- python基础知识
- HDU 1030 Delta-wave 模拟
- leetcode -- Word Search I & II -- dfs 重点
- Does Oracle GoldenGate requires Xlc.Adt.Include 8.0 (文档 ID 1147116.1)
- 求平均排序MATLAB code
- js实现的ajax对象创建&与服务器交互
- jmeter之正则表达式写法实例推荐
- java设计模式(七)--迭代器模式
- 【 bzoj 3992 】 [SDOI2015]序列统计 - NTT 生成函数
- java设计模式
- HashMap多线程死循环问题
- Centos 之find详解
- py环境安装