题目描述

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. java试题复盘——9月26日

    5.在 JAVA 编程中, Java 编译器会将 Java 程序转换为(A) A.  字节码 B.  可执行代码 C.  机器代码 D.  以上都不对 解析: 编译器将Java源代码编译成字节码cla ...

  2. ICEM—两孔圆柱

    ​原视频下载地址: https://pan.baidu.com/s/1eSJ7ciQ 密码: 1gj3

  3. springmvc返回json对象

    1.引入jackson的依赖 <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core -- ...

  4. 中国萌兔-月饼工厂流水线 -万圣节萌宠-月饼售罄后续-B站东予薏米

    B站(Blibli)up主,东予薏米.下面画的五只兔兔,两只狗狗,一只猫猫都是她家的 啊!有个会画画的主人真是幸福- 蹦迪的那个兔兔头昏脑胀,敷了冰袋和膏药哈哈哈哈,好可爱! 下班了下班了~今天真是太 ...

  5. 怎样在VMware虚拟机中使用安装并设置Ubuntu系统

    1 2 3 4 5 6 7 分步阅读 Ubuntu 系统是一款优秀的.基于GNU/Linux 的平台的桌面系统. 当然,目前为止很多应用程序还完全不能允许运行在 Ubuntu 系统上,而且 Ubunt ...

  6. 在Vue文件中引用模块的相对路径“@“符号表示什么意思?

    @ 的作用是在你引入模块时,可以使用 @ 代替 /src 目录,避免书写麻烦又易错的相对路径. import model from "@/common/model"; // 默认路 ...

  7. 详解python中@的用法

    python中@的用法 @是一个装饰器,针对函数,起调用传参的作用. 有修饰和被修饰的区别,‘@function'作为一个装饰器,用来修饰紧跟着的函数(可以是另一个装饰器,也可以是函数定义). 代码1 ...

  8. smarty 模板几个例子(变量调节器)

    一.assign和display方法的使用以及几个变量调节器 header("content-type:text/html;charset=utf-8");//加载Smarty引擎 ...

  9. C++ fill fill_n generate generate_n

    #include <iostream>#include <algorithm>#include <vector>#include <list>#incl ...

  10. Go 微服务架构Micro相关概念理解

    Micro是一个微服务框架(或者说是工具集):提供了各类组件,解决微服务架构中的不同问题,服务监控.服务发现.熔断机制,负载均衡等等,自己一个个解决这些问题几乎不可能,这时候就需要借助go-micro ...