POJ3243 Clever Y大步小步法
Posted 海岛Blog
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ3243 Clever Y大步小步法相关的知识,希望对你有一定的参考价值。
Clever Y
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 11799 Accepted: 2932
Description
Little Y finds there is a very interesting formula in mathematics:
X Y m o d Z = K X^Y mod Z = K XYmodZ=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
POJ Monthly–2007.07.08, Guo, Huayang
问题链接:POJ3243 Clever Y
问题简述:(略)
问题分析:大步小步法的模板题,不解释。
程序说明:(略)
参考链接:(略)
题记:(略)
AC的C++语言程序如下:
/* POJ3243 Clever Y */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long LL;
const int MOD =76532;
LL hs[MOD], id[MOD];
int next2[MOD], head[MOD], top;
void Insert(LL x, LL y)
{
int k = x % MOD;
hs[top] = x; id[top] = y;
next2[top] = head[k]; head[k] = top++;
}
LL Find(int x)
{
int k = x % MOD;
for (int i = head[k]; ~i; i = next2[i])
if (x == hs[i]) return id[i];
return -1;
}
LL BSGS(LL a, LL b, LL n)
{
if (b == 1) return 0;
memset(head, -1, sizeof head);
top = 1;
int m = sqrt(n);
LL p = 1, x = 1;
for (int i = 0; i < m; i++) {Insert(p * b % n, i); p = p * a % n;}
for (LL i = m, k; ; i += m) {
if ((k = Find(x = x * p % n)) != -1) return i - k;
if (i > n) break;
}
return -1;
}
int main()
{
LL x, z, k, ans;
while (~scanf("%lld%lld%lld", &x, &z, &k) && (x || z || k)) {
if ((ans = BSGS(x, k, z)) == -1)
puts("No Solution");
else
printf("%lld\\n",ans);
}
return 0;
}
以上是关于POJ3243 Clever Y大步小步法的主要内容,如果未能解决你的问题,请参考以下文章
MOD - Power Modulo Inverted(SPOJ3105) + Clever Y(POJ3243) + Hard Equation (Gym 101853G ) + EXBSGS(示