uva 10837 - A Research Problem(欧拉功能+暴力)
题目链接:uva 10837 - A Research Problem
题目大意:给定一个phin。要求一个最小的n。欧拉函数n等于phin
解题思路:欧拉函数性质有,p为素数的话有phip=p−1;假设p和q互质的话有phip∗q=phip∗phiq
然后依据这种性质,n=pk11(p1−1)∗pk22(p2−1)∗⋯∗pkii(pi−1),将全部的pi处理出来。暴力搜索维护最小值,尽管看上去复杂度很高,可是由于对于垒乘来说,增长很快,所以搜索范围大大被缩小了。
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxp = 1e4;
const int INF = 0x3f3f3f3f;
int ans;
int np, vis[maxp+5], pri[maxp+5];
int nf, fact[maxp+5], v[maxp+5];
void prime_table (int n) {
np = 0;
for (int i = 2; i <= n; i++) {
if (vis[i])
continue;
pri[np++] = i;
for (int j = i * i; j <= n; j += i)
vis[j] = 1;
}
}
void get_fact (int n) {
nf = 0;
for (int i = 0; i < np && (pri[i]-1) * (pri[i]-1) <= n; i++) {
if (n % (pri[i]-1) == 0)
fact[nf++] = pri[i];
}
}
bool judge (int n) {
if (n == 2)
return true;
for (int i = 0; i < np && pri[i] * pri[i] <= n; i++)
if (n % pri[i] == 0)
return false;
for (int i = 0; i < nf; i++)
if (v[i] && fact[i] == n)
return false;
return true;
}
void dfs (int ret, int cur, int d) {
if (d == nf) {
if (judge(cur+1)) {
if (cur == 1)
cur = 0;
ans = min(ans, ret * (cur+1));
}
return;
}
dfs(ret, cur, d+1);
if (cur % (fact[d]-1) == 0) {
v[d] = 1;
ret *= fact[d];
cur /= (fact[d]-1);
while (true) {
dfs(ret, cur, d+1);
if (cur % fact[d])
return;
ret *= fact[d];
cur /= fact[d];
}
v[d] = 0;
}
}
int solve (int n) {
ans = INF;
get_fact(n);
memset(v, 0, sizeof(v));
dfs(1, n, 0);
return ans;
}
int main () {
prime_table(maxp);
int cas = 1, n;
while (scanf("%d", &n) == 1 && n) {
printf("Case %d: %d %d\n", cas++, n, solve(n));
}
return 0;
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
uva 10837 - A Research Problem(欧拉功能+暴力)的更多相关文章
- UVa 10837 A Research Problem 欧拉函数
题意: 给你一个欧拉函数值 phi(n),问最小的n是多少. phi(n) <= 100000000 , n <= 200000000 解题思路: 对于欧拉函数值可以写成 这里的k有可能是 ...
- UVA 10837 A Research Problem
https://vjudge.net/problem/UVA-10837 求最小的n,使phi(n)=m #include<cstdio> #include<algorithm> ...
- 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)的和,其中 ...
- TOJ 3151: H1N1's Problem(欧拉降幂)
传送门:http://acm.tzc.edu.cn/acmhome/problemdetail.do?&method=showdetail&id=3151 时间限制(普通/Java): ...
- POJ 2480 Longge's problem 欧拉函数—————∑gcd(i, N) 1<=i <=N
Longge's problem Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6383 Accepted: 2043 ...
- 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< ...
随机推荐
- yii配置
一.在config/web.php中添加如下代码(开启debug工具和gii工具) if (YII_ENV_DEV) { // configuration adjustments for 'dev' ...
- C语言之固定格式输出当前时间
固定格式输出当前时间 编程实现将当前时间用以下形式输出:星期 月 日 小时:分:秒 年 代码如下: #include<stdio.h>#include<stdlib.h& ...
- openrisc 之 Wishbone总线学习笔记——总线互联
一,总线命名规范 1,wishbone总线接口信号都是高电平有限 2,wishbone接口信号都是以 _i ,或者是 _o 结束.i表示输入, o表示输出. ()表示该信号为总线信号,总线位宽可以大于 ...
- DataTable Select查询
如果是Excel中数据,空数据为DBNull.Value 查询:dt.Select("name is null") 查询长度:dt.Select("len(convert ...
- 经验总结:按需加载JS和css
项目中做过这样的事情:所有页面都通过SSI指令 include这样一份public-js.shtml, 用来引入涉及到的js(包括公共的脚本 验证插件 自定义组件等),但是一些没有交互效果的页面根本不 ...
- mysql基础(mysql数据库导入到处) 很基础很实用
一.MYSQL的命令行模式的设置:桌面->我的电脑->属性->环境变量->新建->PATH=“:path\mysql\bin;”其中path为MYSQL的安装路径.二.简 ...
- C++动态数组的实现
#include <iostream> using namespace std; int main() { int n; while(cin>>n) { ]; p[]=; p[ ...
- [置顶] PHP开发实战权威指南-读书总结
从今年开始,断断续续学习PHP已经有4个月了. 最初,认真学习PHP几天,就弄WordPress搭建了一个个人博客,这也符合技术人的实践理念. 最近,重温PHP开发实战权威指南,做点总结,整理下自己学 ...
- ORA-00845 Oracle 启不来修改 MEMORY_TARGET
1.内存减小导致Oracle启动不了 Last login: Sun Nov 4 15:09:06 2012 from 192.168.5.222 [oracle@h1 ~]$ sqlplus ...
- 11427 - Expect the Expected(概率期望)
11427 - Expect the Expected Some mathematical background. This problem asks you to compute the expec ...