【Heaven Cow与God Bull】题解
题目
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】题解的更多相关文章
- 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 ...
- 「Poetize3」Heaven Cow与God Bull
描述 Description 给定一个整数n,求一个整数m,满足m<=n,并且m/phi(m)的值最大.注:phi(m)代表m的欧拉函数,即不大于m且与m互质的数的个数. 题解:m/phi(m) ...
- ACM-ICPC 2018 沈阳赛区网络预赛 Made In Heaven(K短路)题解
思路:K短路裸题 代码: #include<queue> #include<cstring> #include<set> #include<map> # ...
- tyvj 1934 高精度
「Poetize3」Heaven Cow与God Bull From wwwwodddd 背景 Background __int64 ago,there's a heaven cow call ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- POJ 3167 Layout(差分约束)
题面 Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 ...
- 2018 ACM 网络选拔赛 沈阳赛区
B. Call of Accepted #include <cstdio> #include <cstdlib> #include <cmath> #include ...
- BootStrap下拉框搜索功能
<!DOCTYPE html> <html> <head> <title>jQuery bootstrap-select可搜索多选下拉列表插件-www. ...
- 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, ...
随机推荐
- 阶段3 1.Mybatis_12.Mybatis注解开发_7 Mybatis注解开发一对多的查询配置
一对多的配置,一个用户对应多个账户 需要在Accout里面增加根据用户的uid查询的方法 在user里面指定子一对多的查询配置 换行显示 测试 把这里注销掉.测试延迟加载,代码注释掉后,延迟加载就没有 ...
- 中国MOOC_零基础学Java语言_第2周 判断_1时间换算
第2周编程题 查看帮助 返回 第2周编程题 依照学术诚信条款,我保证此作业是本人独立完成的. 温馨提示: 1.本次作业属于Online Judge题目,提交后由系统即时判分. 2.学生可以在作业截 ...
- delphi assigned函数的用法
if not Assigned(Modeless) then Assigned()什么意思! assigned 是用来判断某一指针(pointer)或过程引用是否为nil(空),如果为空则返回假(fa ...
- C# 程序异常停止后,sqlite可能变成0kb……
解决办法就是即时备份数据库文件,启动时判断数据库文件是否为0kb,是则还原之
- unity让碰撞只发生一次
碰撞发生在帧的开始,所以你可以检测到冲突,并在LateUpdate复位: private bool hasCollided = false; void OnCollisionEnter(Collisi ...
- Visdom可视化
Visdom是基于Pytorch的可视化工具 安装方式: pip install visdom 因为Visdom本质上是一个Web服务器,把数据渲染到网页上,因此首先需要运行这个 服务器,如下: py ...
- centos7 VM虚拟机在主机关机重启后,无法ping通
解决办法 1.虚拟机的某些网络相关的服务没有启动,打开电脑的服务启动相关服务 2.打开虚拟机的虚拟网络设置,恢复默认设置即可 3.判定虚拟网卡的网关和centos的网关是否一致,如果不一致,改成一致, ...
- SI 和 MDK 添加Astyle功能
一. 什么是Astyle 1. Astyle是一个用来对C/C++代码进行格式化的插件,可在多个环境中使用.该插件基于 Artistic Style 开发 二. 软件获取地址 1.下载地址:https ...
- Coding 地址
Coding 连接 https://dev.tencent.com/u/leexi
- kernel编译
Linux内核编译与安装 Linux内核介绍 Linux内核是一个用C语言写成的,符合POSIX标准的类Unix操作系统.内核是操作系统中最基本的一部分,提供了众多应用程序访问计算机硬件的机制.Lin ...