标签:
组合素数 |
|||
|
|||
description |
|||
小明的爸爸从外面旅游回来给她带来了一个礼物,小明高兴地跑回自己的房间,拆开一看是一个很大棋盘(非常大),小明有所失望。不过没过几天发现了大棋盘的好玩之处。从起点(0,0)走到终点(n,n)的非降路径数是C(2n,n),现在小明随机取出1个素数p, 他想知道C(2n,n)恰好被p整除多少次?小明想了很长时间都没想出来,现在想请你帮助小明解决这个问题,对于你来说应该不难吧! |
|||
input |
|||
有多组测试数据。 第一行是一个正整数T,表示测试数据的组数。接下来每组2个数分别是n和p的值,这里1<=n,p<=1000000000。 |
|||
output |
|||
对于每组测试数据,输出一行,给出C(2n,n)被素数p整除的次数,当整除不了的时候,次数为0。 |
|||
sample_input |
|||
2 2 2 2 3 |
|||
sample_output |
|||
1 1 |
思路:
C(2n,n)=( (2n)! ) / (n!*n!)
求上式可以被素数p整除多少次,也就是求上式中素数p的幂
所以统计2n!的幂和(n!*n!)的幂,相减就可以了,但要注意统计幂的时候要用long long,而且nefu要用%lld
//Accepted 828k 1ms C++ (g++ 3.4.3) 574 #include<cstdio> #include<iostream> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; int main() { int T; scanf("%d",&T); while(T--) { ll n,p; scanf("%lld%lld",&n,&p); ll t1=0,t2=0; ll tmp=2*n; ll P=p; while(tmp/P) { t1+=tmp/P; P*=p; } tmp=n; P=p; while(tmp/P) { t2+=2*(tmp/P); P*=p; } printf("%lld\n",t1-t2); } return 0; }
标签:
原文地址:http://blog.csdn.net/kalilili/article/details/44853263