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. VueJs(8)---组件(注册组件)

    组件(注册组件) 一.介绍 组件系统是Vue.js其中一个重要的概念,它提供了一种抽象,让我们可以使用独立可复用的小组件来构建大型应用,任意类型的应用界面都可以抽象为一个组件树 那么什么是组件呢? 组 ...

  2. spring mvc和spring的区别

    springmvc只是spring其中的一部分. spring 可以 支持 hibernate ,ibatis ,JMS,JDBC 支持事务管理, 注解功能,表达式语言,测试 springmvc 就是 ...

  3. Mybatis Generator实现分页功能

    Mybatis Generator实现分页功能 分类: IBATIS2013-07-17 17:03 882人阅读 评论(1) 收藏 举报 mybatisibatisgeneratorpage分页 众 ...

  4. docker的安装和技巧

    工作了有一段时间,开发环境中需要docker环境,但是docker一直不算很熟,之前一直是利用yum安装,但是yum安装真的很费劲,所以总结了一些经验给大家: 1,利用yum直接安装 官网是直接给了y ...

  5. spring cloud 入门系列六:使用Zuul 实现API网关服务

    通过前面几次的分享,我们了解了微服务架构的几个核心设施,通过这些组件我们可以搭建简单的微服务架构系统.比如通过Spring Cloud Eureka搭建高可用的服务注册中心并实现服务的注册和发现: 通 ...

  6. 谣传QQ被黑客DDOS攻击,那么Python如何实现呢?

    于2018-5-10日晚 网络流传黑客DDOS攻击了QQ服务器,导致大家聊天发送内容时出现感叹号.我们都知道一般情况下出现感叹号都是你的网络不稳定,或者...别人已经删除你了.然而昨晚很奇怪,发出的内 ...

  7. JSON-RPC远程调用协议

    1. JSON-RPC简介 2. 请求 3. 响应 4. 错误 4.1. 错误对象 4.2. 错误码 5. 批量调用 6. 示例 6.1. 列表形式参数 6.2. key-value形式参数 6.3. ...

  8. 安装ie时,报:此安装不支持您的操作系统的当前语言

    打开注册表(win的"运行"栏键入 regedit 再按 OK )的HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Control/Nls/ ...

  9. Python 处理时间的模块

    1.由日期格式转化为字符串格式的函数为: datetime.datetime.strftime().date() 2.由字符串格式转化为日期格式的函数为: datetime.datetime.strp ...

  10. 洛谷 P2764 解题报告

    P2764 最小路径覆盖问题 问题描述: 给定有向图\(G=(V,E)\).设\(P\) 是\(G\) 的一个简单路(顶点不相交)的集合.如果\(V\) 中每个顶点恰好在\(P\) 的一条路上,则称\ ...