當(dāng)前位置:
首頁(yè) >
HDU1163 Eddy's digital Roots(解法二)【快速模幂+九余数定理】
發(fā)布時(shí)間:2025/6/15
51
豆豆
生活随笔
收集整理的這篇文章主要介紹了
HDU1163 Eddy's digital Roots(解法二)【快速模幂+九余数定理】
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
問(wèn)題鏈接:HDU1163 Eddy's digital Roots。
問(wèn)題簡(jiǎn)述:參見(jiàn)上述鏈接。
問(wèn)題分析:計(jì)算n^n的數(shù)根,一要快,二要簡(jiǎn)單。使用快速模冪計(jì)算,加上數(shù)論中的九余數(shù)定理就完美了。
程序說(shuō)明:(略)
參考鏈接:HDU1163 Eddy's digital Roots
題記:(略)
AC的C++語(yǔ)言程序如下:
/* HDU1163 Eddy's digital Roots */#include <iostream>using namespace std;const int NICE = 9;// 快速模冪計(jì)算函數(shù) int powermod(int a, int n, int m) {int res = 1;while(n) {if(n & 1) { // n % 2 == 1res *= a;res %= m;}a *= a;a %= m;n >>= 1;}return res; }int main() {int n, ans;while(cin >> n && n) {ans = powermod(n, n, NICE);cout << (ans ? ans : 9) << endl;}return 0; }
轉(zhuǎn)載于:https://www.cnblogs.com/tigerisland/p/7563705.html
總結(jié)
以上是生活随笔為你收集整理的HDU1163 Eddy's digital Roots(解法二)【快速模幂+九余数定理】的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: Metrics —— JVM上的实时监控
- 下一篇: Zepto源代码分析之二~三个API