bzoj 3283 扩展BSGS + 快速阶乘
T2 扩展BSGS

T3 快速阶乘
给定整数n,质数p和正整数c,求整数s和b,满足n! / pb = s mod pc
考虑每次取出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 );
}
}
bzoj 3283 扩展BSGS + 快速阶乘的更多相关文章
- bzoj 2480——扩展BSGS
题意 给定 $a,b$ 和模数 $p$,求整数 $x$ 满足 $a^x \equiv b(mod \ p)$,不保证 $a,p$ 互质. (好像是权限题,可见洛谷P4195 分析 之前讲过,可以通过 ...
- bzoj 3283: 运算器 扩展Baby Step Giant Step && 快速阶乘
3283: 运算器 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 184 Solved: 59[Submit][Status][Discuss] D ...
- poj 3243 Clever Y && 1467: Pku3243 clever Y【扩展BSGS】
扩展BSGS的板子 对于gcd(a,p)>1的情况 即扩展BSGS 把式子变成等式的形式: \( a^x+yp=b \) 设 \( g=gcd(a,p) \) 那么两边同时除以g就会变成: \( ...
- BSGS与扩展BSGS
BSGS \(BSGS\)算法又称大步小步\((Baby-Step-Giant-Step)\)算法 \(BSGS\)算法主要用于解以下同余方程 \[A^x\equiv B(mod\ p)\]其中\(( ...
- BSGS&扩展BSGS
BSGS 给定\(a,b,p\),求\(x\)使得\(a^x\equiv b \pmod p\),或者说明不存在\(x\) 只能求\(\gcd(a,p)=1\)的情况 有一个结论:如果有解则必然存在\ ...
- POJ 3243 Clever Y 扩展BSGS
http://poj.org/problem?id=3243 这道题的输入数据输入后需要将a和b都%p https://blog.csdn.net/zzkksunboy/article/details ...
- BSGS和扩展BSGS
BSGS: 求合法的\(x\)使得\(a ^ x \quad mod \quad p = b\) 先暴力预处理出\(a^0,a^1,a^2.....a^{\sqrt{p}}\) 然后把这些都存在map ...
- 扩展BSGS求解离散对数问题
扩展BSGS用于求解axΞb mod(n) 同余方程中gcd(a,n)≠1的情况 基本思路,将原方程转化为a与n互质的情况后再套用普通的BSGS求解即可 const int maxint=((1< ...
- bzoj 2242: [SDOI2011]计算器 BSGS+快速幂+扩展欧几里德
2242: [SDOI2011]计算器 Time Limit: 10 Sec Memory Limit: 512 MB[Submit][Status][Discuss] Description 你被 ...
随机推荐
- 使用RegSetValueEx修改注册表时遇到的问题(转)
原文转自 http://blog.csdn.net/tracyzhongcf/article/details/4076870 1.今天在使用RegSetValueEx时发现一个问题: RegSetVa ...
- 解决阿里云无法正常使用samba的问题【转】
转自:https://blog.csdn.net/u011949148/article/details/54311288 昨天在阿里云上申请了一个云服务器,系统用的是ubuntu14.04,由于是免费 ...
- int、long、long long取值范围
unsigned int 0-4294967295 int -2147483648-2147483647 unsigned long 0-4294967295 long -214748 ...
- Team Foundation Server 2010服务器安装
本安装指南使用Windows Server 2008企业版为基础,安装Windows Server 2008 SP2(必须),在此操作系统环境上进行TFS2010的安装与配置. 三.系统用户设置 1. ...
- jumpserver安装教程
centos7系统一步一步安装jumpserver 参照官方文档,查找了百度所有的文档,基本上都是按照官方的文档操作的 官方文档点我-> 安装jumpserver需注意: 1:网络环境要好,有的 ...
- 缓存数据库-redis安装和配置
一:redis安装 python操作redis分为两部分,一为安装redis程序 二是安装支持python操作redis的模块 1)安装redis redis 官方网站:http://www.redi ...
- java基础69 JavaScript产生伪验证码(网页知识)
1.伪验证码 <!doctype html> //软件版本:DW2018版 <html> <head> <meta charset="utf-8&q ...
- redis主从,哨兵(windows版)
一.下载 由于redis官方并不支持windows操作系统,所以官网上是下不到的,需要到gitlab上下载,下载地址如下: https://github.com/MicrosoftArchive/re ...
- Ubuntu 16.04 使用docker资料汇总与应用docker安装caffe并使用Classifier(ros kinetic+usb_cam+caffe)
Docker是开源的应用容器引擎.若想简单了解一下,可以参考百度百科词条Docker.好像只支持64位系统. Docker官网:https://www.docker.com/ Docker - 从入门 ...
- SQLServer系统变量使用
1.@@IDENTITY返回最后插入的标识值.这个变量很有用,当你插入一行数据时,想同时获得该行的的ID(标示列),就可以用@@IDENTITY示例:下面的示例向带有标识列的表中插入一行,并用 @@I ...