【BZOJ1467/2480】Pku3243 clever Y/Spoj3105 Mod

Description

已知数a,p,b,求满足a^x≡b(mod p)的最小自然数x。

Input

    每个测试文件中最多包含100组测试数据。
    每组数据中,每行包含3个正整数a,p,b。
    当a=p=b=0时,表示测试数据读入完全。

Output

    对于每组数据,输出一行。
    如果无解,输出“No Solution”(不含引号),否则输出最小自然数解。

Sample Input

5 58 33
2 4 3
0 0 0

Sample Output

9
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的更多相关文章

  1. 【EX_BSGS】BZOJ1467 Pku3243 clever Y

    1467: Pku3243 clever Y Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 425  Solved: 238[Submit][Status ...

  2. bzoj1467 Pku3243 clever Y

    1467: Pku3243 clever Y Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 313  Solved: 181[Submit][Status ...

  3. bzoj 1467: Pku3243 clever Y 扩展BSGS

    1467: Pku3243 clever Y Time Limit: 4 Sec  Memory Limit: 64 MB[Submit][Status][Discuss] Description 小 ...

  4. poj 3243 Clever Y && 1467: Pku3243 clever Y【扩展BSGS】

    扩展BSGS的板子 对于gcd(a,p)>1的情况 即扩展BSGS 把式子变成等式的形式: \( a^x+yp=b \) 设 \( g=gcd(a,p) \) 那么两边同时除以g就会变成: \( ...

  5. 【POJ】3243 Clever Y

    http://poj.org/problem?id=3243 题意:求$a^y \equiv b \pmod{p}$最小的$y$.(0<=x, y, p<=10^9) #include & ...

  6. 【数论】【ex-BSGS】poj3243 Clever Y

    用于求解高次同余方程A^x≡B(mod C),其中C不一定是素数. http://blog.csdn.net/tsaid/article/details/7354716 这篇题解写得最好. 那啥,这题 ...

  7. 【iOS开发】iOS CGRectGetMaxX/Y 使用

    在iOS的界面布局中我们可以使用CGRectGetMaxX 这个方法来方便的获取当前控件的x坐标值+宽度的数值,这样便可以方便布局. 同理CGRectGetMaxY是获取y坐标值+控件高度的值,当然这 ...

  8. 【小知识】比较 x^y 和 y^x 的大小

    往前翻几个编号相邻的题目翻到了这么一道题,感觉很好奇就做了一下 (upd:我下午问了下出题人做法,他就把题隐藏了……这不太友好啊……所以我补一下题意:) 题意 给你两个整数 $x$ 和 $y$,求 $ ...

  9. 【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) ...

随机推荐

  1. 洛谷 [P3455] ZAP

    莫比乌斯函数 #include <iostream> #include <cstdio> #include <cmath> #include <cstring ...

  2. pstack

    pstree  linux 查看进程树 和 包含的线程 pstack 显示每个进程的栈跟踪

  3. Servlet 2.4 规范之第三篇:Servlet生命周期

        SRV.2.3    Servlet生命周期 servlet有着定义良好且明确的生命周期,它定义了servlet以怎样的方式加载和实例化.初始化.怎样处理客户端请求.以及怎样停止服务.生命周期 ...

  4. js-禁止微信内置浏览器调整字体大小

    js方法: (function(){      if(typeof WeixinJSBridge=="object"&& typeof WeixinJSBridge ...

  5. Hdoj 3506 Monkey Party

    Discription Far away from our world, there is a banana forest. And many lovely monkeys live there. O ...

  6. iOS Mobile Development: Using Xcode Targets to Reuse the Code 使用xcode targets来实现代码复用

    In the context of iOS mobile app development, a clone is simply an app that is based off another mob ...

  7. [Android] 环境配置之Android Studio开发NDK

    分类:Android环境搭建 (14351)  (20) ========================================================作者:qiujuer博客:bl ...

  8. win10 sublime license

    —– BEGIN LICENSE —– TwitterInc 200 User License EA7E-890007 1D77F72E 390CDD93 4DCBA022 FAF60790 61AA ...

  9. NYOJ 722 数独 【DFS】+【预处理】

    数独 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描写叙述 数独是一种运用纸.笔进行演算的逻辑游戏.玩家须要依据9×9盘面上的已知数字,推理出全部剩余空格的数字,并满足每一 ...

  10. css控制打印时只显示指定区域

      CreateTime--2017年9月26日08:16:04 Author:Marydon css控制打印时只显示指定区域 思路: 1.使用打印命令@media print: 2.控制执行打印命令 ...