1163:Eddy's digital Roots
来源:互联网 发布:网络百家家乐公式赢钱 编辑:程序博客网 时间:2024/04/27 16:22
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1163
方法:快速幂取余
思路:这道题如果直接暴力做,都不用想肯定会爆内存,因此肯定是要寻求其它方法解决。先考虑这样一道题,也就是原始的求数字根的题目,题目要求的是随意指定一个数字,求取其数字根,与本题唯一不同的就是不需要进行幂操作,详见1013题。在解决1013时,可以按照题目要求模拟,但是还有一种捷径,那就是对9取余,就是结果,但是要注意一点,如果是9的整数倍,比如27,取余就会变成0,这意味着这种情况要特殊处理,也就是说凡是9的倍数答案都应该是9,这样这个题就比较清晰了,可以采用快速幂取余的思路快速对9取余,就是答案。
难点:想到对9取余以及二分求幂的算法。
#include <cstdio>using namespace std;int powermod(int a,int b,int c){ int ans = 1; while(b!=0) { if(b%2 == 1) { ans *= a; ans %= c; } b /= 2; a *= a; a %= c; } return ans;}int main(){ int n; while(~scanf("%d",&n)) { if(n == 0) break; if(powermod(n,n,9) == 0) printf("9\n"); else printf("%d\n",powermod(n,n,9)); }}
0 0
- 1163:Eddy's digital Roots
- Eddy's digital Roots
- Eddy's digital Roots
- Eddy's digital Roots
- Eddy's digital Roots
- Eddy's digital Roots
- hdu 1163 Eddy's digital Roots
- 杭电1163 Eddy's digital Roots
- hdu 1163 Eddy's digital Roots
- HDU 1163 Eddy's digital Roots
- HDU 1163 Eddy's digital Roots
- HDU 1163 Eddy's digital Roots
- 数论---HDU 1163 Eddy's digital Roots
- HDU 1163 Eddy's digital Roots
- hdu 1163 Eddy's digital Roots
- HDU 1163 Eddy's digital Roots
- hdu(1163)Eddy's digital Roots
- HDU 1163 Eddy's digital Roots(数论)
- [leetcode]Contains Duplicate C语言
- 多线程基础
- 之前UI的小练习
- Makefile汇总
- Session与Cookie
- 1163:Eddy's digital Roots
- 埃及分数 把一个分数分解成n个 m分之一的方式
- 欢迎来到我的博客。
- UVa 1583 Digit Generator
- window10 问题
- iOS开发之有趣的UI —— 基础控件与经典案例
- BZOJ 1488 [HNOI2009]图的同构 Polya定理
- css3的新增属性transform
- 电阻电路分析(基于MATLAB)