题解 P5160 【WD与循环】
JustinRochester · · 题解
看到除了出题人还没人发题解,赶紧发一篇“公(xiā)式(jī)归(bā)纳(luàn)法(cāi)” 2333333
来自某 AFO 且数学学完数学归纳法的蒟蒻
【分析】
这里,如果直接看得出是排列数的大佬可以不用费时间了,因为本蒟蒻看不出来,就说说看不出来怎么破
我们可以根据题目,得到,它相当于是求一个
如果会 dp 的大犇们这里可以列出一个转移方程了:
而答案为不超过
根据 dp 转移方程的定义,我们可以推出答案其实就是
当然,你很快就会发现, dp 的复杂度连
那我们来看一下数据范围,应该是对于每次询问都要
我们来考虑一下:
1.当
2.当
3.当
4.当
5.当
其实,到这里,眼尖的小伙伴已经看出来了,除了
因此,答案为
对答案仍有疑意的小伙伴,可以用数学归纳法证一下,这里就不浪费篇幅证明了,应该是挺显然的
如果我们是数学竞赛,直接输出
不过很可惜,我们不是
我们得考虑如何化简,根据
那么,当
预处理也是很简单的,我们对于
而求逆元,我们只要求
int &me=LRJ;
me->"想一想,为什么?";
【代码】
那本蒟蒻就放 我码风极丑的 代码了:
#include<cstdio>
#include<cstring>
using namespace std;
#define f(a,b,c) for(register int a=b;a<=c;a++)
#define g(a,b,c) for(register int a=b;a>=c;a--)
#define Max(a,b) ((a>b)?a:b)
#define Min(a,b) ((a<b)?a:b)
#define File(s) freopen(s".in","r",stdin);freopen(s".out","w",stdout)
typedef long long int ll;
typedef unsigned long long int ull;
const int Mod=19491001;
inline ll read(){
register ll ans=0;register char c=getchar();register bool neg=0;
while((c<'0')|(c>'9')) neg^=!(c^'-'),c=getchar();
while((c>='0')&(c<='9')) ans=(ans<<3)+(ans<<1)+(c^48),c=getchar();
return neg?-ans:ans;
}//条件反射的结果
ll ar_d_Frac[Mod],ar_d_Afrc[Mod];
void exgcd(int a,int b,ll &x,ll &y){
if(b==0){ x=1; y=0; return ; }
exgcd(b,a%b,x,y);
ll tmp=x;
x=y;
y=tmp-a/b*y;
}
inline ll ny(int a){
ll x,y;
exgcd(a,Mod,x,y);
return (x%Mod+Mod)%Mod;
}
inline void pre(){
ar_d_Frac[0]=ar_d_Frac[1]=ar_d_Afrc[0]=ar_d_Afrc[1]=1;
f(i,2,19491000) ar_d_Frac[i]=ar_d_Frac[i-1]*i%Mod;
ar_d_Afrc[19491000]=ny(ar_d_Frac[19491000]);
g(i,19490999,2) ar_d_Afrc[i]=ar_d_Afrc[i+1]*(i+1)%Mod;
}
inline int c(int n,int m){
if(n<m) return 0;
return ar_d_Frac[n]*ar_d_Afrc[m]%Mod*ar_d_Afrc[n-m]%Mod;
}
int lucas(ll n,ll m){
ll ans=1;
while(n&&m&&ans){
//ans==0时不论乘什么都不会变了,没必要算下去
ans*=c(n%Mod,m%Mod);
ans%=Mod;
n/=Mod;
m/=Mod;
}
return ans;
}
int main(){
pre();
int d_T=read();
while(d_T--){
ll d_N=read(),d_M=read();
printf("%d\n",lucas(d_N+d_M,d_M));
}
return 0;
}