hdu 1163(九余数定理)

来源:互联网 发布:幸运星 网络偶像大师 编辑:程序博客网 时间:2024/05/24 06:02

hdu 1163

九余数定理:
ans = a1 * a2 * a3 * … * an;
t = ans的各个位数之和的最终值;
t = ans % 9 (if (ans == 0) ans = 9);

#include <iostream>using namespace std;int quick_pow(int x, int n, int MOD){    int res = 1;     while (n > 0)      {          if(n & 1)          {            res = (res * x) % MOD;         }         x = (x * x) % MOD;          n >>= 1;      }      return res;  }int main(){    int n;    while (~scanf("%d", &n) && n)    {        int ans = quick_pow(n, n, 9);        if (ans == 0)        {            ans = 9;        }        printf("%d\n", ans);    }}
0 0
原创粉丝点击