模幂运算 我试图使用,以打破大指数的基础,因为数据类型在标准C++库中不存储大的数字。p>
问题是在最后一个循环中,我使用模幂运算 我试图使用,以打破大指数的基础,因为数据类型在标准C++库中不存储大的数字。p>,c++,math,modular,C++,Math,Modular,问题是在最后一个循环中,我使用fmod()函数修改我的大数字。答案应该是1,但我得到了16。有人看到问题了吗 #include <iostream> #include <vector> #include <math.h> using namespace std; typedef vector<int> ivec; ivec binStorage, expStorage; void exponents() { for (int j=bi
fmod()
函数修改我的大数字。答案应该是1,但我得到了16。有人看到问题了吗
#include <iostream>
#include <vector>
#include <math.h>
using namespace std;
typedef vector<int> ivec;
ivec binStorage, expStorage;
void exponents()
{
for (int j=binStorage.size(); j>=0; j--)
if(binStorage[binStorage.size()-j-1]!=0)
expStorage.push_back(pow(2, j));
}
void binary(int number)
{
int remainder;
if(number <= 1)
{
cout << number;
return;
}
remainder = number%2;
binary(number >> 1);
cout << remainder;
binStorage.push_back(remainder);
}
int main()
{
int num = 117;
int message = 5;
int mod = 19;
int prod = 1;
binary(num);
cout << endl;
exponents();
cout << "\nExponents: " << endl;
for (int i=0; i<expStorage.size(); i++)
cout << expStorage[i] << " " ;
cout << endl;
cout << "\nMessage" << "-" << "Exponent" << endl;
for (int i=0; i<expStorage.size(); i++)
{
cout << message << "-" << expStorage[i] << endl;
prod *= fmod(pow(message, expStorage[i]), mod);
}
cout << "\nAnswer: " << fmod(prod, mod) << endl;
return 0;
}
编辑:
这里是问题循环
for (int i=0; i<expStorage.size(); i++)
{
cout << message << "-" << expStorage[i] << endl;
prod *= fmod(pow(message, expStorage[i]), mod);
}
for(int i=0;i您发布的算法是。按照您发布的链接中的步骤,算法将简化为以下代码:
#include <iostream>
#include <cmath>
// B : Base
// E : Exponent
// M : Modulo
constexpr int powermod(int const B, int const E, int const M) {
return ((E > 1) ? (powermod(B, E / 2, M) * powermod(B, E / 2, M) * powermod(B, E % 2, M)) % M
: (E == 0) ? 1 : B % M);
}
int main() {
int const e = 117;
int const b = 5;
int const m = 19;
std::cout << "Answer: " << powermod(b, e, m) << std::endl;
return 0;
}
输出:
1110101
指数:
64 32 16 4 1
消息指数
5-64
5-32
5-16
5-1
答复:一,
你不能把它缩小到一对输入和输出吗?我肯定我们不需要整个程序和所有那些无关的计算。我把它缩小到了一个问题:在某个点上,5是64的幂吗?即溢出,它错过了链接中的替换点。我不是要你在我们身上转储一个随机循环。我用于演示的程序只包含一个小的main
、一个或两个常量输入值、有问题的函数调用,以及一些输出,以证明输出不是它应该的样子。这应该是您的第一个调试步骤。使用instead继续获取错误:预期的主表达式“int”之前的“ion”。这是什么?@user3519448您正在使用的编译器?@user3519448请参阅更新的答案。您的代码中有错误,如超出范围等。
#include <iostream>
#include <cmath>
// B : Base
// E : Exponent
// M : Modulo
constexpr int powermod(int const B, int const E, int const M) {
return ((E > 1) ? (powermod(B, E / 2, M) * powermod(B, E / 2, M) * powermod(B, E % 2, M)) % M
: (E == 0) ? 1 : B % M);
}
int main() {
int const e = 117;
int const b = 5;
int const m = 19;
std::cout << "Answer: " << powermod(b, e, m) << std::endl;
return 0;
}
#include <iostream>
#include <cmath>
#include <vector>
using namespace std;
typedef vector<int> ivec;
// B : Base (e.g., 5)
// E : Exponent (e.g., 32)
// M : Modulo (e.g., 19)
double safemod(double B, double E, double M) {
return ((E > 4) ? fmod(safemod(B, E / 2, M) * safemod(B, E / 2, M), M)
:
fmod(pow(B, E), M));
}
void exponents(ivec const &binStorage, ivec &expStorage) {
int j(pow(2.0, binStorage.size() - 1));
for (vector<int>::const_iterator it(binStorage.begin()), ite(binStorage.end()); it != ite; ++it) {
if (*it != 0) expStorage.push_back(j);
j /= 2;
}
}
void binary(int const number, ivec &binStorage) {
if (number > 0) {
int remainder = number % 2;
binary(number / 2, binStorage);
binStorage.push_back(remainder);
}
}
int main() {
int num = 117;
int message = 5;
int mod = 19;
int prod = 1;
ivec binStorage, expStorage;
binary(num, binStorage);
for (size_t i(0); i < binStorage.size(); ++i) cout << binStorage[i];
cout << endl;
exponents(binStorage, expStorage);
cout << "\nExponents: " << endl;
for (size_t i(0); i<expStorage.size(); ++i) cout << expStorage[i] << " ";
cout << endl;
cout << "\nMessage" << "-" << "Exponent" << endl;
for (size_t i(0); i<expStorage.size(); ++i) {
cout << message << "-" << expStorage[i] << endl;
prod *= safemod(message, expStorage[i], mod);
}
cout << "\nAnswer: " << fmod(prod, mod) << endl;
return 0;
}