【记忆化搜索】Happy Happy Prime Prime

题目描述

RILEY VASHTEE: [reading from display] Find the next number in the sequence:
313 331 367 ...? What?
THE DOCTOR: 379.
MARTHA JONES: What?
THE DOCTOR: It’s a sequence of happy primes — 379.
MARTHA JONES: Happy what?
THE DOCTOR: Any number that reduces to one when you take the sum of the square of its digits and continue iterating it until it yields 1 is a happy number. Any number that doesn’t, isn’t. A happy prime is both happy and prime.
THE DOCTOR: I dunno, talk about dumbing down. Don’t they teach recreational mathematics anymore?
Excerpted from “Dr. Who”, Episode 42 (2007).
The number 7 is certainly prime. But is it happy?
                                        【记忆化搜索】Happy Happy Prime Prime It is happy :-). As it happens, 7 is the smallest happy prime. Please note that for the purposes of this problem, 1 is not prime.
For this problem you will write a program to determine if a number is a happy prime.

 

输入

The first line of input contains a single integer P, (1 ≤ P ≤ 1000), which is the number of data sets that follow. Each data set should be processed identically and independently.
Each data set consists of a single line of input. It contains the data set number, K, followed by the happy prime candidate, m, (1 ≤ m ≤ 10000).

 

输出

For each data set there is a single line of output. The single output line consists of the data set number,K, followed by a single space followed by the candidate, m, followed by a single space, followed by ‘YES’or ‘NO’, indicating whether m is a happy prime.

 

样例输入

4
1 1
2 7
3 383
4 1000

样例输出

1 1 NO
2 7 YES
3 383 YES
4 1000 NO

 

【题解】

分析一下,其实最多也就5位,每个位置上都是9,也就是81*5=405.也就是说每次搜索记忆化搜索就可。

具体看代码就懂了。

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int N = 1e4+200;
 4 typedef long long ll;
 5 int prime[N],cnt;
 6 int dp[N],vis[N];
 7 bool Is_prime[N];
 8 void Euler(){
 9     memset( Is_prime , true , sizeof Is_prime );
10     Is_prime[1] = false ;
11     int tot = 0 ;
12     for(ll i=2;i<N;i++){
13         if( Is_prime[i] ){
14             prime[tot++] = i;
15             for(int j=i*2;j<N;j+=i){
16                 Is_prime[j] = false;
17             }
18         }
19     }
20     cnt = tot;
21 }
22 int F(int x){
23     int res = 0 ;
24     while( x ){
25         res += (x%10)*(x%10);
26         x /= 10;
27     }
28     return res ;
29 }
30 int dfs(int x){
31     //printf("%d —— \n",x);
32     if( x == 1 ) return dp[x] = 1 ;
33     if( vis[x] ) return dp[x] ;
34     if( vis[x] == 0 ){
35         vis[x] = 1;
36         return dp[x] = dfs(F(x));
37     }
38 }
39 void Mark(int x){
40     if( x == 1 ) return ;
41     dp[x] = 1 ;
42     Mark( F(x) );
43 }
44 int main()
45 {
46  
47     Euler();
48     /*
49     for(int i=0;i<10;i++){
50         printf("%d\n",prime[i]);
51     }
52     */
53     int T,kase,n;
54     scanf("%d",&T);
55     vis[1] = dp[1] = 1 ;
56     while(T--){
57         scanf("%d%d",&kase,&n);
58         if( Is_prime[n] ){
59             int t = dfs(n);
60             if( t ){
61                 Mark(n);
62                 printf("%d %d YES\n",kase,n);
63             }else{
64                 printf("%d %d NO\n",kase,n);
65             }
66         }else{
67             printf("%d %d NO\n",kase,n);
68         }
69     }
70     return 0 ;
71 }

 

上一篇:敏捷宣言诞生记(2001年2月11日)


下一篇:JavaWeb5.3【JDBC:案例1】