Clever Y
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 8666   Accepted: 2155

Description

Little Y finds there is a very interesting formula in mathematics:

XY mod Z = K

Given XYZ, we all know how to figure out K fast. However, given XZK, could you figure out Y fast?

Input

Input data consists of no more than 20 test cases. For each test case, there would be only one line containing 3 integers XZK (0 ≤ XZK ≤ 109). 
Input file ends with 3 zeros separated by spaces. 

Output

For each test case output one line. Write "No Solution" (without quotes) if you cannot find a feasible Y (0 ≤ Y < Z). Otherwise output the minimum Y you find.

Sample Input

5 58 33
2 4 3
0 0 0

Sample Output

9
No Solution

Source

 
//消除因子,在做普通的BSGS
#include<cmath>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long ll;
struct Thash{
static const ll N=1e6+,MOD=;
ll tot,val[N],h[N],next[N],head[MOD+];
void clear(){tot=;memset(head,,sizeof head);}
void insert(ll H,ll VAL){
for(ll i=head[H%MOD];i;i=next[i]) if(h[i]==H){val[i]=VAL;return ;}
h[++tot]=H;val[tot]=VAL;next[tot]=head[H%MOD];head[H%MOD]=tot;
}
ll get(ll H){
for(ll i=head[H%MOD];i;i=next[i]) if(h[i]==H) return val[i];
return -;
}
}M;
ll gcd(ll a,ll b){
if(!b) return a;
return gcd(b,a%b);
}
void exgcd(ll a,ll b,ll &d,ll &x,ll &y){
if(!b){d=a;x=;y=;return ;}
exgcd(b,a%b,d,y,x);
y-=a/b*x;
}
ll inv(ll a,ll p){
ll d,x,y;
exgcd(a,p,d,x,y);
return d==?(x%p+p)%p:-;
}
ll BSGS(ll a,ll b,ll mod){
M.clear();
ll m=(ll)ceil(sqrt(mod+0.5));
ll t=;
for(ll i=;i<m;i++){
M.insert(t,i);
t=t*a%mod;
}
ll base=inv(t,mod);
ll res=b;
for(ll i=,z;i<m;i++){
if((z=M.get(res))!=-) return i*m+z;
res=res*base%mod;
}
return -;
}
ll solve(ll a,ll b,ll mod){
ll A=,D=,cnt=,ans;
for(int i=;i<=;i++){
if(A==b) return i;
A=A*a%mod;
}
for(ll t;(t=gcd(a,mod))!=;){
if(b%t)return -;
b/=t;
mod/=t;
D*=a/t;D%=mod;
cnt++;
}
b=b*inv(D,mod)%mod;
ans=BSGS(a,b,mod);
if(~ans) return ans+cnt;
return -;
}
int main(){
ll a,b,c,ans;
while(~scanf("%lld%lld%lld",&c,&a,&b)){
ans=solve(a,b%c,c);
if(~ans) printf("%lld\n",ans);
else puts("no solution");
}
return ;
}

poj3243 Clever Y[扩展BSGS]的更多相关文章

  1. bzoj 1467: Pku3243 clever Y 扩展BSGS

    1467: Pku3243 clever Y Time Limit: 4 Sec  Memory Limit: 64 MB[Submit][Status][Discuss] Description 小 ...

  2. POJ 3243 Clever Y 扩展BSGS

    http://poj.org/problem?id=3243 这道题的输入数据输入后需要将a和b都%p https://blog.csdn.net/zzkksunboy/article/details ...

  3. luogu2485 [SDOI2011]计算器 poj3243 Clever Y BSGS算法

    BSGS 算法,即 Baby Step,Giant Step 算法.拔山盖世算法. 计算 \(a^x \equiv b \pmod p\). \(p\)为质数时 特判掉 \(a,p\) 不互质的情况. ...

  4. 【POJ 3243】Clever Y 拓展BSGS

    调了一周,我真制杖,,, 各种初始化没有设为1,,,我当时到底在想什么??? 拓展BSGS,这是zky学长讲课的课件截屏: 是不是简单易懂.PS:聪哥说“拓展BSGS是偏题,省选不会考,信我没错”,那 ...

  5. 【数论】【ex-BSGS】poj3243 Clever Y

    用于求解高次同余方程A^x≡B(mod C),其中C不一定是素数. http://blog.csdn.net/tsaid/article/details/7354716 这篇题解写得最好. 那啥,这题 ...

  6. poj 3243 Clever Y && 1467: Pku3243 clever Y【扩展BSGS】

    扩展BSGS的板子 对于gcd(a,p)>1的情况 即扩展BSGS 把式子变成等式的形式: \( a^x+yp=b \) 设 \( g=gcd(a,p) \) 那么两边同时除以g就会变成: \( ...

  7. [拓展Bsgs] Clever - Y

    题目链接 Clever - Y 题意 有同余方程 \(X^Y \equiv K\ (mod\ Z)\),给定\(X\),\(Z\),\(K\),求\(Y\). 解法 如题,是拓展 \(Bsgs\) 板 ...

  8. bzoj1467 Pku3243 clever Y

    1467: Pku3243 clever Y Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 313  Solved: 181[Submit][Status ...

  9. [POJ 3243]Clever Y

    Description Little Y finds there is a very interesting formula in mathematics: XY mod Z = K Given X, ...

随机推荐

  1. Linux-HUP信号的干扰问题

    在向大家详细介绍Linux HUP信号之前,首先让大家了解下Linux HUP信号,然后全面介绍Linux服务器X,希望对大家有用.想让进程在断开连接后依然保持运行?如果该进程已经开始运行了该如何补救 ...

  2. js操作时间 加法 减法 计算 格式化时间

    Date.prototype.Format = function (fmt) { var o = { "M+": this.getMonth() + 1, //月份 "d ...

  3. 转:浅析VO、DTO、DO、PO的概念、区别和用处

    原文链接 概念: VO(View Object):视图对象,用于展示层,它的作用是把某个指定页面(或组件)的所有数据封装起来. DTO(Data Transfer Object):数据传输对象,这个概 ...

  4. C# Oracle.ManagedDataAccess 批量更新表数据

    这是我第一次发表博客.以前经常到博客园查找相关技术和代码,今天在写一段小程序时出现了问题, 但在网上没能找到理想的解决方法.故注册了博客园,想与新手分享(因为本人也不是什么高手). vb.net和C# ...

  5. sklearn 中模型保存的两种方法

    一. sklearn中提供了高效的模型持久化模块joblib,将模型保存至硬盘. from sklearn.externals import joblib #lr是一个LogisticRegressi ...

  6. 参数数组(params)的用法

    使用参数数组的注意事项: 1. 只能在一维数组上使用params关键字. 2. 不能重载一个只基于params关键字的方法.params关键字不构成方法的签名的一部分. 如: //编译时错误:重复访问 ...

  7. yum安装Apache,Mysql,PHP

    用yum安装Apache,Mysql,PHP.  用yum安装Apache,Mysql,PHP. 2.1安装Apache yum install httpd httpd-devel 安装完成后,用/e ...

  8. [elk]elasticsearch5.0及head插件安装

    ElasticSearch2.3/2.4升级到ElasticSearch5.0 参考文档(排名不分先后)https://www.elastic.co/guide/en/elasticsearch/re ...

  9. vue路由使用

    <body> <div id="itany"> <div> <!--使用router-link组件来定义导航,to属性指定链接url--& ...

  10. jquery几个按钮同时调用一个方法