-
Notifications
You must be signed in to change notification settings - Fork 0
/
happyprime.c
96 lines (85 loc) · 1.26 KB
/
happyprime.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/**
* Kattis problem (name)
*
* Difficulty: 0.0
* Solved: no
*/
#define UNSET 0
#define VISIT 1
#define HAPPY 2
#define SAD 3
int sieve[10001];
int happy[10001];
void init(){
memset(sieve,1,10001*sizeof(int));
memset(happy,0,10001*sizeof(int));
sieve[0]=0;
sieve[1]=0;
for(int i=0;i<123;i++){
if(sieve[i]==0) continue;
for(int j=2*i;j<10001;j+=i){
sieve[j]=0;
}
}
happy[0]=SAD;
happy[1]=HAPPY;
happy[4]=SAD;
for(int i=0;i<10001;i++){
is_happy(i);
}
}
int next(int s){
char m[8];
sprintf(m,"%d",s);
char *p=m;
int accum=0;
while(*p){
int n=(*p)-'0';
accum+=n*n;
p++;
}
return accum;
}
int is_happy(int i){
switch(happy[i]){
case HAPPY:
return 1;
case SAD:
return 0;
case VISIT:
// we got caught in a loop!
return 0;
case UNSET:
// calculate next value
happy[i]=VISIT;
int nextI = next(i);
if(is_happy(nextI)){
happy[i]=HAPPY;
return 1;
} else {
happy[i]=SAD;
return 0;
}
default:
return 0;
}
}
int main(){
init();
int n;
scanf("%d",&n);
for(int i=0;i<n;i++){
int x;
scanf("%*d");
scanf("%d",&x);
printf("%d %d ",i+1,x);
if(sieve[x]&&is_happy(x)){
puts("YES");
} else {
puts("NO");
}
}
}