所以我有一个最多 9 位的数字 N,我必须得到 3^n + 2^n 的最后一位。这种问题有规律可循吗?我到目前为止的代码:

#include <fstream>
#include <algorithm>
#include <math.h>
using namespace std;

ifstream fin("input.in");
ofstream fout("input.out");

int main(){
int n;
fin>>n;

fout<<fmod(pow(3,n)+pow(2,n),10);
}

但是,如果我使用它并且 n 大于 1000,它会显示 nan。

我的问题是:这样的问题有规则吗?

最佳答案

好吧,我们知道 (3^n + 2^n) % 10 = ((3^n % 10) + (2^n % 10)) % 10 ,所以我们可以使用 Modular Exponentation 来快速解决这个问题。

基本前提是3^n % 10 = (3 * (3^(n-1) % 10)) % 10

关于c++ - (3^n + 2^n) % 10 大 n,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42320746/

10-11 01:08
查看更多