题目

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. 《Using Python to Access Web Data》 Week5 Web Services and XML 课堂笔记

    Coursera课程<Using Python to Access Web Data> 密歇根大学 Week5 Web Services and XML 13.1 Data on the ...

  2. oracle rman catalog--ORA-01580: error creating control backup file

    在测试rman catalog时,错误的设置了snapshot路径,报错 RMAN> show snapshot controlfile name; RMAN configuration par ...

  3. 【ABAP系列】SAP S/4 HANA的SMARTFORMS如何切换到非word编辑器

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[ABAP系列]SAP S/4 HANA的SMA ...

  4. 【MM系列】SAP 的库存管理

    公众号:SAP Technical 本文作者:matinal 原文出处:http://www.cnblogs.com/SAPmatinal/ 原文链接:[MM系列]SAP 的库存管理   前言部分 大 ...

  5. 求方程x1+x2+x3=15的整数解的数目

    求方程x1+x2+x3=15的整数解的数目要求0≤x1≤5,0≤x2≤6,0≤x3≤7.解:令N为全体非负整数解(x1,x2,x3),A1为其中x1≥6的解:y1=x1-6≥0的解:A2为其中x2≥7 ...

  6. 【Linux开发】【Qt开发】ARM QT移植详细步骤教程

    ARM QT移植详细步骤教程 米尔SAM9X5和A5D3X上默认的Qt版本是4.5.3,当这个版本的Qt库不能满足实际开发需求时,可通过此方法制定Qt开发.运行环境. 移植的步骤如下: 1.下载新版q ...

  7. Go语言入门篇-基本数据类型

    一.程序实体与关键字 任何Go语言源码文件都由若干个程序实体组成的.在Go语言中,变量.常量.函数.结构体和接口被统称为“程序实体”,而它们的名字被统称为“标识符”. 标识符可以是任何Unicode编 ...

  8. 使用app.config中的数据对数据库链接信息初始化

    看到别人数据库信息都是在app.config里面设置的,今天来尝试了一下,报了 "System.Configuration.ConfigurationSettings.AppSettings ...

  9. (已实践)PLSQL本地还原Oracle数据库dmp文件

    这个方法很烂,导致重装Oracle时候处处出现问题,不建议使用这个方法,除非你以后不再用Oracle这个软件了,这个方法很烂,再评论一下. 第一,启动服务,(如果数据库处于启动状态,那么略过这一步) ...

  10. 极*Java速成教程 - (4)

    Java语言基础 多态 多态是面向对象的一大重要特性,如果说封装是隐藏一个类怎么做,继承是确定一系列的类做什么,那多态就是通过手段去分离做什么和怎么做. 向上转型与收窄 在开发者将一类事物封装成类以后 ...