Description

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

XY mod Z = K

Given X, Y, Z, we all know how to figure out K fast. However, given X, Z, K, 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 X, Z, K (0 ≤ X, Z, K ≤ 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

【分析】
时间卡得真紧TAT,被T出翔。
后面用HASH + 链表才过...
 /*
五代李煜
《浪淘沙令·帘外雨潺潺》
帘外雨潺潺,春意阑珊。罗衾不耐五更寒。梦里不知身是客,一晌贪欢。
独自莫凭栏,无限江山,别时容易见时难。流水落花春去也,天上人间。
*/
#include <iostream>
#include <cstdio>
#include <ctime>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <set>
#include <vector>
#define LOCAL
const int MAXN = + ;
const int maxn = ;//用来HASH
const int INF = 0x7fffffff;
using namespace std;
typedef long long ll;
struct Hash{
ll a,b,next;
}Hash[maxn << ];
ll flg[maxn + ];
ll top,idx;
void ins(ll a,ll b){
ll k = b & maxn;
if (flg[k] != idx){
flg[k] = idx;
Hash[k].next = -;
Hash[k].a = a;
Hash[k].b = b;
return ;
}
while (Hash[k].next != -){
if(Hash[k].b == b) return ;
k = Hash[k].next;
}
Hash[k].next = ++ top;
Hash[top].next = -;
Hash[top].a = a;
Hash[top].b = b;
}
ll find(ll b){
ll k = b & maxn;
if (flg[k] != idx) return -;
while (k != -){
if(Hash[k].b == b) return Hash[k].a;
k = Hash[k].next;
}
return -;
}
ll gcd(ll a,ll b) {return b == ? a: gcd(b, a % b);}
ll exgcd(ll a, ll b, ll &x, ll &y){
if (b == ){x = ; y = ; return a;}
ll tmp = exgcd(b, a % b, y, x);
y -= x * (a / b);
return tmp;
}
//求解线性同余方程 形如Ax = B(mod C)
ll solve(ll a, ll b, ll c){
ll x, y, Ans;
ll tmp = exgcd(a, c, x, y);
Ans = (ll)(x * b) % c;
return Ans >= ? Ans : Ans + c;
}
ll pow(ll a, ll b, ll c){
if (b == ) return a % c;
ll tmp = pow(a, b / , c);
if (b % == ) return (tmp * tmp) % c;
else return (((tmp * tmp) % c) * a) % c;
}
ll BSGS(ll A, ll B, ll C){
top = maxn;
++idx;
ll buf = % C, D = buf, K, tmp;
for (ll i = ; i <= ; i++){
if (buf == B) return i;
buf = (buf * A) % C;
}
ll d = ;
while ((tmp = gcd(A, C)) != ){
if (B % tmp != ) return -;
d++;
B /= tmp;
C /= tmp;
D = D * A / tmp % C;
}
//hash表记录1-sqrt(c)的值
ll M = (ll)ceil(sqrt(C * 1.0));
buf = % C;
for (ll i = ; i <= M; i++){
ins(i, buf);
buf = (buf * A) % C;
}
K = pow(A, M, C);
for (ll i = ; i <= M; i++){
tmp = solve(D, B, C);
ll w;
if (tmp >= && (w = find(tmp)) != -) return i * M + w + d;
D = (D * K) % C;
}
return -;
} int main(){ ll A, B, C;
while (scanf("%lld%lld%lld", &A, &C, &B) != EOF){
if (A == && C == && B == ) break;
B %= C;
ll tmp = BSGS(A, B, C);
if (tmp >= ) printf("%lld\n", tmp);
else printf("No Solution\n");
}
return ;
}

【POJ3243】【拓展BSGS】Clever Y的更多相关文章

  1. [拓展Bsgs] Clever - Y

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

  2. 【POJ 3243】Clever Y 拓展BSGS

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

  3. poj3243 Clever Y[扩展BSGS]

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

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

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

  5. bzoj 1467: Pku3243 clever Y 扩展BSGS

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

  6. 数学:拓展BSGS

    当C不是素数的时候,之前介绍的BSGS就行不通了,需要用到拓展BSGS算法 方法转自https://blog.csdn.net/zzkksunboy/article/details/73162229 ...

  7. 数论之高次同余方程(Baby Step Giant Step + 拓展BSGS)

    什么叫高次同余方程?说白了就是解决这样一个问题: A^x=B(mod C),求最小的x值. baby step giant step算法 题目条件:C是素数(事实上,A与C互质就可以.为什么?在BSG ...

  8. 【SPOJ】Power Modulo Inverted(拓展BSGS)

    [SPOJ]Power Modulo Inverted(拓展BSGS) 题面 洛谷 求最小的\(y\) 满足 \[k\equiv x^y(mod\ z)\] 题解 拓展\(BSGS\)模板题 #inc ...

  9. bzoj1467 Pku3243 clever Y

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

  10. 【BZOJ1467/2480】Pku3243 clever Y/Spoj3105 Mod EXBSGS

    [BZOJ1467/2480]Pku3243 clever Y/Spoj3105 Mod Description 已知数a,p,b,求满足a^x≡b(mod p)的最小自然数x. Input      ...

随机推荐

  1. 【转】windows常用消息大全(系统消息、通告消息、用户消息)

    原文网址:http://blog.csdn.net/nupt123456789/article/details/7370562 附录A Windows 常用消息大全 表A-1  Windows消息分布 ...

  2. 游戏开发设计模式之对象池模式(unity3d 示例实现)

    前篇:游戏开发设计模式之命令模式(unity3d 示例实现) 博主才学尚浅,难免会有错误,尤其是设计模式这种极富禅意且需要大量经验的东西,如果哪里书写错误或有遗漏,还请各位前辈指正. 原理:从一个固定 ...

  3. HDOJ 1716 排列2 next_permutation函数

    Problem Description Ray又对数字的列产生了兴趣: 现有四张卡片,用这四张卡片能排列出很多不同的4位数,要求按从小到大的顺序输出这些4位数. Input 每组数据占一行,代表四张卡 ...

  4. C语言实现两栈空间共享

    一个同学让我改一段两栈共享的C语言代码,实现进栈.出栈.输出栈里元素的功能. 代码如下: #include <stdio.h> #include <stdlib.h> #def ...

  5. CPP变量参数别名

    1,变量起"绰号"的操作称为引用(reference),"绰号"称为引用名,申明引用的语法格式; 变量数据类型 &引用名 = 已申明的变量名; 和C中的 ...

  6. jQuery各种效果举例

    jQuery 所有jQuery详细使用说明请见:http://www.php100.com/manual/jquery/ jQuery的作用是操作浏览器html,从而达到用户的可视化效果,按照功能可分 ...

  7. 关于日历控件My97DatePicker 在IE6下出现“无法打开站点,已终止操作”

    1.My97DatePicker 官方:http://www.my97.net2.在IE6下出现“无法打开站点,已终止操作”的解决办法(转): .这是一个绝对有效的方法,但是会丢失跨越iframe的特 ...

  8. DEDECMS5.7 首页和栏目页调用文章按权重排序

    dedecms 5.7版本已增加按权重排序功能: [arclist]标签增加按权重排序,在后台管理DEDE里找到以下目录\include\taglib中的arclist.lib.php文件并打开 大约 ...

  9. MySQL注入总结

    SELECT first_name, last_name FROM users WHERE user_id = '$id' 1.id=1' or 1=1 --     这个可以查询所有的信息,其中“- ...

  10. 原生JavaScript的省市县三级联动

    三级联动是我们写表单时必不可少的,比如在写收货地址时,就用到他了,最近在看原生JavaScript,从基础写起,待完善,以后再写个jquery版的. <!DOCTYPE html> < ...