hdu 1163
来源:互联网 发布:node.js入门 编辑:程序博客网 时间:2024/05/16 09:29
主题思想: 9余数定理
一个数各位数字之和如果不是一位数,则继续对结果执行求各位数字之和。最后得到一个一位数。这种计算操作称为求一个数的各位数字之和。
一个数各位数字之和等于对9取余数,如果余数是0,则各位数字只和是9。
快速幂求模定理。
a^b if b==2k+1 a^b =a^k*a^k*a
else if b==2k a^b=a^k*a^k
快速幂定理求模代码
int a,b;int ans=1;while(b){ if(b%2) ans=ans*a%10; a=a*a%10; b/=2;}
通用快速幂代码
//usually nod is 10 but this problem mod is 9;int quickMod(int a,int b,int mod){ int ans=1; while(b){ if(b%2==1) ans=ans*a%mod; a=a*a%mod; b/=2; } return ans%mod;}
AC 代码
#include <iostream>#include<cstdio>using namespace std;//usually nod is 10 but this problem mod is 9;int quickMod(int a,int b,int mod){ int ans=1; while(b){ if(b%2==1) ans=ans*a%mod; a=a*a%mod; b/=2; } return ans%mod;}int main(){ int n; int ans=0; while(scanf("%d",&n)!=EOF&&n!=0){ ans=quickMod(n,n,9); ans=ans==0?9:ans; printf("%d\n",ans); } return 0;}
阅读全文
0 0
- HDU 1163
- hdu 1163
- hdu 1163
- hdu 1163
- hdu 1163
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- Linux下automake使用方法
- windows 安装tensorflow/及开发环境
- Glide使用
- 工作杂记二
- 非root用户启动nginx 80端口
- hdu 1163
- 网易云信混淆设置
- jQuery的属性与样式
- Kubernetes PodGC Controller源码分析
- MicroSoft/LintCode:M-打劫房屋 II
- 算法训练 输出米字形
- 狄克斯特拉算法,解决加权最短路径问题--python实现
- Thread Join()的用法
- MPEG-DASH简介