POJ 3243:Clever Y BSGS
来源:互联网 发布:淘宝收益怎么提现 编辑:程序博客网 时间:2024/05/16 14:39
Clever Y
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 7782 Accepted: 1939
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.
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 332 4 30 0 0
Sample Output
9No Solution
m=sqrt(z),将b^0一直到b^(m-1)次方插入到哈希表中,然后不断枚举i*m,查找是否有x^(i*m)=k*x^(j) mod z。
代码:
#pragma warning(disable:4996)#include <iostream>#include <functional>#include <algorithm>#include <cstring>#include <vector>#include <string>#include <cstdio>#include <cmath>#include <queue>#include <stack>#include <deque>#include <set>#include <map>using namespace std;typedef long long ll;#define INF 0x333f3f3f#define repp(i, n, m) for (int i = n; i <= m; i++)#define rep(i, n, m) for (int i = n; i < m; i++)#define sa(n) scanf("%d", &(n))const int maxn = 1e6 + 7;const double PI = acos(-1.0);const int mod = 1e9 + 7;struct ed{ll next;ll val;ll id;}edge[maxn];int num;int head[maxn];void insert(ll x, ll y){ll k = x%maxn;num++;edge[num].val = x;edge[num].id = y;edge[num].next = head[k];head[k] = num;}ll find(ll x){int k = x%maxn;for (int i = head[k]; i != -1; i = edge[i].next)if (edge[i].val == x)return edge[i].id;return -1;}ll x, z, k;void solve(){ll i, j, m;num = 0;memset(edge, -1, sizeof(edge));memset(head, -1, sizeof(head));m = sqrt(1.0*z);if (k == 1){printf("0\n");return;}ll p = 1;for (i = 0; i < m; i++, p = p*x%z){insert(p*k%z, i);}ll xx = 1;for (i = m;; i += m){ll pos = find(xx = p*xx%z);if (pos != -1){printf("%lld\n", i - pos);return;}if (i > z)break;}printf("No Solution\n");}int main(){#ifndef ONLINE_JUDGE freopen("i.txt", "r", stdin);freopen("o.txt", "w", stdout);#endifwhile (scanf("%lld%lld%lld", &x, &z, &k) != EOF){if (x == 0 && z == 0 && k == 0)break;solve();}return 0;}
0 0
- POJ 3243 Clever Y BSGS
- POJ 3243:Clever Y BSGS
- 【EXT-BSGS算法求离散对数】POJ Clever Y 3243
- POJ 3243 Clever Y BSGS 算法 (模板)
- POJ 3243 Clever Y
- poj 3243 Clever Y
- POJ 3243 Clever Y(扩展BSGS,gcd(a,p)!=1)
- poj3243:Clever Y(BSGS)
- poj3243 Clever Y【BSGS模板】
- BZOJ_P1467/POJ_P3243 Clever Y(扩展BSGS+哈希)
- [BZOJ1467]Pku3243 clever Y(扩展BSGS)
- [BZOJ 1467]Pku3243 clever Y:扩展BSGS
- bzoj 1467: Pku3243 clever Y (扩展BSGS)
- 【bzoj 1467】Pku3243 clever Y(扩展BSGS)
- 【BSGS】POJ2417[Discrete Logging]&POJ3243[Clever Y]题解
- POJ 3243 Clever Y 解高次同余方程
- poj 3243 Clever Y&&hdu 2815 Mod Tree(扩展baby_step)
- POJ 3243 Clever Y Extended-Baby-Step-Giant-Step
- 水声成像之基本概念
- android事件分发之ViewGroup篇
- Rxjava基础自己总结的有不足欢迎探讨
- 类的重写
- 四种常见 Git 工作流比较
- POJ 3243:Clever Y BSGS
- 文本特征提取方法研究
- fastDFS 安装
- java.lang.String的intern()方法
- VIM
- MongoDB入门教程(一)
- mybatis实现拦截器的方法
- mybatis_generator_逆向工程
- nyoj_1 A+B Problem