题目链接

224. 计算器

你被要求设计一个计算器完成以下三项任务:

  1. 给定 \(Y,Z,P\),计算 \(Y^Z \pmod P\) 的值;
  2. 给定 \(Y,Z,P\),计算满足 \(xY≡ Z \pmod P\) 的最小非负整数 \(x\)
  3. 给定 \(Y,Z,P\),计算满足 \(Y^x ≡ Z \pmod P\) 的最小非负整数 \(x\)

输入格式

输入包含多组数据。

第一行包含两个正整数 \(T,K\) 分别表示数据组数和询问类型(对于一个测试点内的所有数据,询问类型相同)。

以下 \(T\) 行每行包含三个正整数 \(Y,Z,P\),描述一个询问。

输出格式

对于每个询问,输出一行答案。

对于询问类型 \(2\)\(3\),如果不存在满足条件的数,则输出 Orz, I cannot find x!,注意逗号与 I 之间有一个空格。

数据范围

\(1 \le Y,Z,P \le 10^9\),其中 \(P\) 为质数。
\(1 \le T \le 10\)

输入样例:

3 1
2 1 3
2 2 3
2 3 3

输出样例:

2
1
2

解题思路

快速幂,扩展欧几里得,bsgs

太板了,第一问快速幂,第二问扩欧,第三问 bsgs,另外需要特判 \(Y\) 是否为 \(P\) 的倍数

  • 时间复杂度:\(O(n\sqrt{p})\)

代码

// Problem: 计算器
// Contest: AcWing
// URL: https://www.acwing.com/problem/content/description/226/
// Memory Limit: 64 MB
// Time Limit: 1000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

// %%%Skyqwq
#include <bits/stdc++.h>
 
// #define int long long
#define help {cin.tie(NULL); cout.tie(NULL);}
#define pb push_back
#define fi first
#define se second
#define mkp make_pair
using namespace std;
 
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
 
template <typename T> bool chkMax(T &x, T y) { return (y > x) ? x = y, 1 : 0; }
template <typename T> bool chkMin(T &x, T y) { return (y < x) ? x = y, 1 : 0; }
 
template <typename T> void inline read(T &x) {
    int f = 1; x = 0; char s = getchar();
    while (s < '0' || s > '9') { if (s == '-') f = -1; s = getchar(); }
    while (s <= '9' && s >= '0') x = x * 10 + (s ^ 48), s = getchar();
    x *= f;
}

int t,k;
int ksm(int a,int b,int p)
{
	int res=1%p;
	while(b)
	{
		if(b&1)res=(LL)res*a%p;
		a=(LL)a*a%p;
		b>>=1;
	}
	return res;
}
int exgcd(int a,int b,int &x,int &y)
{
	if(!b)
	{
		x=1,y=0;
		return a;
	}
	int d=exgcd(b,a%b,y,x);
	y-=a/b*x;
	return d;
}
int bsgs(int a,int b,int p)
{
	b=b%p;
	if(1%p==b)return 0;
	int k=sqrt(p)+1;
	unordered_map<int,int> hash;
	for(int i=0,j=b;i<k;i++)
	{
		hash[j]=i;
		j=(LL)j*a%p;
	}
	int ak=1;
	for(int i=1;i<=k;i++)ak=(LL)ak*a%p;
	for(int i=1,j=ak;i<=k;i++)
	{
		if(hash.count(j))return i*k-hash[j];
		j=(LL)j*ak%p;
	}
	return -1;
}
int main()
{
    while(cin>>t>>k)
    {
    	while(t--)
    	{
    		int y,z,p;
	    	cin>>y>>z>>p;
	    	if(k==1)cout<<ksm(y,z,p)<<'\n';
	    	else if(k==2)
	    	{
	    		int X,Y;
	    		int d=exgcd(y,p,X,Y);
	    		if(z%d!=0)puts("Orz, I cannot find x!");
	    		else
	    		{
	    			p/=d;
	    			X=(LL)z/d*X%p;
	    			X=(X+p)%p;
	    			cout<<X<<'\n';
	    		}
	    	}
	    	else
	    	{
	    		if(y%p==0)
	    		{
	    			if(z==1)puts("0");
	    			else
	    				puts(z%p==0?"1":"Orz, I cannot find x!");
	    		}
	    		else
	    		{
	    			int res=bsgs(y,z,p);
	    			if(res<0)puts("Orz, I cannot find x!");
	    			else
	    				cout<<res<<'\n';	
	    		}
	    	}	
    	}
    }
    return 0;
}

原文地址:http://www.cnblogs.com/zyyun/p/16898496.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性