UVa 10837 A Research Problem 欧拉函数
题意:
给你一个欧拉函数值 phi(n),问最小的n是多少。 phi(n) <= 100000000 , n <= 200000000
解题思路:
对于欧拉函数值可以写成
这里的k有可能是等于0的,所以不能直接将phi(n)分解质因子。但是可以知道(Pr - 1)是一定存在的,那就直接枚举素数,满足phi(n) % (Pr-1)的都加进去,然后对这些素数进行爆搜。。。说到底还是暴力啊。。。想不到什么巧妙的办法了,最后需要注意的是,一遍枚举完各个素数后phi(n)除后还剩now,现在要判断(now+1)是否为素数,还是保证这个素数前面没有访问过。具体实现过程见代码~
/* **********************************************
Author : JayYe
Created Time: 2013/9/25 0:00:42
File Name : JayYe.cpp
*********************************************** */ #include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; const int maxp = 10000 + 10;
bool vis[maxp], done[222];
int pri[maxp], pnum, cur_p[555], cnt_p[555]; void get_prime(int n) {
vis[1] = 1;
for(int i = 2;i*i <= n; i++) if(!vis[i])
for(int j = i*i;j <= n;j += i) vis[j] = 1;
pnum = 0;
for(int i = 2;i <= n; i++) if(!vis[i])
pri[pnum++] = i;
} int tot, ans; void split(int n) {
tot = 0;
for(int i = 0;i < pnum && (pri[i]-1)*(pri[i]-1) <= n; i++) if(n % (pri[i]-1) == 0) {
cur_p[tot++] = pri[i];
}
} int judge(int n) {
if(n == 1) return n;
n++;
// 判断剩余的值 + 1是否为素数
for(int i = 0;i < pnum && pri[i]*pri[i] <= n; i++) if(n % pri[i] == 0)
return -1;
for(int i = 0;i < tot; i++) if(vis[i] && n == cur_p[i]) // 判断这个素数是否已访问过
return -1;
return n;
} //left表示当前的n的值,now表示phi(n)剩余值
void dfs(int left, int now, int c) {
if(c == tot) {
int ret = judge(now);
// printf("left = %d now = %d ret = %d\n", left, now, ret);
if(ret > 0)
ans = min(ans, left*ret);
return ;
}
dfs(left, now, c+1);
if(now % (cur_p[c]-1) == 0) {
vis[c] = 1;
left *= cur_p[c];
now /= cur_p[c] - 1;
while(true) {
dfs(left, now, c+1);
if(now % cur_p[c]) return ;
now /= cur_p[c]; left *= cur_p[c];
}
vis[c] = 0;
}
} void solve(int n) {
memset(done, false, sizeof(done));
ans = 2000000000;
split(n);
dfs(1, n, 0);
} int main() {
get_prime(10000);
int n, cas = 1;
while(scanf("%d", &n) != -1 && n) {
solve(n);
printf("Case %d: %d %d\n", cas++, n, ans);
}
return 0;
}
UVa 10837 A Research Problem 欧拉函数的更多相关文章
- uva 10837 - A Research Problem(欧拉功能+暴力)
题目链接:uva 10837 - A Research Problem 题目大意:给定一个phin.要求一个最小的n.欧拉函数n等于phin 解题思路:欧拉函数性质有,p为素数的话有phip=p−1; ...
- UVA 11424 GCD - Extreme (I) (欧拉函数+筛法)
题目:给出n,求gcd(1,2)+gcd(1,3)+gcd(2,3)+gcd(1,4)+gcd(2,4)+gcd(3,4)+...+gcd(1,n)+gcd(2,n)+...+gcd(n-1,n) 此 ...
- UVA 11426 GCD - Extreme (II) (欧拉函数+筛法)
题目链接:http://acm.hust.edu.cn/vjudge/contest/view.action?cid=70017#problem/O 题意是给你n,求所有gcd(i , j)的和,其中 ...
- UVA 11426 GCD - Extreme (II)(欧拉函数打表 + 规律)
Given the value of N, you will have to find the value of G. The definition of G is given below:Here ...
- poj 2480 Longge's problem [ 欧拉函数 ]
传送门 Longge's problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7327 Accepted: 2 ...
- UVA 11426 - GCD - Extreme (II) 欧拉函数-数学
Given the value of N, you will have to find the value of G. The definition of G is given below:G =i< ...
- UVa 10820 (打表、欧拉函数) Send a Table
题意: 题目背景略去,将这道题很容易转化为,给出n求,n以内的有序数对(x, y)互素的对数. 分析: 问题还可以继续转化. 根据对称性,我们可以假设x<y,当x=y时,满足条件的只有(1, 1 ...
- UVa 10214 (莫比乌斯反演 or 欧拉函数) Trees in a Wood.
题意: 这道题和POJ 3090很相似,求|x|≤a,|y|≤b 中站在原点可见的整点的个数K,所有的整点个数为N(除去原点),求K/N 分析: 坐标轴上有四个可见的点,因为每个象限可见的点数都是一样 ...
- UVA 11426 GCD - Extreme (II) 欧拉函数
分析:枚举每个数的贡献,欧拉函数筛法 #include <cstdio> #include <iostream> #include <ctime> #include ...
随机推荐
- 用Lighttpd做图片服务器
http://www.lsanotes.cn/install_lighttpd 用Lighttpd做图片服务器 一.安装lighttpd所需的库文件1.安装 pcrewgetftp://ftp.csx ...
- ThinkPHP 3.2版本 , 无法读取$_SESSION['verify_code']
官方网站上写的是: 生成的验证码信息会保存到session中,包含的数据有: array('verify_code'=>'当前验证码的值','verify_time'=>'验证码生成的时间 ...
- Android程序版本更新--通知栏更新下载安装(转)
Android应用检查版本更新后,在通知栏下载,更新下载进度,下载完成自动安装,效果图如下: 检查当前版本号 AndroidManifest文件中的versionCode用来标识版本,在服务器放一个新 ...
- 设置listview的header不能点击
View headView = inflater.inflate(R.layout.search_top, null); mListView.addHeaderView(headView ,null, ...
- sql server 2008 评估期已过期
解决办法: 修改注册表: HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Microsoft SQL Server\100\ConfigurationState里的&quo ...
- Activity中setResult(int resultCode, Intent data)与onActivityResult(int requestCode, int resultCode, Intent data)方法的调用
关于Activity的生命周期 onCreate(Bundle savedInstanceState):可以进行一些初始化的工作在activity第一次被创建的时候调用.这里是你做所有初始化设置的地方 ...
- 【转】mysql数据库中实现内连接、左连接、右连接
[转]mysql数据库中实现内连接.左连接.右连接 内连接:把两个表中数据对应的数据查出来 外连接:以某个表为基础把对应数据查出来 首先创建数据库中的表,数据库代码如下: /* Navicat MyS ...
- (多对象)Json转换成List
写的不好,请大家见谅. 1.Json 格式{"packages":[{“type”:”aaa”}],"zone_packages":[{"ticket ...
- (转)教你如何使用php session
学会php session可以在很多地方使用,比如做一个后台登录的功能,要让程序记住用户的session,其实很简单,看了下面的文章你就明白了. PHP session用法其实很简单它可以把用 ...
- Cacti以MB为单位监控流量
Cacti自带的流量监控阀值模板为“Interface – Traffic”,只能监控bytes,在添加阀值之后,报警的流量信息以bytes为单位,查看很不友好,可以通过以下方法将btyes转换成MB ...