Regardless of the length <Program Number F = x a (x), and (F (x) -F (x-1)) * x contribution to the answer in
n squares approach can be directly DP,
Feel equation at all,
Launch formula: Similarly coat, each of length i of the i th calculation.
Then the inclusion-exclusion under:
F is the number of the program, he asked:
Number of segments divided enumeration, the number i exceeds enumeration for inclusion and exclusion:
Breakthrough: There is a ni * k-1, means that i * k <= n, so i k violence and enumerate total nlogn complexity !
Proposed, considering kill j
Forcing the formula:
deal with:
(How to see how I can not see what the truth looks like)
Meaning it came to a combination of:
There are n-ik balls, we start with the j-th selected, and then select from the k selected in the j. In j balls, we selected a special ball for the rest of the balls m 1-color staining.
Consider discussing this particular ball is not a ball in the k
Can be obtained;
(Here less write C (ni * kk, k))
Power pretreatment m-1 and m-th power of the inverse element factorial factorial
O (nlogn)
Remember Finally n * m ^ n-
#include<bits/stdc++.h> #define reg register int #define il inline #define fi first #define se second #define mk(a,b) make_pair(a,b) #define numb (ch^'0') #define pb push_back #define solid const auto & #define enter cout<<endl #define pii pair<int,int> using namespace std; typedef long long ll; template<class T>il void rd(T &x){ char ch;x=0;bool fl=false;while(!isdigit(ch=getchar()))(ch=='-')&&(fl=true); for(x=numb;isdigit(ch=getchar());x=x*10+numb);(fl==true)&&(x=-x);} template<class T>il void output(T x){if(x/10)output(x/10);putchar(x%10+'0');} template<class T>il void ot(T x){if(x<0) putchar('-'),x=-x;output(x);putchar(' ');} template<class T>il void prt(T a[],int st,int nd){for(reg i=st;i<=nd;++i) ot(a[i]);putchar('\n');} int mod; namespace Modulo{ int ad(int x,int y){return (x+y)>=mod?x+y-mod:x+y;} void inc(int &x,int y){x=ad(x,y);} int mul(int x,int y){return (ll)x*y%mod;} void inc2(int &x,int y){x=mul(x,y);} int qm(int x,int y=mod-2){int ret=1;while(y){if(y&1) ret=mul(x,ret);x=mul(x,x);y>>=1;}return ret;} } using namespace Modulo; namespace Miracle{ const int N=300000+5; int n,m; int jie[N],inv[N]; int iv[N]; int m0[N],m1[N]; int C(int n,int m){ if(n<0||m<0||n<m) return 0; return mul(jie[n],mul(inv[m],inv[n-m])); } int main(){ rd(n);rd(m);rd(mod); jie[0]=1; for(reg i=1;i<=n;++i) jie[i]=mul(jie[i-1],i); inv[n]=qm(jie[n],mod-2); for(reg i=n-1;i>=0;--i) inv[i]=mul(inv[i+1],i+1); iv[1]=1; for(reg i=2;i<=n;++i){ iv[i]=mul(mod-mod/i,iv[mod%i]); } m0[0]=m1[0]=1; for(reg i=1;i<=n;++i){ m0[i]=mul(m0[i-1],m); m1[i]=mul(m1[i-1],m-1); } int ans=0; for(reg i=0;i<n;++i){ for(reg k=0;k<=n;++k){ if(i*k+k>n) break; int tmp=0;// if(k!=0) tmp=ad(tmp,mul(C(n-i*k,k),mul(k,mul(m1[k-1],m0[n-i*k-k])))); if(n-i*k-k-1>=0) tmp=ad(tmp,mul(C(n-i*k,k),mul(m1[k],mul(m0[n-i*k-k-1],n-i*k-k)))); tmp=mul(tmp,(k&1)?mod-1:1); tmp=mul(tmp,iv[n-i*k]); ans=ad(ans,tmp); } } ans=mul(ans,m); ans=ad(mul(n,qm(m,n)),mod-ans); ot(ans); return 0; } } signed main(){ Miracle::main(); return 0; } /* Author: *Miracle* */
Is a breakthrough i, k of the total number of nlogn level, kill j with a combination of the strong sense of derivation