求助快速幂!!
  • 板块灌水区
  • 楼主xuxinyi
  • 当前回复10
  • 已保存回复10
  • 发布时间2022/6/18 19:47
  • 上次更新2023/10/27 23:03:26
查看原帖
求助快速幂!!
373422
xuxinyi楼主2022/6/18 19:47

求助 这是快速幂的题 在洛谷上能A但是在别的OJ上只能得40分 这是别的OJ上的题面:
给定A, B, P,求A^B%P

数据规模和约定
  共10组数据
  对100%的数据,A, B为long long范围内的非负整数,P为int内的非负整数。

#include<bits/stdc++.h>
using namespace std;
long long a,b;
int p;
int fastpow(long long a,long long b,int p)
{
  long long ans=1;
  long long temp=a;
  while(b>0)
  {
  	if(b&1)
  	{
  		ans*=temp;
  		ans%=p;
  	}
  	temp*=temp;
  	temp%=p;
  	b>>=1;
  }
  return ans;
}
int main()
{
  cin>>a>>b;
  cin>>p;
  cout<<a<<"^"<<b<<" mod "<<p<<"="<<fastpow(a,b,p);
}

这是我的代码 请各位大佬帮忙看看(我借鉴了洛谷上的一篇题解 因为我刚学这个算法)

2022/6/18 19:47
加载中...