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. LoadRunner监控Windows和Linux常见问题

    LoadRunner 加载监听服务器的步骤如下: 1.在 LoadRunner Controller 下,将工作面板切换到 Run状态,Available Graphs 栏 ,System Resou ...

  2. java IO复习(二)

    package com.zyw.file; import java.io.*; /** * Created by zyw on 2016/3/10. */ public class FileTest2 ...

  3. 《Linear Algebra and Its Applications》-chaper4-向量空间-子空间、零空间、列空间

    在线性代数中一个非常重要的概念就是向量空间R^n,这一章节将主要讨论向量空间的一系列性质. 一个向量空间是一些向量元素构成的非空集合V,需要满足如下公理: 向量空间V的子空间H需要满足如下三个条件: ...

  4. linux —— 学习笔记(用户管理与权限控制)

    目录:1.用户的创建和管理    2.组的创建和管理 3.文件执行权限的控制 4.不用密码执行sudo 1.用户的创建和管理 用户的创建和管理: useradd.usermod . userdel . ...

  5. Mina学习之IoBuffer

    IoBuffer是一个被MINA体系所使用的字节数组.它是ByteBuffer的替代品,Mina不使用NIO的ByteBuffer有两个原因: 1. ByteBuffer没有提供更多有用的api,如f ...

  6. SQL 按月统计(两种方式) 分类: SQL Server 2014-08-04 15:36 154人阅读 评论(0) 收藏

    (1)Convert 函数 select Convert ( VARCHAR(7),ComeDate,120) as Date ,Count(In_code) as 单数,Sum(SumTrueNum ...

  7. linux操作系统cron详解

    Linux操作系统定时任务系统 Cron 入门 cron是一个linux下的定时执行工具,可以在无需人工干预的情况下运行作业.由于Cron 是Linux的内置服务,但它不自动起来,可以用以下的方法启动 ...

  8. GWT中实现跳转及不同entrypoint怎么互相访问

    怎么跳转? 跳转这个概念这里指的是从一个web页面跳转到另一个web页面,如果我们使用gwt来开发web,很自然的我们会想到怎么从一个gwt做的页面跳转到另一个gwt做的页面. 但从网上的gwt例子来 ...

  9. MST(Kruskal’s Minimum Spanning Tree Algorithm)

    You may refer to the main idea of MST in graph theory. http://en.wikipedia.org/wiki/Minimum_spanning ...

  10. SQL语法集锦一:SQL语句实现表的横向聚合

    本文转载:http://www.cnblogs.com/lxblog/archive/2012/09/29/2708128.html 问题描述:假如有一表结构和数据如下: C1 C2 C3 C4 C5 ...