【BZOJ 3643】Phi的反函数 数搜索
这道题是典型的数搜索,讲究把数一层一层化小,而且还有最重要的大质数剪枝。
#include <cstdio>
#include <cmath>
typedef long long LL;
int n;
const int N=;
const LL Inf=0x7fffffff;
LL ans;
int len,prime[N];
bool isnot[N];
inline void getprime(){
int lim=;
for(int i=;i<=lim;i++){
if(!isnot[i])prime[++len]=i;
for(int j=;prime[j]*i<=lim;j++){
isnot[prime[j]*i]=;
if(i%prime[j]==)break;
}
}
}
inline bool isprime(int x){
if(x==)return ;
int lim=(int)sqrt(x+0.5);
for(int i=;prime[i]<=lim;i++)
if(x%prime[i]==)
return ;
return ;
}
inline LL Min(LL x,LL y){
return x<y?x:y;
}
void dfs(int pos,LL now,int rest){
if(pos>=len)return;
if(now>=ans)return;
if(rest==){ans=now;return;}
if(isprime(rest+)){ans=Min(ans,now*(rest+));return;}
LL have=prime[pos]-,j=prime[pos];
if(pos==)have<<=,j<<=;
while(rest%have==)
dfs(pos+,now*j,rest/have),have*=prime[pos],j*=prime[pos];
dfs(pos+,now,rest);
}
int main(){
getprime(),scanf("%d",&n),ans=Inf+;
if(n<=){printf("-1");return ;}
if(n==){printf("");return ;}
dfs(,,n);
printf("%lld",ans==Inf+?-:ans);
}
【BZOJ 3643】Phi的反函数 数搜索的更多相关文章
- [BZOJ]3643 Phi的反函数
我承认开这篇文章只是因为好笑…… 估计Zky神看见3737会很郁闷吧. http://www.lydsy.com/JudgeOnline/problem.php?id=3643 本来想直接交3737改 ...
- 【BZOJ-3643】Phi的反函数 数论 + 搜索
3643: Phi的反函数 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 141 Solved: 96[Submit][Status][Discuss ...
- [BZOJ 3930] [CQOI 2015]选数(莫比乌斯反演+杜教筛)
[BZOJ 3930] [CQOI 2015]选数(莫比乌斯反演+杜教筛) 题面 我们知道,从区间\([L,R]\)(L和R为整数)中选取N个整数,总共有\((R-L+1)^N\)种方案.求最大公约数 ...
- 【BZOJ 3642】Phi的反函数
http://www.lydsy.com/JudgeOnline/problem.php?id=3643 因为\[\varphi(n)=\prod_i p_i^{k_i-1}(p_i-1),n=\pr ...
- bzoj3643 Phi的反函数
传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3643 [题解] n = p1^a1*p2^a2*...*pm^am phi(n) = p1( ...
- [BZOJ 1026] [SCOI 2009] Windy数 【数位DP】
题目链接:BZOJ - 1026 题目分析 这道题是一道数位DP的基础题,对于完全不会数位DP的我来说也是难题.. 对于询问 [a,b] 的区间的答案,我们对询问进行差分,求 [0,b] - [0,a ...
- [BZOJ 1085] [SCOI2005] 骑士精神 [ IDA* 搜索 ]
题目链接 : BZOJ 1085 题目分析 : 本题中可能的状态会有 (2^24) * 25 种状态,需要使用优秀的搜索方式和一些优化技巧. 我使用的是 IDA* 搜索,从小到大枚举步数,每次 DFS ...
- ●BZOJ 4408 [Fjoi 2016]神秘数
题链: http://www.lydsy.com/JudgeOnline/problem.php?id=4408 题解: 主席树 首先,对于一些数来说, 如果可以我们可以使得其中的某些数能够拼出 1- ...
- 【bzoj 3233】[Ahoi2013]找硬币 ——搜索
Description 小蛇是金融部部长.最近她决定制造一系列新的货币.假设她要制造的货币的面值为x1,x2,x3… 那么x1必须为1,xb必须为xa的正整数倍(b>a).例如 1,5,125, ...
随机推荐
- python-生成器单线程并发(简单案例)
#!/usr/local/bin/python3 # -*- coding:utf-8 -*- import time # ----------示例---------- def consumer(na ...
- [Cracking the Coding Interview] 4.5 Validate BST
Implement a function to check if a binary tree is a binary search tree. 这道题很经典,让我们判断一棵树是不是二叉查找树.但是首先 ...
- 隐式Dijkstra:在状态集合中用优先队列求前k小
这种技巧是挺久以前接触的了,最近又突然遇到几道新题,于是总结了一下体会. 这种算法适用的前提是,标题所述的"状态集合"大到不可枚举(否则枚举就行了qaq) ...
- oracle 11g XE 学习版添加scott用户方法全步骤
安装企业版的orcale是不是太费时费力了?若只是学习用途的话,不妨试试轻便版的XE版本,同样是官网下载的,但是这个安装起来比完整版简便多了. 首先,你得先安装好orcale 11g XE 版本:(这 ...
- jetbraints激活码
G91XMO9AVI-eyJsaWNlbnNlSWQiOiJHOTFYTU85QVZJIiwibGljZW5zZWVOYW1lIjoic29uZyB3YW5nIiwiYXNzaWduZWVOYW1lI ...
- Solution to LeetCode Problem Set
Here is my collection of solutions to leetcode problems. Related code can be found in this repo: htt ...
- java通过句柄访问对象
在Java里,任何东西都可看作对象.尽管将一切都“看作”对象,但操纵的标识符实际是指向一个对象的“句柄”(Handle),有的人将其称作一个“引用”,甚至一个“指针”. 主类型的数据成员可直接初始化, ...
- jmeter对请求参数的签名处理
1.首先在本地写好签名的算法,本文采用RSA签名. public final class Base64 { static private final int BASELENGTH = 128; sta ...
- ES5新增数组方法(1):filter
检测数组元素,并返回符合条件所有元素的数组. 1.过滤数组中不符合条件的元素 let arr = [1, 2, 3, 4, 5, 6]; // 方式一 let newArr = arr.filter( ...
- (干货分享)mac python+appium环境搭建
因为mac本自带python2.x(不建议卸载,因为本本本身有很多依赖与此),所以装python3的过程极其坎坷,勉强装好后也总是各种报错.这次装appium环境,直接把原来的python3卸了,用h ...