The number 145 is well known for the property that the sum of the factorial of its digits is equal to 145:

1! + 4! + 5! = 1 + 24 + 120 = 145

Perhaps less well known is 169, in that it produces the longest chain of numbers that link back to 169; it turns out that there are only three such loops that exist:

169  363601  1454  169 871  45361  871 872  45362  872

It is not difficult to prove that EVERY starting number will eventually get stuck in a loop. For example,

69  363600  1454  169  363601 ( 1454) 78  45360  871  45361 ( 871) 540  145 ( 145)

Starting with 69 produces a chain of five non-repeating terms, but the longest non-repeating chain with a starting number below one million is sixty terms.

How many chains, with a starting number below one million, contain exactly sixty non-repeating terms?

题目大意:

数字145有一个著名的性质:其所有位上数字的阶乘和等于它本身。

1! + 4! + 5! = 1 + 24 + 120 = 145

169不像145那么有名,但是169可以产生最长的能够连接回它自己的数字链。事实证明一共有三条这样的链:

169  363601  1454  169 871  45361  871 872  45362  872

不难证明每一个数字最终都将陷入一个循环。例如:

69  363600  1454  169  363601 ( 1454) 78  45360  871  45361 ( 871) 540  145 ( 145)

从69开始可以产生一条有5个不重复元素的链,但是以一百万以下的数开始,能够产生的最长的不重复链包含60个项。

一共有多少条以一百万以下的数开始的链包含60个不重复项?

//(Problem 74)Digit factorial chains
// Completed on Tue, 18 Feb 2014, 04:21
// Language: C11
//
// 版权所有(C)acutus (mail: acutus@126.com)
// 博客地址:http://www.cnblogs.com/acutus/
#include<stdio.h>
#include<math.h>
#include<stdbool.h> #define N 1000000
long long fac[]; //保存1~ 9阶乘的数组 long long factorial(int n) //计算阶乘函数
{
if(n == || n == ) return ;
else return n * factorial(n - );
} void init() //初始化数组
{
int i;
for(i = ; i <= ; i++) {
fac[i] = factorial(i);
}
} long long sum(long long n) //计算整数n各位的阶乘的和
{
int ans = ;
while(n) {
ans += fac[n % ];
n /= ;
}
return ans;
} bool fun(int n)
{
int i, count, t;
bool flag = false;
count = ;
while() {
switch(n) {
case : count += ; flag = true; break;
case : count += ; flag = true; break;
case : count += ; flag = true; break;
case : count += ; flag = true; break;
case : count += ; flag = true; break;
case : count += ; flag = true; break;
case : count += ; flag = true; break;
default: t = sum(n);
if( n == t) {
flag = true;
break;
} else{
n = t;
count++; break;
}
}
if(flag) break;
}
if(count == ) return true;
else return false;
} void solve()
{
int i, count;
count = ;
for(i = ; i <= N; i++) {
if(fun(i)) count++;
}
printf("%d\n", count);
} int main()
{
init();
solve();
return ;
}
Answer:
402

(Problem 74)Digit factorial chains的更多相关文章

  1. (Problem 34)Digit factorials

    145 is a curious number, as 1! + 4! + 5! = 1 + 24 + 120 = 145. Find the sum of all numbers which are ...

  2. (Problem 33)Digit canceling fractions

    The fraction 49/98 is a curious fraction, as an inexperienced mathematician in attempting to simplif ...

  3. (Problem 16)Power digit sum

    215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26. What is the sum of the digits of th ...

  4. (Problem 73)Counting fractions in a range

    Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...

  5. (Problem 42)Coded triangle numbers

    The nth term of the sequence of triangle numbers is given by, tn = ½n(n+1); so the first ten triangl ...

  6. (Problem 41)Pandigital prime

    We shall say that an n-digit number is pandigital if it makes use of all the digits 1 to n exactly o ...

  7. (Problem 70)Totient permutation

    Euler's Totient function, φ(n) [sometimes called the phi function], is used to determine the number ...

  8. (Problem 46)Goldbach's other conjecture

    It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a ...

  9. (Problem 72)Counting fractions

    Consider the fraction, n/d, where n and d are positive integers. If nd and HCF(n,d)=1, it is called ...

随机推荐

  1. [Android]The connection to adb is down, and a severe error has occured.

    在Android开发时,有时我们开启虚拟机执行程序时.会报类似下面错误: [2015-07-19 15:08:29 - TestXiaoYanLibrary] The connection to ad ...

  2. Android TextView drawableLeft 在代码中实现

    方法1 Drawable drawable= getResources().getDrawable(R.drawable.drawable); /// 这一步必须要做,否则不会显示. drawable ...

  3. Android Studio插件之FindBugs

    1.安装方法: AndroidStudio->Settigns->Plugins->Browse repositories->search "findBUgs-IDE ...

  4. 锁·——lock关键字详解

    作  者:刘铁猛 日  期:2005-12-25 关键字:lock 多线程 同步 小序 锁者,lock关键字也.市面上的书虽然多,但仔细介绍这个keyword的书太少了.MSDN里有,但所给的代码非常 ...

  5. BZOJ 4000: [TJOI2015]棋盘( 状压dp + 矩阵快速幂 )

    状压dp, 然后转移都是一样的, 矩阵乘法+快速幂就行啦. O(logN*2^(3m)) ------------------------------------------------------- ...

  6. 在C#调用C++的DLL方法(一)生成非托管dll

    C#与C/C++相比,前者的优势在于UI,后者的优势在于算法,C++下的指针虽然恶心,若使用得当还是相当方便的,最重要的问题是,市面上很多流行的开发工具库,几乎没有不支持C++的,但全面支持C#只能说 ...

  7. 收集的jquery插件

    1.精美jQuery分页插件 带滑动条分页使用HTML5实现刮刮卡效果 今天开始我们来收集一些jQuery分页插件,今天第一款jQuery分页插件适用于长翻页列表的分页应用,因为这款分页插件带有滑动条 ...

  8. 原生js动态改变dom高度

    item参数为要改变高度的dom,maxHight参数为dom的最大高度,speed参数为改变高度的速度function addHeight(item,maxHight,speed){ var ite ...

  9. Java处理java.util.ConcurrentModificationException异常

    代码: public static void reduce(HashMap<String, Integer> hashMap, final Integer count) { Iterato ...

  10. eclipse更改主题

    长期使用eclipse,导致视觉疲劳,就想着能否换个主题调节调节. 通过设置window>preferences>appearance设置theme,貌似不起作用. 一查,发现一个绝佳的网 ...