bool MillerRabinKnl(Bigint& n) //单次MillerRabin素性检测 { Bigint b, m, v, temp; Bigint j = { 0 }; Bigint one = { 1 }; Bigint two = { 2 }; Bigint three = { 3 }; m = Sub(n, one);//n-1 while (!(m.num[0] & 0x01)) { j = Add(j, one);//j+1 BitMoveRight(m); } b = Add(two, BigRand(Sub(n, three))); v = PowMod(b, m, n);//模幂 if (Compare(v, one) == 0)//v=1 return 1; Bigint i = { 1 }; temp = Sub(n, one); while (Compare(v, temp) < 0) { if (Compare(i, j) == 0) return 0; v = MulMod(v, v, n); i = Add(i, one); } return 1; }
时间: 2024-04-05 19:29:40 浏览: 12
这段代码中用到了一些自定义的大整数运算函数,比如 Sub、Add、BigRand、PowMod 和 MulMod。其中,Sub 和 Add 分别实现了大整数的减法和加法,BigRand 是用于生成一个随机的大整数,PowMod 是用于进行大整数的模幂运算,MulMod 是用于进行大整数的模乘运算。
具体来说,Sub 和 Add 都是通过模拟手算的方式实现的,BigRand 是通过调用 C++ 标准库的随机数生成器实现的,PowMod 和 MulMod 都是通过使用快速幂和快速乘算法实现的。
需要注意的是,这段代码中的大整数类型 Bigint 可能是作者自己实现的,也可能是调用了某个第三方库,因此具体实现可能会有所不同。
相关问题
#include<clocale> #include<algorithm> #include<cmath> #include<cstdio> #include<cstdlib> #include<cstring> #include<ctime> #include<iostream> #include<string> using namespace std; //bool a(int n){ // if((n%4==0&&n%100!=0)||n%400==0){ // return true; // } // else{ // return false; // } // //} int b(int y,int n){ if(n==1||n==3||n==5||n==7||n==8||n==10||n==12){ return 31; } if(n==4||n==6||n==9||n==11){ return 30; } if(n==2&&a(y)==true){ return 29; } if(n==2&&a(y)==false){ return 28; } } int main(){ int y,m,d; cin>>y>>m>>d; int s=0; // if(a(y)==true){ // bool c=true; // } // if(a(y)==false){ // bool c=false; // } for(int i=1;i<=m;i++){ s+=b(y,i); } s+=d; cout<<s; return 0; }
在你提供的代码中,你注释掉了函数a的定义和调用,导致在函数b中使用a(y)时出现了错误。为了解决这个问题,你可以取消注释函数a的定义和调用,或者在函数b中使用其他方式来判断是否为闰年。
以下是取消注释函数a的部分代码:
```cpp
bool a(int n){
if((n%4==0&&n%100!=0)||n%400==0){
return true;
}
else{
return false;
}
}
```
然后在函数b中调用函数a来判断是否为闰年:
```cpp
int b(int y,int n){
if(n==1||n==3||n==5||n==7||n==8||n==10||n==12){
return 31;
}
if(n==4||n==6||n==9||n==11){
return 30;
}
if(n==2&&a(y)==true){
return 29;
}
if(n==2&&a(y)==false){
return 28;
}
}
```
这样就可以解决函数a未定义导致的错误。请注意,在使用函数b之前,需要确保函数a已经定义并按照需要进行调用。
cpp如何将bool类型打印成true/false而不是1/0
在C++中,可以使用boolalpha流控制符将bool类型输出为true/false而不是1/0。
示例代码如下:
```c++
#include <iostream>
int main() {
bool b = true;
std::cout << std::boolalpha << b << std::endl;
return 0;
}
```
输出结果为:
```true```
需要注意的是,使用boolalpha流控制符会影响后续所有bool类型的输出,如果需要取消这种输出方式,可以使用noboolalpha流控制符。