Mod power或totient逻辑问题
Mod power or totient logic issue
这里是我改编的一些代码,用于处理欧拉totient函数和幂模函数。每个n
, f2
都是3
而不是各种数字。有人看到错误了吗?phi(n)
和modpow(n)
似乎都工作良好。
#include <iostream>
using namespace std;
int gcd(int a, int b)
{
while (b != 0)
{
int c = a % b;
a = b;
b = c;
}
return a;
}
int phi(int n)
{
int x = 0;
for (int i=1; i<=n; i++)
{
if (gcd(n, i) == 1)
x++;
}
return x;
}
int modpow(int base, int exp, int mod) // from stackoverflow
{
base %= mod;
long long result = 1;
while (exp > 0)
{
if (exp & 1)
result = (result * base) % mod;
base = (base * base) % mod;
exp >>= 1;
}
return result;
}
int f2(int n) // f(f(n)) mod n
{
long long a = modpow(2, n, phi(n)) + 1;
return (modpow(2, a, n) + 1) % n;
}
// ...
int main()
{
int n = 520001;
while (true)
{
cout << "f2 " << f2(n) << endl;
if (f2(n) == 0)
{
// ...
}
n += 2;
}
return 0;
}
f2(n)
的值应该是9, 458278, 379578, ...
如果base>= 65536,
base*base将超过int型的大小。尝试这个修复,似乎工作:
int modpow(int base, int exp, int mod) // from stackoverflow
{
base %= mod;
long long result = 1;
while (exp > 0)
{
if (exp & 1)
result = (result * base) % mod;
base = (int)(((long long)base * (long long)base) % mod);
exp >>= 1;
}
return (int) result;
}
相关文章:
- 警告处理为错误这里有什么问题
- 最小硬币更换问题(自上而下方法)
- 为"adjacent"变量赋值时出现问题
- 我的神经网络不起作用 [XOR 问题]
- 在Ubuntu 16.04上安装Cilk时出现问题
- C++我的数学有什么问题,为什么我的代码不能正确循环
- 编译包含字符串的代码时遇到问题
- Project Euler问题4的错误解决方案
- 问题:什么是QAbstractItemView::NoEditTriggers的反面
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 在进程中对同一管道进行读取和写入时C++管道出现问题
- 静态数据成员的问题-修复链接错误会导致编译器错误
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 一个关于在C++中重载布尔运算符的问题
- 首要问题的答案让值班员搞错了
- setlocale的C++土耳其字符串问题
- 如何重构类层次结构以避免菱形问题
- 基于boost的程序的静态链接——zlib问题
- C++格式化输出问题
- Mod power或totient逻辑问题