【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) ...
随机推荐
- net4:GridView中的重要操作(添加checkbox,以及鼠标动作,行颜色等)
原文发布时间为:2008-07-29 -- 来源于本人的百度文章 [由搬家工具导入] using System;using System.Data;using System.Configuration ...
- react-native 判断是不是IPhone X
import { Platform, Dimensions } from 'react-native'; // iPhoneX const X_WIDTH = 375; const X_HEIGHT ...
- est6 -- Object.is()、Object.assign()、Object.defineProperty()、Symbol、Proxy
Object.is()用来比较两个值是否严格相等.它与严格比较运算符(===)的行为基本一致,不同之处只有两个:一是+0不等于-0,二是NaN等于自身. + === - //true NaN === ...
- 第三章 poj 1064——关于带精度的二分法
/* 题意:给定n个实数l[i],给定一个k 问:求最大的ans,使得sum l[i]/ans i=1 to n >=k,且ans最大*/ #include <iostream> # ...
- Java多线程中Lock的使用
Jdk1.5以后,在java.util.concurrent.locks包下,有一组实现线程同步的接口和类,说到线程的同步,可能大家都会想到synchronized关键字, 这是java内置的关键字, ...
- BZOJ2002弹飞绵羊
动态树LCT模板题 #include<cstdio> #include<cctype> #include<algorithm> using namespace st ...
- ApplicationContext介绍
如果说BeanFactory是Spring的心脏,那么ApplicationContext就是完整的身躯了.ApplicationContext由BeanFactory派生而来,提供了更多面向实际应用 ...
- GitBook一个专注于帮助文档的工具
官网:https://www.gitbook.com GitHub组织:https://github.com/gitbookio GitBook一个专注于帮助文档的工具,比如: 1.简单的左侧列表右侧 ...
- java后4位打成*显示
/** * [固定电话] 后四位,其他隐藏<例子:****1234> * * @param num * @return */ public static String fixedPhone ...
- 提交IOS开发效率的几个插件(Xcode神器推荐贴)
Code Pilot 2 Xcode上的Command-T,讓你快速跳轉到某個文件或某個符號 XVim 讓Xcode使用Vim的鍵綁定,Vim党必備 Injection for Xcode 調試利器, ...