快速幂定理和九余数定理(Eddy's digital Roots)
2015-10-11 10:43
447 查看
题目描述:
The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root.
If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit.
For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3
and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.
The Eddy's easy problem is that : give you the n,want you to find the n^n's digital Roots.
Input
The input file will contain a list of positive integers n, one per line. The end of the input will be indicated by an integer value of zero. Notice:For each integer in the input n(n<10000).
Output
Output n^n's digital root on a separate line of the output.
这道题运用九余数定理和快速幂定理可以很好的完成。
九余数定理:一个数各位数字之和等于这个数对9取模所得的数。
快速幂定理:假设是求a的b次方。
下面是快速幂定理的C代码实现:
有了这两个定理,下面我们就可以运用这两个定理来解析这道题了。
The digital root of a positive integer is found by summing the digits of the integer. If the resulting value is a single digit then that digit is the digital root.
If the resulting value contains two or more digits, those digits are summed and the process is repeated. This is continued as long as necessary to obtain a single digit.
For example, consider the positive integer 24. Adding the 2 and the 4 yields a value of 6. Since 6 is a single digit, 6 is the digital root of 24. Now consider the positive integer 39. Adding the 3
and the 9 yields 12. Since 12 is not a single digit, the process must be repeated. Adding the 1 and the 2 yeilds 3, a single digit and also the digital root of 39.
The Eddy's easy problem is that : give you the n,want you to find the n^n's digital Roots.
Input
The input file will contain a list of positive integers n, one per line. The end of the input will be indicated by an integer value of zero. Notice:For each integer in the input n(n<10000).
Output
Output n^n's digital root on a separate line of the output.
这道题运用九余数定理和快速幂定理可以很好的完成。
九余数定理:一个数各位数字之和等于这个数对9取模所得的数。
快速幂定理:假设是求a的b次方。
下面是快速幂定理的C代码实现:
int quick_pow( int a, int b ){ int r = 1, base = a; while( b != 0 ){ if( b&1 ){ //取 b 二进制的最低位 r *= base; } base *= base; //幂次控制器 b>>=1; //将 b 的二进制位右移一位 } return r; }
有了这两个定理,下面我们就可以运用这两个定理来解析这道题了。
int quick_p_9( int a, int b, int mod ){ int rs = 1, base = a; while( b != 0 ){ if( b & 1 ){ rs = ( rs * (base%mod) ) % mod; } base = ( (base%mod) * (base%mod) )%mod; b >>= 1; } return rs; } int main() { int n; while( cin>>n && n != 0 ){ int ans = quick_p_9(n,n,9); cout<<( ans == 0 ? 9 : ans )<<endl; } return 0; }
相关文章推荐
- C++实现Matlab的psf2otf函数
- 百度质量部开发测试一面2015/10/10
- 不定宽高的水平垂直居中实现方式
- HashMap 4种循环遍历方式及其性能对比
- eclipse 快捷键大全
- 程序员和码农的区别
- 沙盒
- 创建自己的Stack底层使用数组来实现
- RecyclerView的基本使用(上)
- 字典
- 有人3见解
- HDU5221 Occupation 树链剖分
- 有关字符数组和字符串的函数
- UITableView自定义cell更换选中的背景色
- 【IOS 开发学习总结-OC-41】★★ios开发——代码控制 UI 界面与 UI 控件的自定义
- 按字符拆分用法
- linux配置matconvnet(GPU support)
- ocp-300
- C++ 函数--参数传递
- ocp-299