题目

Description

__int64 ago,there's a heaven cow called sjy...

A god bull named wzc fell in love with her...

As an OI & MOer,wzc gave sjy a quesiton...

给定一个整数n,求一个整数m,满足m<=n,并且m/phi(m)的值最大。

注:phi(m)代表m的欧拉函数,即不大于m且与m互质的数的个数。

Input

第一行是一个整数T,表示该测试点有T组数据。

接下来T行,每行一个整数n,意义如上所述。

Output

输出一共T行,每行一个整数m。

若对于某个n,有不止一个满足条件的m,则输出最小的m。

Sample Input

1
10

Sample Output

6

Data Constraint

对于10%的数据, n<=1000

对于30%的数据, n<=10^10

对于60%的数据, n<=10^2000

对于100%的数据,T<=100,n<=10^25000。


分析

设pi为质数,m=p1e1·p2e2·p3^e3····。

我们首先来化简一下m/φ(m),容易得出m/φ(m)=(p1-1)(p2-1)(p3-1)···/p1·p2·p3····。

易证当p2>p1时,(p1-1)/p1>(p2-1)/p2,如果m取前k个质数的乘积,答案一定更优。答案就是最大的p1·p2·p3····。

如果边做边求p1·p2·p3····显然是不行的,所以先预处理p1·p2·p3····。大概60000以内的质数就可以,有6057个。为了缩短时间,高精度我压了11位,有点小恶心,事实上速度还是挺快的。

#include <cmath>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <queue>
const long long maxlongint=2147483647;
const long long mo=100000000000;
using namespace std;
long long b[6500][2500],zs[6500],t,n,m,a[2500];
char s[100000];
int times(long long x,long long y,long value)
{
long i,j,k,l;
for(i=1;i<=b[x][0];i++)
{
b[y][i]+=b[x][i]*value;
b[y][i+1]+=b[y][i]/mo;
b[y][i]%=mo;
}
b[y][0]=b[x][0];
if(b[y][b[y][0]+1]>0)
b[y][0]++;
}
int bj(long long a[2500],long long b[2500])
{
if(b[0]>a[0]) return true;
if(b[0]<a[0]) return false;
else
{
for(long long i=b[0];i>=1;i--)
{
if(b[i]>a[i]) return true;
if(b[i]<a[i]) return false;
}
}
return false;
}
int main()
{
scanf("%lld\n",&t);
long long i,j,k,l,x,y;
for(i=2;i<=60000;i++)
{
bool q=true;
for(j=2;j<=(long long)(sqrt(i));j++)
{
if(!(i%j)) q=false;
}
if(q)
{
zs[++zs[0]]=i;
}
}
b[0][0]=1;
b[0][1]=1;
for(i=1;i<=zs[0];i++)//预处理
{
times(i-1,i,zs[i]);
}
while(t--)
{
scanf("%s\n",s);
a[0]=0;
k=0;
int len=strlen(s);
l=1;
for(i=1;i<=len;i++)
{
k=k+(s[len-i]-48)*l;
l*=10;
if(l==mo)
{
a[++a[0]]=k;
l=1;
k=0;
}
}
if(k)
a[++a[0]]=k;
for(i=1;i<=zs[0];i++)//O(6057)求答案。如果还想快点,可以二分
{
if(bj(a,b[i]))
{
for(j=b[i-1][0];j>=1;j--)
{
if(j!=b[i-1][0])
printf("%011lld",b[i-1][j]);
else
printf("%lld",b[i-1][j]);
}
break;
}
}
printf("\n");
}
}

【Heaven Cow与God Bull】题解的更多相关文章

  1. bzoj3034: Heaven Cow与God Bull

    Description __int64 ago,there's a heaven cow called sjy...A god bull named wzc fell in love with her ...

  2. 「Poetize3」Heaven Cow与God Bull

    描述 Description 给定一个整数n,求一个整数m,满足m<=n,并且m/phi(m)的值最大.注:phi(m)代表m的欧拉函数,即不大于m且与m互质的数的个数. 题解:m/phi(m) ...

  3. ACM-ICPC 2018 沈阳赛区网络预赛 Made In Heaven(K短路)题解

    思路:K短路裸题 代码: #include<queue> #include<cstring> #include<set> #include<map> # ...

  4. tyvj 1934 高精度

    「Poetize3」Heaven Cow与God Bull From wwwwodddd     背景 Background __int64 ago,there's a heaven cow call ...

  5. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  6. POJ 3167 Layout(差分约束)

    题面 Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 ...

  7. 2018 ACM 网络选拔赛 沈阳赛区

    B. Call of Accepted #include <cstdio> #include <cstdlib> #include <cmath> #include ...

  8. BootStrap下拉框搜索功能

    <!DOCTYPE html> <html> <head> <title>jQuery bootstrap-select可搜索多选下拉列表插件-www. ...

  9. POJ 2186 Popular cows(SCC 缩点)

    Every cow's dream is to become the most popular cow in the herd. In a herd of N (1 <= N <= 10, ...

随机推荐

  1. 初识Nginx及其LNMP搭建

    Nginx介绍 nginx www服务软件 俄罗斯人开发 开源 性能很高 web产品 大小780k c语言开发 本身是一款静态www软件,不能解析php jsp .do 最大特点 静态小文件(1m), ...

  2. Linux_NFS/Samba服务器

    目录 目录 网络共享的解决方案 搭建NFS服务 服务器端19216801 客户端19216802 autofs自动挂载nfs共享 搭建Samba服务 服务器端 客户端 网络共享的解决方案 Linux/ ...

  3. JS获取URL中参数值

    一.正则法: function getQueryString(name) { var reg = new RegExp('(^|&)' + name + '=([^&]*)(& ...

  4. 关于Polyaxon的使用

    1.首先upload代码 polyaxon upload 注意,upload只能在新建notebook前进行,且每次只能upload一次,所以在结束每次项目测试(开发)前,都应该在Polyaxon上修 ...

  5. 2019JAVA第一次編程总结

    2019第二周实验报告 Java实验报告 班级 计算机科学与技术二班 学号 20188442 姓名 吴怡君 完成时间 2019/9/7 评分等级 实验一 Java开发环境与简单Java程序 一. 实验 ...

  6. RabbitMQ 安装步骤

    RabbitMQ安装步骤 一.安装erlang 1.下载erlang wget https://packages.erlang-solutions.com/erlang-solutions-1.0-1 ...

  7. Unix时间戳和Java 的 System.currentTimeMillis()的区别

  8. oracle 实现mysql find_set_in函数

    create or replace FUNCTION F_FIND_IN_SET(piv_str1 varchar2, piv_str2 varchar2, p_sep varchar2 := ',' ...

  9. numpy库的认识以及数组的创建

    numpy库 numpy是Python数值计算最重要的基础包.大多数提供科学计算的包都是用NumPy的数组作为构建基础.numpy十分高效,基于NumPy的算法要比纯Python快10到100倍(甚至 ...

  10. qt QUndoGroup的使用

    最近项目中用到撤销,恢复功能.qt的demo中有一个例子,是类似于单文档的.而我的项目中是类似于多文档的项目,即可能要打开多个页面,不同的页面都有撤销恢复功能.这样的话,就要用到QUndoGroup类 ...