【BZOJ1467/2480】Pku3243 clever Y/Spoj3105 Mod EXBSGS
【BZOJ1467/2480】Pku3243 clever Y/Spoj3105 Mod
Description
Input
Output
Sample Input
2 4 3
0 0 0
Sample Output
No Solution
HINT
100%的数据,a,p,b≤1e9。
题解:EXBSGS
因为A和C不互质,所以我们令A,B,C同时除以gcd(A,C),得到的C'可能与A还不互质,所以我们再除上gcd(A,C'),直到A,C互质。
此时得到方程$A^{x-k}*{A^k\over g_1*g_2...g_k}=B'(mod C')$
将${A^k\over g_1*g_2...g_k}$除过去,然后套用BSGS即可。
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <cmath>
using namespace std;
typedef long long ll;
map<int,int> mp;
int pm(int a,int b,int c)
{
int ret=1;
while(b)
{
if(b&1) ret=(ll)ret*a%c;
a=(ll)a*a%c,b>>=1;
}
return ret;
}
int gcd(int a,int b)
{
return b?gcd(b,a%b):a;
}
int BSGS(int a,int b,int c,int d)
{
int x,y,i;
mp.clear();
int M=ceil(sqrt(c));
for(x=b,i=0;i<=M;i++,x=(ll)x*a%c) mp[x]=i;
for(y=d,x=pm(a,M,c),i=1;i<=M;i++)
{
y=(ll)y*x%c;
int tmp=mp[y];
if(tmp) return (ll)i*M-tmp;
}
return -1;
}
void work(int a,int b,int c)
{
int A=1,k=0;
for(int i=0;(1<<i)<=c;i++)
{
if(pm(a,i,c)==b)
{
printf("%d\n",i);
return ;
}
}
while(1)
{
int g=gcd(a,c);
if(g==1) break;
if(b%g!=0)
{
printf("No Solution\n");
return ;
}
b/=g,c/=g,A=((ll)A*a/g)%c,k++;
}
int tmp=BSGS(a,b,c,A);
if(tmp==-1) printf("No Solution\n");
else printf("%d\n",tmp+k);
}
int main()
{
int a,b,c;
while(1)
{
scanf("%d%d%d",&a,&c,&b);
if(!a&&!b&&!c) return 0;
if(!a&&!b) printf("1\n");
else a%=c,b%=c,work(a,b,c);
}
}
【BZOJ1467/2480】Pku3243 clever Y/Spoj3105 Mod EXBSGS的更多相关文章
- 【EX_BSGS】BZOJ1467 Pku3243 clever Y
1467: Pku3243 clever Y Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 425 Solved: 238[Submit][Status ...
- bzoj1467 Pku3243 clever Y
1467: Pku3243 clever Y Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 313 Solved: 181[Submit][Status ...
- bzoj 1467: Pku3243 clever Y 扩展BSGS
1467: Pku3243 clever Y Time Limit: 4 Sec Memory Limit: 64 MB[Submit][Status][Discuss] Description 小 ...
- poj 3243 Clever Y && 1467: Pku3243 clever Y【扩展BSGS】
扩展BSGS的板子 对于gcd(a,p)>1的情况 即扩展BSGS 把式子变成等式的形式: \( a^x+yp=b \) 设 \( g=gcd(a,p) \) 那么两边同时除以g就会变成: \( ...
- 【POJ】3243 Clever Y
http://poj.org/problem?id=3243 题意:求$a^y \equiv b \pmod{p}$最小的$y$.(0<=x, y, p<=10^9) #include & ...
- 【数论】【ex-BSGS】poj3243 Clever Y
用于求解高次同余方程A^x≡B(mod C),其中C不一定是素数. http://blog.csdn.net/tsaid/article/details/7354716 这篇题解写得最好. 那啥,这题 ...
- 【iOS开发】iOS CGRectGetMaxX/Y 使用
在iOS的界面布局中我们可以使用CGRectGetMaxX 这个方法来方便的获取当前控件的x坐标值+宽度的数值,这样便可以方便布局. 同理CGRectGetMaxY是获取y坐标值+控件高度的值,当然这 ...
- 【小知识】比较 x^y 和 y^x 的大小
往前翻几个编号相邻的题目翻到了这么一道题,感觉很好奇就做了一下 (upd:我下午问了下出题人做法,他就把题隐藏了……这不太友好啊……所以我补一下题意:) 题意 给你两个整数 $x$ 和 $y$,求 $ ...
- 【POJ 2480】Longge's problem(欧拉函数)
题意 求$ \sum_{i=1}^n gcd(i,n) $ 给定 $n(1\le n\le 2^{32}) $. 链接 题解 欧拉函数 $φ(x)$ :1到x-1有几个和x互质的数. gcd(i,n) ...
随机推荐
- UVA 10131 Is Bigger Smarter?(DP最长上升子序列)
Description Question 1: Is Bigger Smarter? The Problem Some people think that the bigger an elepha ...
- ng-include
ng-include可以作为标签或者属性来使用,作用是引入公用文件. <div ng-include="'header.html'"></div> 注意里面 ...
- 模仿世纪佳缘网站PC端的首页效果
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- CSS-实现倒影效果box-reflect
我需要的效果: html: <img src="images/my1.jpg" width="20%"/> css: img{-webkit-b ...
- 深入理解js中的立即执行函数(function(){…})()
javascript和其他编程语言相比比较随意,所以javascript代码中充满各种奇葩的写法,有时雾里看花,当然,能理解各型各色的写法也是对javascript语言特性更进一步的深入理解. ( f ...
- socket的accept: Invalid argument问题
void local_sdk_server::wait_remote_client_connect_and_comm() { /*服务器服务启动,等待客户端的链接的到来*/ //sockaddr_in ...
- sed实战、find实战、grep实战
1.find实战 # 删除指定文件(三种方法) find /data/ -type f -name "*.log" -exec rm {} \; find /data/ -type ...
- 板子-GOD BLESS ALL OF YOU
字符串: KMP #include<cstdio> #include<cstring> ; ]; ]; int l1,l2; void get_next() { next[]= ...
- java三角形和菱形的打印
一.三角形的打印 package 向家康; import java.util.Scanner; public class Main { public void san(int num) { for(i ...
- Myeclipse 出现了Could not generate DH keypair 错误
在myeclipse下安装svn插件,出现了Could not generate DH keypair,这么一个错误. 这个问题困扰了我半天时间,各种百度也找不到答案,或许是百度能力问题吧.百度出来的 ...