题目描述

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. C#控制台输入输出

    C#控制台输入输出 Console.Read()方法: //从控制台窗口读取一个字符,返回int值 Console.ReadLine()方法: // 从控制台窗口读取一行文本,返回string值 Co ...

  2. jenkins之自动化部署github上maven项目

    部署流程:将代码从github上拉取下来,使用maven打包,将打包后的jar通过ssh发送到服务器上,然后构建docker镜像,运行容器. 1.安装插件 如果是第一次使用jenkins,需要检查并确 ...

  3. 修复grub rescue问题

    前几天,手欠点了下win10的系统升级,直接从17.09升级到了19.3虽然也有些波折,总体顺利,以为一切都完事大吉之时,重启系统,原来,万恶的win10给我挖好了坑,早等着我呢.我去,千万只cnm脑 ...

  4. C#winform如何实现文本编辑框(TextBox)的Hint提示文字效果

    C#winform如何实现文本编辑框(TextBox)的Hint提示文字效果 private const int EM_SETCUEBANNER = 0x1501; [DllImport(" ...

  5. Nginx内置变量及正则语法

    对于很多Nginx初学者来说,配置文件是必须要看懂的.但是当公司的Nginx配置文件放在你面前的时候你总会被一些带着"$"符号和一大推看不懂的的正则给正懵逼.没错带着"$ ...

  6. Flutter移动电商实战 --(19)首页_火爆专区商品接口制作

    Dart中可选参数的设置 上节课在作通用方法的时候,我们的参数使用了一个必选参数,其实我们可以使用一个可选参数.Dart中的可选参数,直接使用“{}”(大括号)就可以了.可选参数在调用的时候必须使用p ...

  7. 五一 DAY 4

    DAY 4    2019.5.1 PART 1    进制转化 10 = 23+21= 1010(2)       = 32+30= 101(3) 进制转化常见问题: 1.十进制数 x ----&g ...

  8. SQL-W3School-高级:SQL ALIAS(别名)

    ylbtech-SQL-W3School-高级:SQL ALIAS(别名) 1.返回顶部 1. 通过使用 SQL,可以为列名称和表名称指定别名(Alias). SQL Alias 表的 SQL Ali ...

  9. Python - pytesseract 机器视觉

    机器视觉  - tesseract ( 验证码 ) 安装 Ubuntu sudo apt-get install tesseract-ocr Windows 下载安装包 添加环境变量(Path) :搜 ...

  10. ajax结合mysql数据库和smarty实现局部数据状态的刷新

    效果状态:通过点击锁定状态实现状态锁定与不锁定之间的切换 1.主程序:01.php导入smarty和mysql类,取得数据导入列表模板 <?php    include './include/M ...