题目描述

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?

                                        

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.也就是说每次搜索记忆化搜索就可。

具体看代码就懂了。

 #include<bits/stdc++.h>
using namespace std;
const int N = 1e4+;
typedef long long ll;
int prime[N],cnt;
int dp[N],vis[N];
bool Is_prime[N];
void Euler(){
memset( Is_prime , true , sizeof Is_prime );
Is_prime[] = false ;
int tot = ;
for(ll i=;i<N;i++){
if( Is_prime[i] ){
prime[tot++] = i;
for(int j=i*;j<N;j+=i){
Is_prime[j] = false;
}
}
}
cnt = tot;
}
int F(int x){
int res = ;
while( x ){
res += (x%)*(x%);
x /= ;
}
return res ;
}
int dfs(int x){
//printf("%d —— \n",x);
if( x == ) return dp[x] = ;
if( vis[x] ) return dp[x] ;
if( vis[x] == ){
vis[x] = ;
return dp[x] = dfs(F(x));
}
}
void Mark(int x){
if( x == ) return ;
dp[x] = ;
Mark( F(x) );
}
int main()
{ Euler();
/*
for(int i=0;i<10;i++){
printf("%d\n",prime[i]);
}
*/
int T,kase,n;
scanf("%d",&T);
vis[] = dp[] = ;
while(T--){
scanf("%d%d",&kase,&n);
if( Is_prime[n] ){
int t = dfs(n);
if( t ){
Mark(n);
printf("%d %d YES\n",kase,n);
}else{
printf("%d %d NO\n",kase,n);
}
}else{
printf("%d %d NO\n",kase,n);
}
}
return ;
}

【记忆化搜索】Happy Happy Prime Prime的更多相关文章

  1. UVa 11762 Race to 1 (数学期望 + 记忆化搜索)

    题意:给定一个整数 n ,然后你要把它变成 1,变换操作就是随机从小于等于 n 的素数中选一个p,如果这个数是 n 的约数,那么就可以变成 n/p,否则还是本身,问你把它变成 1 的数学期望是多少. ...

  2. UVA-11761-马尔可夫/记忆化搜索

    https://vjudge.net/problem/UVA-11762 给出一个整数n,每次随机挑选一个小于等于n的素数,如果是n的因子,n变为n/x ,否则不变,问n变为1的期望挑选次数. f[i ...

  3. uva 11762 数学期望+记忆化搜索

    题目大意:给一个正整数N,每次可以在不超过N的素数中随机选择一个P,如果P是N的约数,则把N变成N/p,否则N不变,问平均情况下需要多少次随机选择,才能把N变成1? 分析:根据数学期望的线性和全期望公 ...

  4. 洛谷 p2618 数字工程 记忆化搜索_ 线性筛

    我们在线筛的同时处理出每个数的所有质因子,记忆化搜索的时候直接枚举质因子即可. 时间复杂度为 O(nlogn)O(nlogn)O(nlogn) Code: #include<cstdio> ...

  5. Uva11762 Race to 1——有向无环图&&记忆化搜索

    题意 给出一个整数 $N$,每次可以在不超过 $N$ 的素数中等概率随机选择一个 $P$,如果 $P$ 是 $N$ 的约数,则把 $N$ 变成 $N/P$,否则 $N$ 不变.问平均情况下需要多少次随 ...

  6. [ACM_动态规划] 数字三角形(数塔)_递推_记忆化搜索

    1.直接用递归函数计算状态转移方程,效率十分低下,可以考虑用递推方法,其实就是“正着推导,逆着计算” #include<iostream> #include<algorithm> ...

  7. 【BZOJ-3895】取石子 记忆化搜索 + 博弈

    3895: 取石子 Time Limit: 1 Sec  Memory Limit: 512 MBSubmit: 263  Solved: 127[Submit][Status][Discuss] D ...

  8. hdu3555 Bomb (记忆化搜索 数位DP)

    http://acm.hdu.edu.cn/showproblem.php?pid=3555 Bomb Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  9. zoj 3644(dp + 记忆化搜索)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 思路:dp[i][j]表示当前节点在i,分数为j的路径条数,从 ...

随机推荐

  1. Editor

    E. Editor 我们把"("用1表示,")"用-1表示,其余字母用0表示,这样形成的一个数组,我们求出它的前缀和sum[],只有当\(sum[n]==0\) ...

  2. oracle传输表空间

    https://blog.csdn.net/ch7543658/article/details/39271135/ Oracle expdp/impdp常用性能优化方法 1.查看操作系统endiann ...

  3. BitmapDrawable

    对Bitmap的一种封装,可以设置它包装的bitmap在BitmapDrawable区域中的绘制方式,有: 平铺填充,拉伸填或保持图片原始大小!以<bitmap>为根节点! 可选属性如下: ...

  4. osg help

    #ifdef _WIN32#include <Windows.h>#endif // _WIN32 #include <osgViewer/Viewer>#include &l ...

  5. CentOS7使用rpm安装mysql5.7

    第一步.前往mysql官网下载所需的版本 Mysql5.7的rpm包下载地址为https://dev.mysql.com/get/Downloads/MySQL-5.7/mysql-5.7.26-1. ...

  6. python用户评论标签匹配的解决方法

    python用户评论标签匹配的解决方法 这篇文章主要为大家详细介绍了python用户评论标签匹配的解决方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下 我们观察用户评论发现:属性词往往和情感词伴 ...

  7. Nonce

    Nonce是或Number once的缩写,在密码学中Nonce是一个只被使用一次的任意或非重复的随机数值. 在加密技术中的初始向量和加密散列函数都发挥着重要作用,在各类验证协议的通信应用中确保验证信 ...

  8. Ubuntu+Django+uWSGI+Nginx部署Django项目

    安装uWSGI,pip依据自己要使用的python版本自行选择,python2.x版本使用pip进行安装,python3.x版本使用pip3进行安装 pip install uwsgi 配置uWSGI ...

  9. Spark源码资料汇总

    近几月,想要了解Spark的内部实现原理,因此想要查阅Spark的源码信息,现将所了解的资料汇总如下: 1. 博客文字类 (1) 官网 1) github 2) Spark官网 (2) gitbook ...

  10. Flutter 圆形/圆角头像图片

    图片显示 1.本地图片 Image.asset加载项目资源包的图片 //先将图片拷贝到项目 images 目录中,然后在 pubspec.yaml文件配置文件相对路径到 assets Image.as ...