这个题就是把三个数论基础合在了一起,算是一道比较全面的题.

1的时候就是快速幂

2的时候是exgcd求逆元,特殊的,只有两数互质才有逆元.

3就是bsgs啦,还是不太熟

题干:

Description
你被要求设计一个计算器完成以下三项任务:
、给定y,z,p,计算Y^Z Mod P 的值;
、给定y,z,p,计算满足xy≡ Z ( mod P )的最小非负整数;
、给定y,z,p,计算满足Y^x ≡ Z ( mod P)的最小非负整数。
Input 输入包含多组数据。
第一行包含两个正整数T,K分别表示数据组数和询问类型(对于一个测试点内的所有数据,询问类型相同)。
以下行每行包含三个正整数y,z,p,描述一个询问。
Output
对于每个询问,输出一行答案。对于询问类型2和3,如果不存在满足条件的,则输出“Orz, I cannot find x!”,注意逗号与“I”之间有一个空格。

代码:

#include<iostream>
#include<cstdio>
#include<cmath>
#include<ctime>
#include<queue>
#include<algorithm>
#include<map>
#include<cstring>
using namespace std;
#define duke(i,a,n) for(register int i = a;i <= n;i++)
#define lv(i,a,n) for(register int i = a;i >= n;i--)
#define clean(a) memset(a,0,sizeof(a))
#define int long long
const int INF = << ;
typedef long long ll;
typedef double db;
template <class T>
void read(T &x)
{
char c;
bool op = ;
while(c = getchar(), c < '' || c > '')
if(c == '-') op = ;
x = c - '';
while(c = getchar(), c >= '' && c <= '')
x = x * + c - '';
if(op) x = -x;
}
template <class T>
void write(T x)
{
if(x < ) putchar('-'), x = -x;
if(x >= ) write(x / );
putchar('' + x % );
}
int t,k;
map <int,int> mp;
void work1(int y,int z,int p)
{
int tot = ;
while(z)
{
if(z & )
{
tot *= y;
tot %= p;
}
y *= y;
y %= p;
z >>= ;
}
printf("%lld\n",tot);
}
int exgcd(int a,int b,int &x,int &y)
{
if(b == )
{
x = ;
y = ;
return a;
}
int t = exgcd(b,a % b,y,x);
y -= (a / b * x);
return t;
}
void work2(int y,int z,int p)
{
int x,f;
// z %= p;
int d = exgcd(y,p,x,f);
if(d != )
{
printf("Orz, I cannot find x!\n");
return;
}
x = (x % p + p) % p;
x = (x * z) % p;
/*if(now == p)
printf("Orz, I cannot find x!");
else*/
printf("%lld\n",x);
}
int qpow(int a,int b,int p)
{
int tot = ;
while(b)
{
if(b & )
{
tot *= a;
tot %= p;
}
a *= a;
a %= p;
b >>= ;
}
return tot;
}
void work3(int a,int b,int p)
{
if(a % p == )
{
printf("Orz, I cannot find x!\n");
return;
}
mp.clear();
int m = ceil(sqrt(p));
int now = b % p,ans;
mp[now] = ;
duke(i,,m)
{
now = (now * a) % p;
mp[now] = i;
}
int t = qpow(a,m,p);
now = ;
int ok = ;
duke(i,,m)
{
now = (now * t) % p;
if(mp[now])
{
ans = i * m - mp[now];
printf("%lld\n",(ans % p + p) % p);
ok = ;
break;
}
}
if(ok == )
printf("Orz, I cannot find x!\n");
}
main()
{
read(t);read(k);
//cout<<t<<endl;
duke(i,,t)
{
int y,z,p;
read(y);read(z);read(p);
if(k == )
{
work1(y,z,p);
}
else if(k == )
{
work2(y,z,p);
}
else if(k == )
{
work3(y,z,p);
}
//cout<<i<<" "<<t<<endl;
}
return ;
}

最新文章

  1. linux内存管理
  2. 有关DTCoreText无法加载网络图片及应用问题
  3. 在CSS中定义a:link、a:visited、a:hover、a:active顺序
  4. BZOJ 1026: [SCOI2009]windy数
  5. 【IOS笔记】Windows
  6. js获取URL地址中的GET参数
  7. (转)HTML 5离线存储之Web SQL
  8. 【转】Android Studio Essential Training
  9. 那些我希望在一开始使用 Zsh(oh-my-zsh) 时就知道的
  10. bzoj 2733: [HNOI2012]永无乡
  11. RuntimeError: Model class app_anme.models.User doesn&#39;t declare an explicit app_label and isn&#39;t in an application in INSTALLED_APPS.---python学习错误记录
  12. Python——数组模块(array)
  13. opencv学习之路(10)、ROI与mask掩码
  14. 1.git使用入门之基本的更新提交操作
  15. vue的cli中自定义router
  16. 二分查找算法,java实现
  17. ansible批量修改linux服务器密码的playbook
  18. Flash 0day CVE-2018-4878 漏洞复现
  19. LightOJ 1088 - Points in Segments 二分
  20. [c#基础]使用抽象工厂实现三层

热门文章

  1. CAD把一个dwg文件,或者图像文件当着一个背景导入(com接口VB语言)
  2. Ubuntu安装Foxit PDF阅读器
  3. &lt;MySQL&gt;入门四 事务控制语言 TCL
  4. Java垃圾回收是如何工作的?
  5. Python学习第二阶段Day2,模块subprocess、 logging、re
  6. 如何将jsp后缀重写为html
  7. codevs1001 舒适的线路
  8. jQuery通过event获取点击事件的事件对象
  9. TortoiseGit推送失败的问题
  10. [bzoj1617][Usaco2008 Mar]River Crossing渡河问题_动态规划