BZOJ1467_Pku3243 clever Y_EXBSGS

Description

小Y发现,数学中有一个很有趣的式子: X^Y mod Z = K 给出X、Y、Z,我们都知道如何很快的计算K。但是如果给出X、Z、K,你是否知道如何快速的计算Y呢?

Input

本题由多组数据(不超过20组),每组测试数据包含一行三个整数X、Z、K(0 <= X, Z, K <= 109)。 输入文件一行由三个空格隔开的0结尾。

Output

对于每组数据:如果无解则输出一行No Solution,否则输出一行一个整数Y(0 <= Y < Z),使得其满足XY mod Z = K,如果有多个解输出最小的一个Y。

Sample Input

5 58 33
2 4 3
0 0 0

Sample Output

9
No Solution


exbsgs模板题,用来处理模数非质数的情况。

具体实现比较简单,注意前num个数需要枚举。

挖坑待填...

代码:

#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <map>
#include <math.h>
using namespace std;
typedef long long ll;
map<ll,int>mp;
void exgcd(ll a,ll b,ll &x,ll &y,ll &p) {
if(!b) {x=1; y=0; p=a; return ;}
exgcd(b,a%b,y,x,p);
y-=a/b*x;
}
ll gcd(ll x,ll y) {
return y?gcd(y,x%y):x;
}
ll EXBSGS(ll a,ll b,ll n) {
if(n==1) return b==0?0:-1;
if(a%n==0) return b==0?0:1;
mp.clear();
ll r,D=1,num=0,now,base=1;
while((r=gcd(a,n))>1) {
if(b%r) return -1;
num++; b/=r; n/=r; D=D*a/r%n;
}
int i;
for(i=0,now=1;i<num;i++,now=now*a%n)
if(now==b) return i;
ll m=ceil(sqrt(n));
for(i=0;i<m;i++) {
if(!mp.count(base)) mp[base]=i;
base=base*a%n;
}
ll x,y,p;
for(i=0;i<m;i++) {
exgcd(D,n,x,y,p);
x=(x*b%n+n)%n;
if(mp.count(x)) return i*m+mp[x]+num;
D=D*base%n;
}
return -1;
}
int main() {
ll a,b,n;
while(scanf("%lld%lld%lld",&a,&n,&b)!=EOF) {
if(a==0&&b==0&&n==0) return 0;
ll ans=EXBSGS(a,b,n);
if(ans==-1) puts("No Solution");
else printf("%lld\n",ans);
}
}

BZOJ1467_Pku3243 clever Y_EXBSGS的更多相关文章

  1. 11 Clever Methods of Overfitting and how to avoid them

    11 Clever Methods of Overfitting and how to avoid them Overfitting is the bane of Data Science in th ...

  2. bzoj 1467: Pku3243 clever Y 扩展BSGS

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

  3. [拓展Bsgs] Clever - Y

    题目链接 Clever - Y 题意 有同余方程 \(X^Y \equiv K\ (mod\ Z)\),给定\(X\),\(Z\),\(K\),求\(Y\). 解法 如题,是拓展 \(Bsgs\) 板 ...

  4. 算法训练 Tricky and Clever Password

     算法训练 Tricky and Clever Password   时间限制:2.0s   内存限制:256.0MB      问题描述 在年轻的时候,我们故事中的英雄——国王 Copa——他的私人 ...

  5. 2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 F题 Clever King(最小割)

    2018 ACM-ICPC 中国大学生程序设计竞赛线上赛:https://www.jisuanke.com/contest/1227 题目链接:https://nanti.jisuanke.com/t ...

  6. bzoj1467 Pku3243 clever Y

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

  7. poj3243 Clever Y[扩展BSGS]

    Clever Y Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 8666   Accepted: 2155 Descript ...

  8. 【EX_BSGS】BZOJ1467 Pku3243 clever Y

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

  9. 【BZOJ1467/2480】Pku3243 clever Y/Spoj3105 Mod EXBSGS

    [BZOJ1467/2480]Pku3243 clever Y/Spoj3105 Mod Description 已知数a,p,b,求满足a^x≡b(mod p)的最小自然数x. Input      ...

随机推荐

  1. Map 遍历分析

    1. 阐述 对于Java中Map的遍历方式,很多文章都推荐使用entrySet,认为其比keySet的效率高很多.理由是:entrySet方法一次拿到所有key和value的集合:而keySet拿到的 ...

  2. Day3_函数

    为啥要用到函数: 复杂度增大 组织结构不清晰 可读性差 工具就是具备某一种功能的物件,就是程序中函数的概念. 事先准备工具的过程称为函数的定义 遇到特定的场景拿来用就称为函数的调用 函数的分类: 内置 ...

  3. ubuntu导入公钥的方法

    导入公钥的办法: #方法1: gpg --keyserver subkeys.pgp.NET --recv 6E871C4A881574DEgpg --export --armor 6E871C4A8 ...

  4. Manacher算法解析

    Manacher算法 Manachar算法主要是处理字符串中关于回文串的问题的,它可以在 O(n) 的时间处理出以字符串中每一个字符为中心的回文串半径,由于将原字符串处理成两倍长度的新串,在每两个字符 ...

  5. Python2和Python3比较分析

    一直有看到网上有讨论Python2和Python3的比较,最近公司也在考虑是否在spark-python大数据开发环境中升级到python3.通过本篇博文记录Python2.7.13和Pthon3.5 ...

  6. 团队项目第二阶段个人进展——Day4

    一.昨天工作总结 冲刺第四天,分析完成后端处理的数据有主题,时间,地点,照片信息,前几个都是字符串类型,后一个是照片格式 二.遇到的问题 照片格式数据不知道怎么处理 三.今日工作规划 学习后端小程序后 ...

  7. python_code list_2

    >>> import math>>> math.sin(0.5)0.479425538604203>>> >>> import ...

  8. codeforces 979D Kuro and GCD and XOR and SUM

    题意: 给出两种操作: 1.添加一个数字x到数组. 2.给出s,x,k,从数组中找出一个数v满足gcd(x,k) % v == 0 && x + v <= s && ...

  9. git merge 撤销与git 回滚

    git merge提交后恢复 http://blog.psjay.com/posts/git-revert-merge-commit/ git回滚 https://www.jianshu.com/p/ ...

  10. new/delete和malloc/free的比较

    从C++角度上说,使用new分配堆空间可以调用类的构造函数,而malloc()函数仅仅是一个函数调用,它不会调用构造函数,它所接受的参数是一个unsigned long类型.同样,delete在释放堆 ...