HDOJ 1163 Eddy's digital Roots(简单数论)
2015-03-01 23:56
447 查看
【思路】: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);[公式二]
可以看出,N个相同整数的乘积总值的树根 = 每一项元素的树根的乘积
再设另外一个整数cd,且cd!=ab
ab*cd = (a*10+b)*(c*10+d) = 100*a*c+10*(a*d+b*c)+b*d
根据上式可得:root(ab*cd) = a*c+a*d+b*c+b*d = (a+b)*(c+d);[公式三]
可见,对于两个不相同整数也成立。
最后将上面证得的结果一般化:
N个整数的乘积总值的数根 = 每个项元素的数根的乘积
提示:本题只需根据[公式三] 即可AC.
注:思路确实是公式二没错,但是中间过程采用公式二会溢出,所以必须通过公式三。(思想一样)
【AC代码】:
求解思路:
现在分析一个问题,假设将十位数为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);[公式二]
可以看出,N个相同整数的乘积总值的树根 = 每一项元素的树根的乘积
再设另外一个整数cd,且cd!=ab
ab*cd = (a*10+b)*(c*10+d) = 100*a*c+10*(a*d+b*c)+b*d
根据上式可得:root(ab*cd) = a*c+a*d+b*c+b*d = (a+b)*(c+d);[公式三]
可见,对于两个不相同整数也成立。
最后将上面证得的结果一般化:
N个整数的乘积总值的数根 = 每个项元素的数根的乘积
提示:本题只需根据[公式三] 即可AC.
注:思路确实是公式二没错,但是中间过程采用公式二会溢出,所以必须通过公式三。(思想一样)
【AC代码】:
#include <iostream> #include <cstdlib> #include <cstdio> #include <cstring> #include <cmath> #include <iomanip> #include <algorithm> using namespace std; /* run this program using the console pauser or add your own getch, system("pause") or input loop */ int getRoot(int n) { while (n >= 10) { int sum = 0; while (n) { sum += n%10; n /= 10; } n = sum; } return n; } int main(int argc, char** argv) { //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); int n = 0; while (cin >> n && n) { int root = getRoot(n); int i = 0, mul = 1; for (i = 0; i < n; i++) mul = getRoot(root*mul); cout << getRoot(mul) << endl; } return 0; }
相关文章推荐
- HDOJ 1163 Eddy's digital Root (简单数论,但我也不会。。。。)
- HDOJ 1163 Eddy's digital Roots [简单数论]
- HDOJ 1163 Eddy's digital Roots [简单数论]
- 数论一(hdoj 简单数学题、推理题)
- HDOJ,数论简单入门题目,杭电1262,寻找素数对
- hdoj简单题目(九)
- hdoj简单题目(七)
- hdoj简单题目(五)
- hdoj简单题目(一)
- HDOJ 2086 A1=? [简单归纳]
- HDU/HDOJ 4002 2011大连赛区网络赛 数论
- hdoj 2050 折线分割平面解题报告(简单递推)
- 简单的数论题目zju1652(不知道有没有AC,因为我在zju上没帐号)PKU上没有
- HDOJ-4002/(大连网赛1002)- Find the maximum 数论
- HDOJ-4002/(大连网赛1002)- Find the maximum 数论
- hdoj简单题目(八)
- 几个简单数论的算法的实现
- hdoj简单题目(二)
- HDU 1222(数论,简单题)
- hdoj简单题目(十)