您的位置:首页 > 其它

数论——Lucas定理模板

2011-09-17 13:42 309 查看

Formulation

For non-negative integers m and n and a prime p, the following congruence relation holds:



where



and



are the base p expansions of m and n respectively.

#include <iostream>
#include <cstdio>
#include <cstring>
usingnamespace std;

typedef longlong llg;

constint N =150000;

llg n, m, p, fac
;

void init()
{
int i;
fac[0] =1;
for(i =1; i <= p; i++)
fac[i] = fac[i-1]*i % p;
}

llg pow(llg a, llg b)
{
llg tmp = a % p, ans =1;
while(b)
{
if(b &1)  ans = ans * tmp % p;
tmp = tmp*tmp % p;
b >>=1;
}
return  ans;
}

llg C(llg n, llg m)
{
if(m > n)  return0;
return  fac
*pow(fac[m]*fac[n-m], p-2) % p;
}

llg Lucas(llg n, llg m)
{
if(m ==0)  return1;
elsereturn  (C(n%p, m%p)*Lucas(n/p, m/p))%p;
}

int main()
{
int t;
scanf("%d", &t);
while(t--)
{
scanf("%I64d%I64d%I64d", &n, &m, &p);
init();
printf("%I64d\n", Lucas(n+m, n));
}
return0;
}


  
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: