平方的大数的模数
Modulo for large numbers that are squared
我有很多数字想拿走剩下的。例如 300^31。当我尝试使用运算符%
它说我不能在双精度上使用它,因为它仅适用于整数。该计算有什么功能吗?
我试图使用a=pow(b,x)%d;
如果 b
、 x
和 d
适合整数,则可以使用
int expoModulo(int b, int x, int d) {
int a = 1;
for(int i=0; i<x; i++) {
a = (a*b)%d;
}
return a;
}
我认为最好
在尝试指数时取余数。您可以使用大模算法。它将帮助您避免获得大数字。之后,你可以只使用长长而不是双倍。
我尝试在代码块 16.01(codeblocks-16.01mingw-setup.exe)中编写此代码,并且效果很好(我的代码与链接代码略有不同,但它具有相同的概念):
#include<iostream>
using namespace std;
int bmod = 33;
int bigmod(long long int x,long long int b){ // x^b
int ans=1;
while(b){
if(b%2)ans=((long long)ans*x)%bmod;
b=b>>1;
x=((long long)x*x)%bmod;
}
return ans;}
int main(){
cout<<bigmod(300,31);
}
相关文章:
- 没有找到相关文章