题目描述

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. cesium地下模式(地表透明)1

    cesium没有提供地下功能,实现地下模式需要以下三步. 1.修改cesium源码,在GlobeSurfaceTileProvider.js文件里修改一行代码 command.pass = Pass. ...

  2. 使用ICEM绘制非结构网格时,如何提高网格质量?【转载】

    作者:杨淑娟 链接:https://www.zhihu.com/question/20851390/answer/26152732 来源:知乎 著作权归作者所有.商业转载请联系作者获得授权,非商业转载 ...

  3. CTF辅助脚本

    首先推荐这篇文章,网上有多次转载,这是我见过日期比较早的 CTF中那些脑洞大开的编码和加密 凯撒密码 flag='flag{abcdef}' c='' n=20 for i in flag: if ' ...

  4. 拆分项目搞成framework 实例

    目前工作中遇到的问题,是讲项目三大模块拆分, 将Discover.Shop和Events的源代码拆分到独立的项目中 拆分是个麻烦事,里面相互依赖很多 ,打包编译也异常复杂,各种报错 编译Event 遇 ...

  5. java课后实验性问题6

    1.继承条件下的构造方法调用. class Grandparent { public Grandparent(){ System.out.println("GrandParent Creat ...

  6. centos7 设置 mysql 开机自启

    前述 CentOS 7是目前较为流行的Linux发行版本.CentOS 7比起之前版本有了许多的变更.如firewall不在用iptables管理,而交由firewall-cmd管理.同样的,在Cen ...

  7. benchmark在postgresql上的安装及使用

     BenchmarkSQL是一款经典的开源数据库测试工具,内嵌了TPCC测试脚本,可以对EnterpriseDB.PostgreSQL.MySQL.Oracle以及SQL Server等数据库直接进行 ...

  8. php中如何传递Session ID

    一般通过在各个页面之间传递的唯一的 Session ID,并通过 Session ID 提取这个用户在服务器中保存的 Session 变量,来跟踪一个用户.常见的 Session ID 传送方法主要有 ...

  9. 阶段5 3.微服务项目【学成在线】_day04 页面静态化_19-页面静态化-模板管理-模板存储

    因为没有界面,所以手动的通过操作数据库的方式来存储 比如说要存一个轮播图的模板 把自增的_id去掉 先用测试程序把文件存储到gridFS得到一个gridId,然后拿着这个文件的id 这样就添加好了这一 ...

  10. JAVA 基础编程练习题25 【程序 25 求回文数】

    25 [程序 25 求回文数] 题目:一个 5 位数,判断它是不是回文数.即 12321 是回文数,个位与万位相同,十位与千位相同. package cskaoyan; public class cs ...