HDU1163 Eddy's digital Roots(解法二)【快速模幂+九余数定理】
生活随笔
收集整理的這篇文章主要介紹了
HDU1163 Eddy's digital Roots(解法二)【快速模幂+九余数定理】
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
問題鏈接:HDU1163 Eddy's digital Roots。
問題簡述:參見上述鏈接。
問題分析:計算n^n的數根,一要快,二要簡單。使用快速模冪計算,加上數論中的九余數定理就完美了。
程序說明:(略)
參考鏈接:HDU1163 Eddy's digital Roots
題記:(略)
AC的C++語言程序如下:
/* HDU1163 Eddy's digital Roots */#include <iostream>using namespace std;const int NICE = 9;// 快速模冪計算函數 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; }
轉載于:https://www.cnblogs.com/tigerisland/p/7563705.html
總結
以上是生活随笔為你收集整理的HDU1163 Eddy's digital Roots(解法二)【快速模幂+九余数定理】的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Metrics —— JVM上的实时监控
- 下一篇: 99% 的新移动恶意程序是针对 Andr