#include <iostream>
#include <vector>
#include <cmath>
using namespace std;
vector<int> v;
vector<int> dp;
// 判断素数
bool f(int x)
{
if (x == 1)
return 0;
for (int i = 2; i <= sqrt(x); i++)
{
if (x % i == 0)
return 0;
}
return 1;
}
vector<pair<int, int>> ans;
void dfs(int k)
{
int sum = 0;
int x = k;
// 迭代
while (x)
{
sum += ((x % 10) * (x % 10));
x /= 10;
}
// 看是否循环
for (auto e : v)
{
if (e == sum)
return;
}
// 因为经过的数都不是幸福数
v.push_back(k);
if (sum != 1)
dfs(sum);
else if (sum == 1)
{
// 这些数都不是幸福数
for (auto e : v)
dp[e] = -1;
// 第一个是哦(暂时来说)
dp[v[0]] = 1;
int len = v.size();
if (f(v[0]))
{
len *= 2;
}
ans.push_back({v[0], len});
}
}
int main()
{
int n, m;
cin >> n >> m;
dp.resize(10000);
for (int i = n; i <= m; i++)
{
v.clear();
if (dp[i] != -1)
{
dfs(i);
}
}
if (ans.empty())
cout << "SAD";
for (auto e : ans)
{
if (dp[e.first] != -1)
cout << e.first << ' ' << e.second << endl;
}
return 0;
}
06-18
10-04
2290