题解 SP26368 【PWRANDMOD - Power and Mod】
实际发表时间:2020-04-16
https://www.luogu.com.cn/problem/SP26368
不想用龟速乘的看这里来!!!
前两篇题解用的都是__int128,但是相乘时又会爆__int128,又不想用龟速乘,怎么办呢?
我们注意到,数据范围都是正整数,所以我们可以使用无符号的__int128——__uint128_t!
然后读入使用快读,输出用快写,幂运算用快速幂即可
但是NOIP不能用...5555
代码:
#include<bits/stdc++.h>
#define int __uint128_t //原来忘记用了
using namespace std;
inline int read() //快读
{
int s=0;
char ch=getchar();
while(!isdigit(ch))ch=getchar();
while(isdigit(ch))
{
s=(s<<3)+(s<<1)+ch-48;
ch=getchar();
}
return s;
}
inline void write(int x) //快写
{
if(x<0)putchar('-'),x=-x;
if(x>9)write(x/10);
putchar(x%10^48);
}
inline int qp(int x,int y,int mod) //快速幂
{
int res=1;
while(y)
{
if(y&1)res=res*x%mod;
y>>=1;
x=x*x%mod;
}
return res;
}
unsigned a,b,t; //unsigned运算好像比signed运算快
int m;
signed main()
{
t=read(); //读入t
while(t--)
{ //操作
a=read(),b=read(),m=read();//读入a,b,m
write(qp(a,b,m)),puts(""); //输出
}
return 0;
}

浙公网安备 33010602011771号