1467: Pku3243 clever Y

Time Limit: 4 Sec  Memory Limit: 64 MB
Submit: 425  Solved: 238
[Submit][Status][Discuss]

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

题解

EX_BSGS模板题

思路懒得写了

代码

//by 减维
#include<set>
#include<map>
#include<queue>
#include<ctime>
#include<cmath>
#include<bitset>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define ll long long
#define il inline
#define rg register
#define db double
#define mpr make_pair
#define maxn
#define inf (1<<30)
#define eps 1e-8
#define pi 3.1415926535897932384626L
using namespace std; inline int read()
{
int ret=;bool fla=;char ch=getchar();
while((ch<''||ch>'')&&ch!='-')ch=getchar();
if(ch=='-'){fla=;ch=getchar();}
while(ch>=''&&ch<=''){ret=ret*+ch-'';ch=getchar();}
return fla?-ret:ret;
} ll x,y,p;
map<ll,ll> mp; ll gcd(ll x,ll y){return y==?x:gcd(y,x%y);} ll ksm(ll x,ll y,ll p)
{
ll ret=;x%=p;
for(;y;y>>=,x=x*x%p)
if(y&) ret=ret*x%p;
return ret;
} ll exbsgs(ll x,ll y,ll p)
{
x%=p;y%=p;
if(!x&&!y) return ;
if(y==) return ;
if(!x) return -;
ll t,d=,k=;
while((t=gcd(p,x))!=)
{
if(y%t) return -;
y/=t,p/=t,d=d*x/t%p,k++;
if(d==y) return k;
}
mp.clear();
ll m=sqrt(p+0.5);
ll o=y;
for(int i=;i<=m;++i,o=o*x%p)
if(!mp.count(o)) mp[o]=i;
ll tmp=ksm(x,m,p);
for(int i=;i<=m;++i)
{
d=d*tmp%p;
if(mp.count(d)) return ((i*m-mp[d])%p+k+p)%p;
}
return -;
} int main()
{
while()
{
scanf("%lld%lld%lld",&x,&p,&y);
if(!x&&!y&&!p) break;
ll ans=exbsgs(x,y,p);
if(ans==-) printf("No Solution\n");
else printf("%lld\n",ans);
}
return ;
}

【EX_BSGS】BZOJ1467 Pku3243 clever Y的更多相关文章

  1. bzoj1467 Pku3243 clever Y

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

  2. 【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      ...

  3. bzoj 1467: Pku3243 clever Y 扩展BSGS

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

  4. 【BZOJ4385】[POI2015]Wilcze doły 单调栈+双指针法

    [BZOJ4385][POI2015]Wilcze doły Description 给定一个长度为n的序列,你有一次机会选中一段连续的长度不超过d的区间,将里面所有数字全部修改为0.请找到最长的一段 ...

  5. 【理论】X理论、Y理论及Z理论

      道格拉斯·麦格雷戈(Douglas Mcgregor)把对人的基本假设作了区分,即X理论和Y理论.X理论认为:人们总是尽可能地逃避工作,不愿意承担责任,因此要想有效地进行管理,实现组织的目标,就必 ...

  6. 【2024】求X到Y之间的整数和

    Time Limit: 3 second Memory Limit: 2 MB [问题描述] 计算X到Y之间的整数和(要求用函数实现).注意输入时X不一定小于Y,且X.Y不一定都是整数. [输入] 两 ...

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

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

  8. 【bzoj4385】[POI2015]Wilcze doły

    单调队列扫描,记录当前区间长度为d的一段的和的最大值,和当前区间和. #include<algorithm> #include<iostream> #include<cs ...

  9. 【bzoj1597】[Usaco2008 Mar]土地购买

    1597: [Usaco2008 Mar]土地购买 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 3739  Solved: 1376[Submit] ...

随机推荐

  1. https及其背后的加密原理阅读总结

    https是以安全为目标的http通道,简单讲是http的安全版.当我们往服务器发送比较隐私的数据(比如说你的银行卡,身份证)时,如果使用http进行通信.那么安全性将得不到保障. 首先数据在传输的过 ...

  2. spring boot 集成swagger2

    1  在pom.xml中加入Swagger2的依赖 <dependency> <groupId>io.springfox</groupId> <artifac ...

  3. 7- vue django restful framework 打造生鲜超市 -商品类别数据展示(上)

    Vue+Django REST framework实战 搭建一个前后端分离的生鲜超市网站 Django rtf 完成 商品列表页 并没有将列表页的数据json 与前端的页面展示结合起来 讲解如果将dr ...

  4. 问题:Could not install packages due to an EnvironmentError: [Errno 13] Permission denied:

    1.安装django 执行pip3 install --user django 2.解决方法:加--user   执行pip3 install --user django

  5. 常用的几个JQuery代码片段

    1. 导航菜单背景切换效果 在项目的前端页面里,相对于其它的导航菜单,激活的导航菜单需要设置不同的背景.这种效果实现的方式有很多种,下面是使用JQuery实现的一种方式: //注意:代码需要修饰完善 ...

  6. 常用自写函数[更新ing]

    int gcd (int x, int y)//最大公约数 { return y == 0 ? x : gcd( y , x % y ); } int lcm(int x, int y)//最小公倍数 ...

  7. Lake Counting(dfs)

    Description Due to recent rains, water has pooled in various places in Farmer John's field, which is ...

  8. 精通SpringBoot--整合Redis实现缓存

    今天我们来讲讲怎么在spring boot 中整合redis 实现对数据库查询结果的缓存.首先第一步要做的就是在pom.xml文件添加spring-boot-starter-data-redis.要整 ...

  9. 从0到n-1中随机等概率输出m个不同的数

    //假设输入的n远大于m void knuth(int n, int m) { for (int i = 0; i < n; i++) { if (rand() % (n - i)<m) ...

  10. 最短路径(最基础,经典的模板和思想):HDU-2544最短路

    题目: 最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Su ...