题目描述

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. jenkins的任务卡住

    今天做jenkins任务的时候,发现一个启动后,一直卡住,在那转圈圈,其实这个时候,任务已经执行完了. 经过分析,因为这个任务是启动一个web服务,直接在机器上执行时,直接占用一个终端. 解决办法,放 ...

  2. cxf报错 Cannot find any registered HttpDestinationFactory from the Bus.

    错误信息:Cannot find any registered HttpDestinationFactory from the Bus. 报错主要是因为缺少jetty依赖 一般添加如下依赖即可 < ...

  3. 【Linux】安装 PostgreSQL

    参考: CSDN1:https://blog.csdn.net/ctwy291314/article/details/79900074 1.进入 PostgreSQL 官网的下载地址, 2.选择下面的 ...

  4. Python下划线命名模式

  5. java获取远程图片分辨率

    package com.haiyisoft.hyoaPc; import java.awt.image.BufferedImage;import java.io.IOException;import ...

  6. mysql查询表里的重复数据方法

    select username,count(*) as count from hk_test group by username having count>1;

  7. Linux通过AIO进行异步读文件

    下面列出源代码: #include <stdio.h> #include <stdlib.h> #include <string.h> #include <a ...

  8. 阶段5 3.微服务项目【学成在线】_day02 CMS前端开发_01-vuejs研究-vuejs介绍

    1.vue.js是什么? Vue (读音 /vjuː/,类似于 view) 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计 为可以自底向上逐层应用.Vue 的核心库只关注视 ...

  9. pcntl_fork()函数说明

    pcntl_fork()函数复制了当前进程的PCB,并向父进程返回了派生子进程的pid,父子进程并行,打印语句的先后完全看系统的调度算法,打印的内容控制则靠pid变量来控制.因为我们知道pcntl_f ...

  10. idea调试jdk1.8源码(最新)

    我们发现如果,直接用idea点项目jdk源码进去后发现自己不能注释说明,非常麻烦,不便阅读记录 于是: 1.在安装的jdk1.8路径下,找到src.zip和javafx-src.zip压缩文件 ,解压 ...