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. PHP-PHP程序员的技术成长规划(By黑夜路人)

    按照了解的很多PHP/LNMP程序员的发展轨迹,结合个人经验体会,抽象出很多程序员对未来的迷漫,特别对技术学习的盲目和慌乱,简单梳理了这个每个阶段PHP程序员的技术要求,来帮助很多PHP程序做对照设定 ...

  2. CI框架下nginx重写规则

    因为: server { listen ; server_name 222.73.130.124; location / { index index.html index.htm index.php; ...

  3. T-sql for xml path使用

    用法: FOR XML PATH 方法是用于将查询结果集以XML形式展示 sql: p.ContactTypeID,p.ModifiedDate,p.Name from [Person].[Conta ...

  4. C#调用Windows CMD命令并,返回输出结果或错误信息

    public static string InvokeExcute(string Command) { Command = Command.Trim().TrimEnd('&') + &quo ...

  5. Hive 数据倾斜原因及解决方法(转)

    在做Shuffle阶段的优化过程中,遇到了数据倾斜的问题,造成了对一些情况下优化效果不明显.主要是因为在Job完成后的所得到的Counters是整个Job的总和,优化是基于这些Counters得出的平 ...

  6. 轻量集群管理工具PSSH

    PSSH 的意思是 Parallel SSH,并行的SSH,很好理解,PSSH 可以让一条命令在多个服务器上同时执行 这就简化了集群的管理工作,例如想查看一下各台服务器现在的负载状况,就可以通过 PS ...

  7. CentOS7 安装lua环境(我是在mysql读写分离用的)

    下载地址:http://www.lua.org/download.html 安装方法: 依次执行以下命令: curl -R -O http://www.lua.org/ftp/lua-5.3.1.ta ...

  8. Sublime Text 2 和 Verilog HDL

    Sublime Text 2 和 Verilog HDL Date  Fri 04 July 2014 Tags Sublime Text / Vivado Sublime Text 代码编辑器之于程 ...

  9. Nginx日志过滤 使用ngx_log_if不记录特定日志

    ngx_log_if是Nginx的一个第三方模块.它在Github上的描述是这样介绍的:ngx_log_if是一个独立的模块,允许您控制不要写的访问日志,类似于Apache的"CustomL ...

  10. 算法提高 道路和航路 SPFA 算法

    我简单的描述一下题目,题目中所说的有道路和航路: 1.公路是双向的,航路是单向的: 2.公路是正值,航路可正可负: 每一条公路i或者航路i表示成连接城镇Ai(1<=A_i<=T)和Bi(1 ...