-
Notifications
You must be signed in to change notification settings - Fork 2
/
happyprime.cpp
113 lines (101 loc) · 2.3 KB
/
happyprime.cpp
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#define _USE_MATH_DEFINES
#include <iostream>
#include <stdio.h>
#include <cmath>
#include <iomanip>
#include <vector>
#include <string>
#include <algorithm>
#include <unordered_set>
#include <ctype.h>
#include <queue>
#include <map>
#include <set>
#include <stack>
#include <unordered_map>
#define EPSILON 0.00001
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
using namespace std;
void primeSeive(vector<bool> & primes, long long n)
{
primes[0] = false;
primes[1] = false;
for (int i = 2; i < n; i++)
{
if (primes[i])
{
for (int j = 0; j < n / i; j++)
{
if (i < 10000 && (i*i) + (i * j) < n)
{
primes[(i*i) + (i * j)] = false;
}
}
}
}
}
int main()
{
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
ull i, j, k;
vector<bool> primes(10001, true);
ull cases;
ll num, test;
ll total;
ll temp;
string tempString;
ll state;
primeSeive(primes, 10001);
vector<int> goesToOne(10001, -2); //-2 is untouced, 1 coverges to 1, -1 is inf, 0 is in current q
stack<int> myStack;
goesToOne[1] = 1;
for (i = 2; i < 10001; i++)
{
temp = i;
while (goesToOne[temp] == -2)
{
total = 0;
myStack.push(temp);
goesToOne[temp] = 0;
tempString = to_string(temp);
for (auto thing : tempString)
{
total += pow((thing - '0'), 2);
}
temp = total;
//cout << total << "\n";
}
if (goesToOne[temp] == 1)
{
state = 1;
}
else
{
state = -1;
}
while (myStack.size())
{
goesToOne[myStack.top()] = state;
myStack.pop();
}
}
cin >> cases;
for (i = 0; i < cases; i++)
{
cin >> num >> test;
cout << num << " " << test << " ";
if (goesToOne[test] == 1 && primes[test])
{
cout << "YES\n";
}
else
{
cout << "NO\n";
}
}
return 0;
}