Eddy's digital Roots-数论基础

B - Eddy‘s digital Roots

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d
& %I64u

Submit Status Practice HDU
1163

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 
//九余数定理
//对于一个数的各数位和等于他对9进行取余,当为零的时候则是9
/*
Author: 2486
Memory: 1408 KB		Time: 0 MS
Language: G++		Result: Accepted
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n;
int main(){
while(~scanf("%d",&n),n){
    int i=n;
    int sum=1;
    while(i--){
        sum*=n;
        sum%=9;
    }
    if(sum==0)printf("9\n");
    else printf("%d\n",sum);
}

return 0;
}
//N个整数的乘积总值的数根 = 每个项元素的数根的乘积
/*
Author: 2486
Memory: 1408 KB		Time: 15 MS
Language: G++		Result: Accepted
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int n;
int rot(int x){
    int sum=0;
    while(x){
        sum+=x%10;
        x/=10;
    }
    if(sum>=10)return rot(sum);
    return sum;
}
int main(){
while(~scanf("%d",&n),n){
    int k=rot(n);
    int ans=1;
    for(int i=0;i<n;i++){
        ans=rot(k*ans);
    }
    printf("%d\n",ans);
}

return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

Eddy's digital Roots-数论基础

时间: 2024-10-12 17:45:48

Eddy's digital Roots-数论基础的相关文章

HDOJ 1163 Eddy&#39;s digital Roots(简单数论)

[思路]:http://blog.csdn.net/iamskying/article/details/4738838 求解思路: 现在分析一个问题,假设将十位数为a,个位数为b的一个整数表示为ab,则推导得 ab*ab = (a*10+b)*(a*10+b) = 100*a*a+10*2*a*b+b*b 根据上式可得:root(ab*ab) = a*a+2*a*b+b*b = (a+b)*(a+b);[公式一] 同理也可证得:root(ab*ab*ab) = (a+b)*(a+b)*(a+b)

HDU 1163 Eddy&#39;s digital Roots

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 digit

HDU1163 Eddy&#39;s digital Roots【九余数定理】

Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4436    Accepted Submission(s): 2505 Problem Description The digital root of a positive integer is found by summing the digi

Hdu-1163 Eddy&#39;s digital Roots(九余数定理)

题意:对于一个数,只要它不是单个数,就把它各位的数相加,直到成为单个数. 九余数定理 一个数对九取余后的结果称为九余数. 一个数的各位数字之和想加后得到的<10的数字称为这个数的九余数(如果相加结果大于9,则继续各位相加) 因为数据挺大的,不能暴力.所以可以边乘边取余.很像快速幂. 1 #include<iostream> 2 #include<algorithm> 3 using namespace std; 4 5 int main(){ 6 int n; 7 while

HDU1163 Eddy&#39;s digital Roots

问题链接:HDU1013 Digital Roots.入门练习题,用C语言编写程序. 这个问题是对于输入的n,计算n^n的数根. 先看一下以下式子: 因为:(10*a+b)*(10*a+b)=100*a*a+10*2*a*b+b*b 所以右边式子的数根(中间结果,也是左边式子的数根)为:a*a+2*a*b+b*b=(a+b)*(a+b) 故:对于两位数n,n*n的数根=n的树根×n的树根. 同理可以推出,对于任意位数的n,也满足:n*n的数根=n的树根×n的树根. 程序中,实现一个计算整数数根的

Eddy&#39;s digital Roots(九余数定理)

Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 5113    Accepted Submission(s): 2851 Problem Description The digital root of a positive integer is found by summing the digit

杭电 HDU 1163 Eddy&#39;s digital Roots

Eddy's digital Roots Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 4904    Accepted Submission(s): 2743 Problem Description The digital root of a positive integer is found by summing the digi

hdu 1163 Eddy&#39;s digital Roots(九余数定理)

hdu 1163 Eddy's digital Roots 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

Eddy&#39;s digital Roots

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4793    Accepted Submission(s): 2672 Problem Description The digital root of a positive integer is found by summing the digits of the integer. If