T2  扩展BSGS

bzoj 3283 扩展BSGS + 快速阶乘-LMLPHP

T3 快速阶乘

给定整数n,质数p和正整数c,求整数s和b,满足n! / p = s mod p

考虑每次取出floor(n/p)个p因子,然后将问题转化为子问题。

 /**************************************************************
Problem: 3283
User: idy002
Language: C++
Result: Accepted
Time:1704 ms
Memory:12380 kb
****************************************************************/ #include <cstdio>
#include <cstring>
#include <cmath> typedef long long dnt;
void exgcd( dnt a, dnt b, dnt &d, dnt &x, dnt &y ) {
if( b== ) {
x=, y=, d=a;
} else {
exgcd(b,a%b,d,y,x);
y-=a/b*x;
}
}
dnt inv( int a, int n ) {
dnt d, x, y;
exgcd(a,n,d,x,y);
return d== ? (x%n+n)%n : -;
}
dnt mpow( dnt a, dnt b, dnt c ) {
dnt rt;
for( rt=; b; b>>=,a=a*a%c )
if( b& ) rt=rt*a%c;
return rt;
} namespace Task1 {
void sov( dnt a, dnt b, dnt c ) {
printf( "%lld\n", mpow(a,b,c) );
}
};
namespace Task2 {
const int mod = ;
const int len = mod<<;
struct Hash {
int head[mod], val[len], rat[len], next[len], ntot;
void init() {
ntot=;
memset( head, , sizeof(head) );
}
void insert( int v, int r ) {
int k = v % mod;
ntot++;
next[ntot] = head[k];
val[ntot] = v;
rat[ntot] = r;
head[k] = ntot;
}
int query( int v ) {
int k = v % mod;
for( int t=head[k]; t; t=next[t] )
if( val[t]==v ) return rat[t];
return -;
}
}hash; dnt gcd( dnt a, dnt b ) {
return b ? gcd(b,a%b) : a;
}
int bsgs( dnt s, dnt a, dnt b, dnt c ) {
hash.init();
int m = ceil(sqrt(c));
for( int i=; i<m; i++ ) {
if( s==b ) return i;
hash.insert( s, i );
s = s*a % c;
}
dnt am = ;
for( int i=; i<m; i++ )
am = am*a % c;
am = inv(am,c);
b = b*am % c;
for( int i=m; i<c; i+=m ) {
int j = hash.query( b );
if( j!=- ) return i+j;
b = b*am % c;
}
return -;
}
int exbsgs( dnt a, dnt b, dnt c ) {
dnt s = ;
for( int i=; i<; i++ ) {
if( s==b ) return i;
s = s*a % c;
}
dnt cd;
s = ;
int rt = ;
while( (cd=gcd(a,c))!= ) {
rt++;
s*=a/cd;
if( b%cd ) return -;
b/=cd;
c/=cd;
s%=c;
}
int p = bsgs(s,a,b,c);
if( p==- ) return -;
return rt + p;
}
void sov( int a, int b, int c ) {
int res = exbsgs(a,b,c);
if( res==- ) printf( "Math Error\n" );
else printf( "%d\n", res );
}
};
namespace Task3 {
struct Pair {
int s, k;
Pair( int s, int k ):s(s),k(k){}
};
dnt aa[], mm[];
dnt pres[];
dnt pp[], cc[], ppp[], tot; dnt china( int n, dnt *a, dnt *m ) {
int M=;
for( int i=; i<n; i++ )
M *= m[i];
int rt = ;
for( int i=; i<n; i++ ) {
dnt Mi = M/m[i];
rt = (rt+Mi*inv(Mi,m[i])*a[i]) % M;
}
return rt;
}
void init( int p, int pp ) {
pres[] = ;
for( int i=; i<=pp; i++ ) {
if( i%p== ) {
pres[i] = pres[i-];
} else {
pres[i] = pres[i-]*i % pp;
}
}
}
Pair split( int n, int p, int c, int pp ) {
int b = n/p;
if( b== ) {
return Pair( pres[n], );
} else {
Pair pr = split( b, p, c, pp );
return Pair( (pr.s*pres[n%pp]%pp) * mpow(pres[pp],n/pp,pp) % pp, pr.k+b );
}
}
void sov( int m, int n, int c ) {
tot = ;
for( int i=; i*i<=c; i++ ) {
if( c%i== ) {
pp[tot] = i;
cc[tot] = ;
ppp[tot] = ;
while( c%i== ) {
cc[tot]++;
ppp[tot] *= pp[tot];
c/=i;
}
tot++;
}
}
if( c!= ) {
pp[tot] = c;
cc[tot] = ;
ppp[tot] = c;
tot++;
c = ;
}
for( int i=; i<tot; i++ ) {
init(pp[i],ppp[i]);
Pair pn = split( n, pp[i], cc[i], ppp[i] );
Pair pa = split( m, pp[i], cc[i], ppp[i] );
Pair pb = split( n-m, pp[i], cc[i], ppp[i] );
if( pn.k-pa.k-pb.k >= cc[i] ) {
aa[i] = ;
mm[i] = ppp[i];
} else {
aa[i] = pn.s * (inv(pa.s,ppp[i])*inv(pb.s,ppp[i])%ppp[i]) % ppp[i];
for( int j=; j<pn.k-pa.k-pb.k; j++ )
aa[i] = (dnt) aa[i]*pp[i] % ppp[i];
mm[i] = ppp[i];
}
}
/*
fprintf( stderr, "tot=%d\n", tot );
for( int i=0; i<tot; i++ )
fprintf( stderr, "%d %d\n", aa[i], mm[i] );
*/
printf( "%lld\n", china(tot,aa,mm) );
}
}; int main() {
int n;
scanf( "%d", &n );
for( int i=,opt,y,z,p; i<=n; i++ ) {
scanf( "%d%d%d%d", &opt, &y, &z, &p );
if( opt== )
Task1::sov( y, z, p );
else if( opt== )
Task2::sov( y, z, p );
else
Task3::sov( y, z, p );
}
}
05-11 11:03