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. C语言-结构体内存对齐

    C语言结构体对齐也是老生常谈的话题了.基本上是面试题的必考题.内容虽然很基础,但一不小心就会弄错.写出一个struct,然后sizeof,你会不会经常对结果感到奇怪?sizeof的结果往往都比你声明的 ...

  2. Linux-使用 screen 管理你的远程会话

    转自:http://www.ibm.com/developerworks/cn/linux/l-cn-screen/ 你是不是经常需要 SSH 或者 telent 远程登录到 Linux 服务器?你是 ...

  3. linuxshell中"2>&1"含义

    http://blog.sina.com.cn/s/blog_652819220100wpvu.html

  4. 解决——CSS :before、:after ,当content使用中文时有时候会出现乱码

    问题: 在进行页面开发时,经常会使用:before, :after伪元素创建一些小tips,但是在:before或:after的content属性使用中文的话,会导致某些浏览器上出现乱码. 例如我遇到 ...

  5. 360极速浏览器无法正确getHours

    开发了一个显示时间的功能. 功能是这样的,首先从数据库中读取时间,然后显示到前端页面上. 数据库中:2017-08-10 14:25:35.867 前端显示:2017-08-10T14:25:35.8 ...

  6. MySQL主从(MySQL proxy Lua读写分离设置,一主多从同步配置,分库分表方案)

    Mysql Proxy Lua读写分离设置 一.读写分离说明 读写分离(Read/Write Splitting),基本的原理是让主数据库处理事务性增.改.删操作(INSERT.UPDATE.DELE ...

  7. python-求直角三角形斜边

    设计一个求直角三角形斜边长的函数(两条直角边为参数,求最长边) 如果直角边边长分分别为3和4,那么返回的结果应该像这样: The right triangle third side's length ...

  8. unity, 不要用TextMesh,用图片代替

    <方块鸭快跑>(见:http://www.cnblogs.com/wantnon/p/4596222.html)1.0版本开始界面中鸭子的speech bubble中的文字用的是TextM ...

  9. unity, unlit surface shader (texColor only surface shader)

    要实现双面透明无光照只有纹理色的surface shader. 错误的写法:(导致带有曝光) Shader "Custom/doubleFaceTranspTexColor" { ...

  10. atitit.流程标准化--- mysql启动不起来的排查流程attilax总结

    atitit.流程标准化--- mysql启动不起来的排查流程attilax总结 1. mysql的启动日志文件 1 2. console方式 1 3. 安装为服务 1 3.1. 使用默认配置文件 1 ...