EXBSGS
http://210.33.19.103/problem/2183
参考:https://blog.csdn.net/frods/article/details/67639410(里面代码好像不太对)(不用求逆元的方法)
https://blog.csdn.net/zzkksunboy/article/details/73162229(要逆元的方法)
参考代码:https://blog.csdn.net/clove_unique/article/details/51227328
以下代码pojA不掉,没有unordered_map,map又T掉;要手写哈希表
https://www.luogu.org/problemnew/show/P3846
https://cn.vjudge.net/problem/POJ-3243
https://cn.vjudge.net/problem/POJ-2417
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<tr1/unordered_map>
using namespace std;
using namespace tr1;
typedef long long LL;
LL poww(LL a,LL b,LL md)
{
LL base=a,ans=;
for(;b;base=base*base%md,b>>=) if(b&) ans=ans*base%md;
return ans;
}
LL logm(LL a,LL b,LL md)
{
if(md==) return -;
if(a%md==) return b?-:(a==);
if(b==) return a?:-;
//printf("a%lld %lld %lld\n",a,b,md);
//以上为特判,记一下
LL g,num=,d=;
while((g=__gcd(a,md))!=)
{
if(b%g) return -;
num++;b/=g;md/=g;d=d*(a/g)%md;
if(b==d) return num;//特殊技巧,记一下;num==0的情况前面已经判了
//设gg为所有已经除过的g的积,原来b为B,此时的b实际相当于B/gg
//d=a^num/gg,如果a^num==B,则d==b
}
//printf("b%lld %lld %lld\n",a,b,md);
LL sz=sqrt(md+0.5),sz1=(md-)/sz+,i,now,t=poww(a,sz,md);//sz块大小,sz1块数
unordered_map<LL,LL> ma;
for(i=,now=b%md;i<sz;i++,now=now*a%md)//x=i*sz-j,因此j属于[0,sz-1]
{
ma[now]=i;//应该使得x最小,则j应越大越好,因此相同的保留最大的
//printf("c%lld %lld\n",i,now);
}
for(i=,now=d;i<=sz1;i++)//因此i属于[1,sz1]
{
now=now*t%md;
if(ma.count(now)) return i*sz-ma[now]+num;
//printf("d%lld\n",i);
}
return -;
}
LL a,md,b,ans;
int main()
{
while(){
scanf("%lld%lld%lld",&a,&md,&b);
if(a==&&md==&&b==) break;
ans=logm(a,b,md);
ans==-?puts("No Solution"):printf("%lld\n",ans);
}
return ;
}
压一下:
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<tr1/unordered_map>
using namespace std;
using namespace tr1;
typedef long long LL;
LL poww(LL a,LL b,LL md)
{
LL base=a,ans=;
for(;b;base=base*base%md,b>>=) if(b&) ans=ans*base%md;
return ans;
}
LL logm(LL a,LL b,LL md)
{
if(md==) return -;
if(a%md==) return b?-:(a==);
if(b==) return a?:-;
LL g,num=,d=;
while((g=__gcd(a,md))!=)
{
if(b%g) return -;
num++;b/=g;md/=g;d=d*(a/g)%md;
if(b==d) return num;
}
LL sz=sqrt(md+0.5),sz1=(md-)/sz+,i,now,t=poww(a,sz,md);
unordered_map<LL,LL> ma;
for(i=,now=b%md;i<sz;i++,now=now*a%md) ma[now]=i;
for(i=,now=d;i<=sz1;i++)
{
now=now*t%md;
if(ma.count(now)) return i*sz-ma[now]+num;
}
return -;
}
LL a,md,b,ans;
int main()
{
while(){
scanf("%lld%lld%lld",&a,&md,&b);
if(a==&&md==&&b==) break;
ans=logm(a,b,md);
ans==-?puts("No Solution"):printf("%lld\n",ans);
}
return ;
}
EXBSGS的更多相关文章
- BZOJ 3122 SDOI2013 随机数发生器 数论 EXBSGS
标题效果:给定一列数X(i+1)=(a*Xi+b)%p 最低要求i>0.所以Xi=t 0.0 这个问题可以1A那很棒 首先讨论特殊情况 如果X1=t ans=1 如果a=0 ans=b==t? ...
- BZOJ2480Spoj3105 Mod&BZOJ1467Pku3243 clever Y——EXBSGS
题目描述 已知数a,p,b,求满足a^x≡b(mod p)的最小自然数x. 输入 每个测试文件中最多包含100组测试数据. 每组数据中,每行包含3个正整数a,p,b. 当a=p ...
- BSGS与exBSGS学习笔记
\(BSGS\)用于解决这样一类问题: 求解\(A^x ≡B(modP)\)的最小\(x\),其中\(P\)为质数. 这里我们采用分块的方法,把\(x\)分解为\(i *t-b\)(其中\(t\)是分 ...
- hdu 2815 Mod Tree (exBSGS)
http://acm.hdu.edu.cn/showproblem.php?pid=2815 //解 K^D ≡ N mod P #include<map> #include<cma ...
- exBSGS·BSGS-Senior/扩展的BSGS
\(\rm{0x01\quad Preface}\) \(emmm\)严格来讲,不应该被算到一个模板里面.因为在我看来模板是人构造出来的,但是这个算法应该是一个解决问题的\(process\)-更像是 ...
- Luogu4195 【模板】exBSGS(exBSGS)
如果a和p互质,用扩欧求逆元就可以直接套用普通BSGS.考虑怎么将其化至这种情况. 注意到当x>=logp时gcd(ax,p)是一个定值,因为这样的话每个存在于a中的质因子,其在ax中的出现次数 ...
- BSGS&EXBSGS 大手拉小手,大步小步走
大步小步走算法处理这样的问题: A^x = B (mod C) 求满足条件的最小的x(可能无解) 其中,A/B/C都可以是很大的数(long long以内) 先分类考虑一下: 当(A,C)==1 即A ...
- exBSGS学习笔记
exBSGS学习笔记 Tags:数学 题目的话就做下洛谷的模板好了 // luogu-judger-enable-o2 #include<algorithm> #include<io ...
- 【模板】exBSGS/Spoj3105 Mod
[模板]exBSGS/Spoj3105 Mod 题目描述 已知数\(a,p,b\),求满足\(a^x\equiv b \pmod p\)的最小自然数\(x\). 输入输出格式 输入格式: 每个测试文件 ...
- MOD - Power Modulo Inverted(SPOJ3105) + Clever Y(POJ3243) + Hard Equation (Gym 101853G ) + EXBSGS
思路: 前两题题面相同,代码也相同,就只贴一题的题面了.这三题的意思都是求A^X==B(mod P),P可以不是素数,EXBSGS板子题. SPOJ3105题目链接:https://www.spoj. ...
随机推荐
- Arcgis Engine(ae)接口详解(6):workspace操作
//此处用的workspace来源与用户选择 IWorkspace workspace = null; //workspace一般等同于数据库 //工作空间类型,也可理解为数据库类型 //esriFi ...
- appium-java-api
AppiumDriver getAppStrings() 默认系统语言对应的Strings.xml文件内的数据. driver.getAppStrings(String language) 查找某一个 ...
- html-基本form元素---ShinePans
<html> <meta http-equiv="content-type" content="text/html;charset=UTF-8" ...
- (Android 即时通讯) [悬赏],不管是谁发现一个漏洞奖励人民币1000元!
悬赏,不管是谁发现一个漏洞奖励人民币1000元! 3Q Android 手机版即时通讯系统正式推出,可与电脑版 地灵(http://im.yunxunmi.com) 即时通讯系统互通! 适用于:小米 ...
- queue_action
http://www.learn4master.com/programming-language/python/python-queue-for-multithreading
- MTK平台下Battery驱动分析
主要涉及代码: Kernel: kernel-3.10\drivers\power\mediatek\ kernel-3.10\drivers\misc\mediatek\mach\mt6580\&l ...
- redis04-----Hash 哈希数据类型相关命令
Hash 哈希数据类型相关命令 hset key field value 这里的域就是键值对的键. 作用: 把key中 filed域的值设为value 注:如果没有field域,直接添加,如果有,则覆 ...
- regmap使用介绍【转】
本文转载自:http://blog.csdn.net/hellowxwworld/article/details/10737569 内核3.1引入一套新的API regmap,目的是提取出关于I2C ...
- monitor.sh java脚本学习
#! /bin/bash# unset any variable which system may be using# clear the screen while getopts ivh named ...
- File.Copy的时候Could not find a part of the path
https://developercommunity.visualstudio.com/content/problem/378265/filecopy-did-not-throw-the-correc ...