【SPOJ】Power Modulo Inverted(拓展BSGS)
【SPOJ】Power Modulo Inverted(拓展BSGS)
题面
洛谷
求最小的\(y\)
满足
\]
题解
拓展\(BSGS\)模板题
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
using namespace std;
#define ll long long
#define RG register
inline int read()
{
RG int x=0,t=1;RG char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
const int HashMod=123456;
struct HashTable
{
struct Line{int u,v,next;}e[100000];
int h[HashMod],cnt;
void Add(int u,int v,int w){e[++cnt]=(Line){w,v,h[u]};h[u]=cnt;}
void clear(){memset(h,0,sizeof(h));cnt=0;}
void Insert(int x,int i)
{
int k=x%HashMod;
Add(k,i,x);
}
int Query(int x)
{
for(int i=h[x%HashMod];i;i=e[i].next)
if(e[i].u==x)return e[i].v;
return -1;
}
}Hash;
int fpow(int a,int b,int MOD)
{
int s=1;
while(b){if(b&1)s=1ll*s*a%MOD;a=1ll*a*a%MOD;b>>=1;}
return s;
}
void NoAnswer(){puts("No Solution");}
void ex_BSGS(int y,int z,int p)
{
if(z==1){puts("0");return;}
int k=0,a=1;
while(233)
{
int d=__gcd(y,p);if(d==1)break;
if(z%d){NoAnswer();return;}
z/=d;p/=d;++k;a=1ll*a*y/d%p;
if(z==a){printf("%d\n",k);return;}
}
Hash.clear();
int m=sqrt(p)+1;
for(int i=0,t=z;i<m;++i,t=1ll*t*y%p)Hash.Insert(t,i);
for(int i=1,tt=fpow(y,m,p),t=1ll*a*tt%p;i<=m;++i,t=1ll*t*tt%p)
{
int B=Hash.Query(t);if(B==-1)continue;
printf("%d\n",i*m-B+k);return;
}
NoAnswer();
}
int main()
{
int x,z,k;
while(233)
{
x=read();z=read();k=read();
if(x==0&&z==0&&k==0)break;
ex_BSGS(x,k,z);
}
return 0;
}
【SPOJ】Power Modulo Inverted(拓展BSGS)的更多相关文章
- 「SPOJ 3105」Power Modulo Inverted
「SPOJ 3105」Power Modulo Inverted 传送门 题目大意: 求关于 \(x\) 的方程 \[a^x \equiv b \;(\mathrm{mod}\; p) \] 的最小自 ...
- MOD - Power Modulo Inverted(SPOJ3105) + Clever Y(POJ3243) + Hard Equation (Gym 101853G ) + EXBSGS
思路: 前两题题面相同,代码也相同,就只贴一题的题面了.这三题的意思都是求A^X==B(mod P),P可以不是素数,EXBSGS板子题. SPOJ3105题目链接:https://www.spoj. ...
- spoj3105 MOD - Power Modulo Inverted(exbsgs)
传送门 关于exbsgs是个什么东东可以去看看yyb大佬的博客->这里 //minamoto #include<iostream> #include<cstdio> #i ...
- 【POJ 3243】Clever Y 拓展BSGS
调了一周,我真制杖,,, 各种初始化没有设为1,,,我当时到底在想什么??? 拓展BSGS,这是zky学长讲课的课件截屏: 是不是简单易懂.PS:聪哥说“拓展BSGS是偏题,省选不会考,信我没错”,那 ...
- 数论之高次同余方程(Baby Step Giant Step + 拓展BSGS)
什么叫高次同余方程?说白了就是解决这样一个问题: A^x=B(mod C),求最小的x值. baby step giant step算法 题目条件:C是素数(事实上,A与C互质就可以.为什么?在BSG ...
- [拓展Bsgs] Clever - Y
题目链接 Clever - Y 题意 有同余方程 \(X^Y \equiv K\ (mod\ Z)\),给定\(X\),\(Z\),\(K\),求\(Y\). 解法 如题,是拓展 \(Bsgs\) 板 ...
- 数学:拓展BSGS
当C不是素数的时候,之前介绍的BSGS就行不通了,需要用到拓展BSGS算法 方法转自https://blog.csdn.net/zzkksunboy/article/details/73162229 ...
- 【POJ3243】拓展BSGS(附hash版)
上一篇博文中说道了baby step giant step的方法(简称BSGS),不过对于XY mod Z = K ,若x和z并不互质,则不能直接套用BSGS的方法了. 为什么?因为这时候不存在逆元了 ...
- 【POJ3243】【拓展BSGS】Clever Y
Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, ...
随机推荐
- java lang(Comparable接口) 和java util(Comparator接口)分析比较
//Comparable 接口强行对实现它的每个类的对象进行整体排序. -- 自然排序.类的compareTo称为自然比较方法. public interface Comparable<T> ...
- [转帖]SAP一句话入门:Production Planning
SAP一句话入门:Production Planning http://blog.vsharing.com/MilesForce/A617692.html SAP是庞大的,模块是多多的,功能是强大的, ...
- 994.Contiguous Array 邻近数组
描述 Given a binary array, find the maximum length of a contiguous subarray with equal number of 0 and ...
- 网络编程--使用TCP协议发送接收数据
package com.zhangxueliang.tcp; import java.io.IOException; import java.io.OutputStream; import java. ...
- 二叉搜索树的第k个节点
给定一棵二叉搜索树,请找出其中的第k小的结点.例如, (5,3,7,2,4,6,8) 中,按结点数值大小顺序第三小结点的值为4. = =一看就想到中序遍历 public class Soluti ...
- java学习之—并归排序
/** * 并归排序 * Create by Administrator * 2018/6/26 0026 * 下午 5:13 **/ public class DArray { private lo ...
- ASP.NET4.0所有网页指令
ASP.NET网页指令(Page Directive)就是在网页开头的标签声明: <% Page Language="C#" %> 而指令的作用在于指定网页和用户控件编 ...
- scrapy的一些容易忽视的点(模拟登陆,传递item等)
scrapy爬虫注意事项 一.item数据只有最后一条 这种情况一般存在于对标签进行遍历时,将item对象放置在了for循环的外部.解决方式:将item放置在for循环里面. 二.item字段传递 ...
- PHP的特质Trait使用
参考: Trait的使用,网站地址https://www.jianshu.com/p/fc053b2d7fd1
- vue-resource: jsonp请求百度搜索的接口
1. yarn add vue-resource 2. main.js引入vue-resource import Vue from 'vue' import MintUI from 'mint-ui' ...