Eddy's digital Roots

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5783    Accepted Submission(s): 3180

Problem Description
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.
 
Sample Input
2
4
0
 
Sample Output
4
4
 
 
 
解析: 考察数字根
数字根定义:对于一个正整数,如果它是个位数,它的数字根就是它本身。如果它不是个位数,则将其各位上的数字相加,所得的和如果是个位数,那么这个数就是它的数字根;否则将这个数各位上的数字相加,直至所得的和为个位数为止。
数n的数字根:digital root = (n-1)%9+1。为了方便计算,我们也可以写为digital root = (n%9 == 0 ? 9 : n%9)。
数字根性质:
1.任何数加上9的数字根等于它本身的数字根。
2.任何数乘以9的数字根等于9。
3.多个数之和的数字根等于这多个数的数字根的和的数字根。
4.多个数之积的数字根等于这多个数的数字根的积的数字根。
根据以上规律,结合快速幂及其取模的方法,可以得到较快速的算法。
 
//计算x的y次幂(快速)
int quickpow(int x,int y)
{
int ret = ;
while(y){
if(y&)
ret *= x;
x *= x;
y >>= ;
}
return ret;
}
//计算x的y次幂对mod取模(快速)
int quickpowmod(int x,int y,int mod)
{
int ret = ;
x %= mod;
while(y){
if(y&)
ret = ret*x%mod;
x = x*x%mod;
y >>= ;
}
return ret;
}
 #include <cstdio>

 int quickpowmod(int x,int y,int mod)
{
int ret = ;
x %= mod;
while(y){
if(y&)
ret = ret*x%mod;
x = x*x%mod;;
y >>= ;
}
return ret;
} int main()
{
int n;
while(scanf("%d",&n), n){
int ans = quickpowmod(n,n,);
printf("%d\n",ans == ? : ans);
}
return ;
}

HDU 1163 Eddy's digital Roots的更多相关文章

  1. HDU 1163 Eddy's digital Roots(模)

    HDU 1163 题意简单,求n^n的(1)各数位的和,一旦和大于9,和再重复步骤(1),直到和小于10. //方法一:就是求模9的余数嘛! (228) leizh007 2012-03-26 21: ...

  2. hdu 1163 Eddy's digital Roots 【九余数定理】

    http://acm.hdu.edu.cn/showproblem.php?pid=1163 九余数定理: 如果一个数的各个数位上的数字之和能被9整除,那么这个数能被9整除:如果一个数各个数位上的数字 ...

  3. HDOJ 1163 Eddy's digital Roots(九余数定理的应用)

    Problem Description The digital root of a positive integer is found by summing the digits of the int ...

  4. HDOJ 1163 Eddy's digital Roots 九余数定理+简单数论

    我在网上看了一些大牛的题解,有些知识点不是太清楚, 因此再次整理了一下. 转载链接: http://blog.csdn.net/iamskying/article/details/4738838 ht ...

  5. Eddy's digital Roots(九余数定理)

    Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  6. HDU-1163 Eddy's digital Roots(九余数定理)

    Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

  7. Eddy's digital Roots

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission( ...

  8. HDU1163 - Eddy's digital Roots

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1163 九余数:一个数除于9所得到的余数,即模9得到的值 求九余数: 求出一个数的各位数字之和,如果是两 ...

  9. HDU1163 Eddy&#39;s digital Roots【九剩余定理】

    Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Oth ...

随机推荐

  1. Asp.net之LINQ入门视频教程

    当前位置: 主页 > 编程开发 > Asp.net视频教程 > Asp.net之LINQ入门视频教程 > http://www.xin1234.com/Program/Aspn ...

  2. EXTJS store 某行某列数据更新等操作

    1.可以使用add(Ext.data.Record[] records)或者add(Ext.data.Record record)向store末尾添加一个或多个record.如: var newRec ...

  3. Hibernate从入门到精通(三)Hibernate配置文件

    在上次的博文Hibernate从入门到精通(二)Hibernate实例演示我们已经通过一个实例的演示对Hibernate的基本使用有了一个简单的认识,这里我们在此简单回顾一下Hibernate框架的使 ...

  4. (转)深入理解SP、LR和PC

    网址:http://blog.csdn.net/zhou1232006/article/details/6149548 深入理解ARM的这三个寄存器,对编程以及操作系统的移植都有很大的裨益. 1.堆栈 ...

  5. NOSQL Mongo入门学习笔记 - C++连接Mongodb(三)

    OS环境: Centos 7.1 release X86_64 编译环境: G++ 4.8.3 已经成功搭建好了Mongodb,也初步在命令行中的查询与写入数据的基本方法,现在通过C++来连接Mong ...

  6. NOI2015考试小结

    这次NOI2015有幸获得金牌考进了国家集训队,意味着我的OI退役时间既省选之后有延迟了好几个月,又有了新的目标吧. 先说一下考试之外的感受吧,学军宿舍很牛X,接待NOIers而不提供插座,唯一可以用 ...

  7. Unix/Linux下如何使用Vi编辑器

    vi 的工作模式 Vi 在初始启动后首先进入编辑模式,这时用户可以利用一些预先定义的按键来移动光标.删除文字. 复制或粘贴文字等.这些按键均是普通的字符,例如 l 是向右移动光标,相当于向右箭头键,k ...

  8. Redis安装整理(window平台) +php扩展redis

    window平台Redis安装 redis windows安装文件下载地址:http://code.google.com/p/servicestack/wiki/RedisWindowsDownloa ...

  9. HDU1431+简单题

    题意简单 预处理之后会发现符合条件的数最多781个... 所以打表.. /* */ #include<algorithm> #include<iostream> #includ ...

  10. Servlet课程0425(五) sendRedirect实现不同页面共享数据

    Login.java //登录界面 package com.tsinghua; import javax.servlet.http.*; import java.io.*; public class ...