题目: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. Java用代码获取classes目录下的文件的方式

    假设我们有个Maven的Web项目,其中有src/main/java和src/main/resources两个输出目录(注意这两个目录下的内容运行后都会输出到classes目录下), 而在src/ma ...

  2. 637. Average of Levels in Binary Tree

    Given a non-empty binary tree, return the average value of the nodes on each level in the form of an ...

  3. 搭建eclipse+tomcat开发环境

    JDK 1.6 Eclipse IDE For JEE Version Tomcat 6.0 tomcatPluginV33  //eclipse平台上的插件,但它并不是tomcat本身,需要安装独立 ...

  4. Linux第七节随笔-中 /date / ln /

    4.date link 作用:显示或设定系统的日期与时间 参数: -d<字符串> 显示字符串所指的日期与时间.字符串前后必须加上双引号. -s<字符串> 根据字符串来设置日期与 ...

  5. springBoot系列教程03:redis的集成及使用

    1.为了高可用,先安装redis集群 参考我的另一篇文章 http://www.cnblogs.com/xiaochangwei/p/7993065.html 2.POM中引入redis <de ...

  6. 人工智能一:Al学习路线

    想要跨入AI的大门,如何跨?终于找到了一套学习方法 努力向你靠近 2017-12-03 07:14:51 当下人工智能领域的发展已经有了燎原之势,麦肯锡全球研究院就认为人工智能促进对社会的转变速度将比 ...

  7. Odwiedziny[POI 2015]

    题目描述 给定一棵n个点的树,树上每条边的长度都为1,第i个点的权值为a[i]. Byteasar想要走遍这整棵树,他会按照某个1到n的全排列b走n-1次,第i次他会从b[i]点走到b[i+1]点,并 ...

  8. Jenkins 学习笔记(三):我们的JAVA 项目是这么发布的

    发布拓扑 1. 拓扑图 2. 流程说明: Git 插件从 Git Server 上面拉取源代码. Maven 插件将源代码安装我们设定的指令进行编译打包,存放于项目的 WorkSpace. Publi ...

  9. JavaWeb项目中获取对Oracle操作时抛出的异常错误码

    最近在项目中碰到了这么一个需求,一个JavaWeb项目,数据库用的是Oracle.业务上有一个对一张表的操作功能,当时设置了两个字段联合的唯一约束.由于前断没有对重复字段的校验,需要在插入时如果碰到唯 ...

  10. ECMAScript 6新特性简记

    ECMAScript 6.0是JavaScript语言的2015年6月的发布版. 一.let和const命令 let:用来声明变量,用法类似于var,但是只在let命令所在的代码块内有效. var a ...