题目:http://www.lydsy.com/JudgeOnline/problem.php?id=2219

N次剩余+CRT。。。

就是各种奇怪的分类讨论。。

#include<cstring>
#include<iostream>
#include<cstdio>
#include<map>
#include<cmath>
#include<algorithm>
#define rep(i,l,r) for (int i=l;i<=r;i++)
#define down(i,l,r) for (int i=l;i>=r;i--)
#define clr(x,y) memset(x,y,sizeof(x))
#define maxn 50050
#define inf 2000000000
using namespace std;
int a,b,k,p,t,ans;
map<int,int> mp;
int read(){
int x=,f=; char ch=getchar();
while (!isdigit(ch)) {if (ch=='-') f=-; ch=getchar();}
while (isdigit(ch)) {x=x*+ch-''; ch=getchar();}
return x*f;
}
int Pow(int x,int y){
int ans=;
while (y){
if (y&) ans=1LL*ans*x;
x=1LL*x*x; y>>=;
}
return ans;
}
int Pow(int x,int y,int mo){
int ans=; while (y){
if (y&) ans=1LL*ans*x%mo;
x=1LL*x*x%mo; y>>=;
}
return ans;
}
int divs[maxn],dtot;
int groot(int p){//是对于p-1的每一个素因子a去检查(p-1)/a!!
dtot=;
int q=p-;
for (int i=;i*i<=q;i++){
if (q%i==) {
divs[++dtot]=i;
if (i*i!=q) divs[++dtot]=q/i;
}
}
for (int i=;i<p;i++){
int ok=;
rep(j,,dtot) if (Pow(i,divs[j],p)==) {ok=;break;}
if (!ok) return i;
}
}
int exgcd(int a,int b,int &x,int &y){
if (b==) {
x=; y=; return a;
}
int d=exgcd(b,a%b,y,x);
y-=a/b*x;
return d;
}
int gcd(int a,int b){
if (b==) return a;
return gcd(b,a%b);
}
int NI(int a,int b){
int x,y;
exgcd(a,b,x,y);
return (x%b+b)%b;
}
int bsgs(int a,int b,int p){
mp.clear();
int m=sqrt(p)+,mul,tmp;
int now=; rep(i,,m-) {if (!mp.count(now)) mp[now]=i; now=1LL*now*a%p;}
now=; mul=Pow(a,m,p);
rep(i,,m-){
tmp=1LL*b*NI(now,p)%p;
if (mp.count(tmp)) return mp[tmp]+i*m;
now=1LL*now*mul%p;
if (now==) break;
}
return inf;
}
int solve(int a,int b,int p,int d){
int pd=Pow(p,d);
b=b%pd;
if (b==) return Pow(p,d-((d-)/a+));
else {
int bet=;
while (b%p==) bet++,b/=p;
if (bet%a!=) return ;
int tt=bet/a;
int g=groot(p),phi=pd-pd/p;
int ind=bsgs(g,b,pd);
int d=gcd(a,phi);
if (ind%d==) return d*Pow(p,(a-)*tt);
else return ;
}
}
int main(){
t=read();
while (t--){
a=read(); b=read(); k=read();
p=*k+;
ans=;
for (int i=;i*i<=p&&ans;i++) if ((p%i)==) {
int now=; while ((p%i)==) p/=i,now++;
ans=ans*solve(a,b,i,now);
}
if (p!=&&ans) ans=ans*solve(a,b,p,);
printf("%d\n",ans);
}
return ;
}

BZOJ 2219: 数论之神的更多相关文章

  1. BZOJ 2219 数论之神 (CRT推论+BSGS+原根指标)

    看了Po神的题解一下子就懂了A了! 不过Po神的代码出锅了-solve中"d-temp"并没有什么用QwQQwQQwQ-应该把模数除以p^temp次方才行. 来自BZOJ讨论板的h ...

  2. 【BZOJ】【2219】数论之神

    中国剩余定理+原根+扩展欧几里得+BSGS 题解:http://blog.csdn.net/regina8023/article/details/44863519 新技能get√: LL Get_yu ...

  3. bzoj2219: 数论之神

    #include <iostream> #include <cstdio> #include <cstring> #include <cmath> #i ...

  4. BZOJ2219数论之神——BSGS+中国剩余定理+原根与指标+欧拉定理+exgcd

    题目描述 在ACM_DIY群中,有一位叫做“傻崽”的同学由于在数论方面造诣很高,被称为数轮之神!对于任何数论问题,他都能瞬间秒杀!一天他在群里面问了一个神题: 对于给定的3个非负整数 A,B,K 求出 ...

  5. BZOJ2219 数论之神 数论 中国剩余定理 原根 BSGS

    原文链接https://www.cnblogs.com/zhouzhendong/p/BZOJ2219.html 题目传送门 - BZOJ2219 题意 求同余方程 $x^A\equiv B \pmo ...

  6. 牛客国庆集训派对Day5 数论之神

    题目描述 终于活成了自己讨厌的样子. 这是她们都还没长大的时候发生的故事.那个时候,栗子米也不需要为了所谓的爱情苦恼. 她们可以在夏日的午后,花大把的时间去研究生活中一些琐碎而有趣的事情,比如数论. ...

  7. BZOJ 4815 数论

    今年的重庆省选? 具体就是,对于每次修改,A[p,q]这个位置,  设d=gcd(p,q) ,则 gcd为d的每一个格子都会被修改,且他们之间有个不变的联系 A[p,q]/p/q==A[k,t]/k/ ...

  8. BZOJ 1037 生日聚会(神DP)

    这题的DP很难想,定义dp[i][j][a][b]表示用了i个男生,j个女生,任一连续的后缀区间内,男生比女生最多多a人,女生比男生最多多b人. 转移就是显然了. # include <cstd ...

  9. bzoj 1406 数论

    首先问题的意思就是在找出n以内的所有x^2%n=1的数,那么我们可以得到(x+1)(x-1)=y*n,那么我们知道n|(x+1)(x-1),我们设n=a*b,那么我们对于任意的a,我们满足n%a==0 ...

随机推荐

  1. iOS学习——内存泄漏检查及原因分析

    项目的代码很多,前两天老大突然跟我说项目中某一个ViewController的dealloc()方法没有被调用,存在内存泄漏问题,需要排查原因,解决内存泄漏问题.由于刚加入项目组不久,对出问题的模块的 ...

  2. Python学习日记:day5-------dict字典

    #字典dict------->唯一的映射类型 1.数据类型的划分 数据类型划分为可变数据类型和不可变数据类型. 不可变数据类型:tupe(元组).bool.int.str           可 ...

  3. windows server 2008 R2服务器安装IIS并添加网站

    一.连接远程计算机 1.因为我的电脑是win7系统,故这里以win7为例,其他windows系统大同小异,首先点开开始菜单栏,在windows附件下找到远程桌面连接 或者采用通用的方法,利用快捷键wi ...

  4. geoserver发布地图服务WMTS

    WMTS: 切片地图web服务(OpenGIS Web Map Tile Service) WMTS提供了一种采用预定义图块方法发布数字地图服务的标准化解决方案.WMTS弥补了WMS不能提供分块地图的 ...

  5. Who Will Win?

    Gautam and Subhash are two brothers. They are similar to each other in all respects except one. They ...

  6. ecshop 属性表(attribute)商品属性表(goods_attr)货品表(prduct) 商品数量的联系

    ecshop 属性表(attribute)商品属性表(goods_attr)货品表(prduct) 商品数量的联系 一个商城的商品属性存放在属性表(attribute)里 ,每个商品对应的属性在goo ...

  7. lesson - 12 课程笔记

    一.w 命令 作用: 用于显示已经登录系统的用户列表, 并显示用户正在执行的指令. 执行这个命令可得知目前登入系统的用户有哪些人, 以及他们正在执行的程序.  单独执行w 命令会显示所有的用户, 您也 ...

  8. LVS-DR集群搭建

    安装LVS 下载源码包,安装时需要根据自己的内核,下载 ipvsadm-1.26.tar.gz的源码包,在进行编译安装以后,我们需要检查必需包是否安装: 1.对内核文件做链接 # uname -r 2 ...

  9. Cleaner, more elegant, and wrong(msdn blog)

    Cleaner, more elegant, and wrong Just because you can't see the error path doesn't mean it doesn't e ...

  10. Qt数据库集成应用封装

    平时的大大小小的项目中,基本上都需要与数据库打交道,会遇到各种各样的应用场景,本人主要遇到四种场景1:数据库自动重连,例如mysql数据库中经常遇到服务器挂掉的情况,需要自动检测并重新连接数据库.2: ...