POJ 3243 Clever Y (求X^Y mod Z = K)
来源:互联网 发布:java中求最大公约数 编辑:程序博客网 时间:2024/05/12 08:35
Clever Y
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 7301 Accepted: 1807
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 outK fast. However, given X, Z, K, could you figure outY fast?
Input
Input data consists of no more than 20 test cases. For each test case, there would be only one line containing 3 integersX, 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 feasibleY (0 ≤ Y < Z). Otherwise output the minimum Y you find.
Sample Input
5 58 332 4 30 0 0
Sample Output
9No Solution
套模板
#include<stdio.h>#include<string.h>#include<math.h>#include <iostream>#include<stdlib.h>#include <algorithm>using namespace std;#define CC(m ,what) memset(m , what , sizeof(m))typedef __int64 LL ;LL A, B ,C ;const int NN = 99991 ;bool hash[NN] ;int idx[NN] , val[NN] ;void insert(int id , LL vv){ LL v = vv % NN ; while( hash[v] && val[v]!=vv) { v++ ; if(v == NN) v-=NN ; } if( !hash[v] ) { hash[v] = 1; val[v] = vv ; idx[v] = id ; }}int find(LL vv){ LL v = vv % NN ; while( hash[v] && val[v]!=vv) { v++ ; if(v == NN) v-=NN ; } if( !hash[v] ) return -1; return idx[v] ;}void ex_gcd(LL a , LL b , LL& x , LL& y){ if(b == 0) { x = 1 ; y = 0 ; return ; } ex_gcd(b , a%b , x, y) ; LL t = x ; x = y; y = t - a/b*y ;}LL gcd(LL a,LL b){ while( a%b != 0) { LL c = a ; a = b ; b = c % b ; } return b ;}LL baby_step(LL A, LL B , LL C){ LL ans = 1 ; for(LL i=0; i<=50; i++) { if(ans == B) return i ; ans = ans * A % C ; } LL tmp , d = 0 ; LL D = 1 % C ; while( (tmp=gcd(A,C)) != 1 ) { if(B % tmp) return -1 ; d++ ; B/=tmp ; C/=tmp ; D = D*A/tmp%C ; } CC( hash , 0) ; CC( idx, -1) ; CC(val , -1) ; LL M = ceil( sqrt(C*1.0) ) ; LL rr = 1 ; for(int i=0; i<M; i++) { insert(i, rr) ; rr = rr * A % C ; } LL x, y ; for(int i=0; i<M; i++) { ex_gcd(D, C , x, y) ; LL r = x * B % C; r = (r % C + C) % C ; int jj = find( r ) ; if(jj != -1) { return LL(i)*M + LL(jj) + d ; } D = D * rr % C ; } return -1 ;}int main(){ while(scanf("%I64d %I64d %I64d",&A,&C,&B) == 3) { if(A+B+C == 0 ) break ; LL res = baby_step(A,B,C) ; //A%C==B; if( res == -1 ) { printf("No Solution\n"); continue ; } printf("%I64d\n",res); } return 0 ;}
0 0
- POJ 3243 Clever Y (求X^Y mod Z = K)
- POJ 3243 Clever Y 求A^X = B (mod C) / BabyStep_GiantStep
- POJ 3243: Clever Y 求A^X = B (mod C) / BabyStep_GiantStep
- POJ 3243 Clever Y
- poj 3243 Clever Y
- poj 3243 Clever Y&&hdu 2815 Mod Tree(扩展baby_step)
- POJ 3243 Clever Y BSGS
- POJ 3243:Clever Y BSGS
- 【EXT-BSGS算法求离散对数】POJ Clever Y 3243
- hdu4282 x^z+y^z+x*y*z=k 解的个数
- 求(x-y+z)*2
- POJ 3243 Clever Y BSGS 算法 (模板)
- 为了使平面3*x-k*y-3z+16=0与曲面3*x^2+y^2+z^2=16相切,求k
- 算法分析:x+y=x|y,求k小y
- 计算函数F(x,y,z)=(x+y)/(x-y)+(z+y)/(z-y)的值
- 笔试面试13--给定 x, k ,求满足 x + y = x | y 的第 k 小的正整数 y
- z = 16*x + y
- x > y ? y : x > z ? z : x;
- Java线程进入
- message_flood
- 下拉刷新效果
- 常见的HTTP状态码
- iOS App键盘第一次启动延迟问题
- POJ 3243 Clever Y (求X^Y mod Z = K)
- js判断是否定义
- leetcode之Add Digits
- Java 多线程常用概念
- leetcode-101-Symmetric Tree
- poj--1256
- 黑马程序员_java 操作文件的扩展,制作excel表格
- CYC-集合视图 UICollectionView的简单使用
- PopupWindow