hdu 4002 Find the maximum

Find the maximum

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 1731    Accepted Submission(s): 742

Problem Description

Euler‘s
Totient function, φ (n) [sometimes called the phi function], is used to
determine the number of numbers less than n which are relatively prime
to n . For example, as 1, 2, 4, 5, 7, and 8, are all less than nine and
relatively prime to nine, φ(9)=6.
HG is the master of X Y. One day
HG wants to teachers XY something about Euler‘s Totient function by a
mathematic game. That is HG gives a positive integer N and XY tells his
master the value of 2<=n<=N for which φ(n) is a maximum. Soon HG
finds that this seems a little easy for XY who is a primer of Lupus,
because XY gives the right answer very fast by a small program. So HG
makes some changes. For this time XY will tells him the value of
2<=n<=N for which n/φ(n) is a maximum. This time XY meets some
difficult because he has no enough knowledge to solve this problem. Now
he needs your help.

Input

There are T test cases (1<=T<=50000). For each test case, standard input contains a line with 2 ≤ n ≤ 10^100.

Output

For each test case there should be single line of output answering the question posed above.

Sample Input

2

10

100

Sample Output

6

30

Hint

If the maximum is achieved more than once, we might pick the smallest such n.

容易看出如果一个数x含有越多质因子的话 , phi(x) 就约小 , 那么 x / phi(x) 便更大 。

原题的解就变成了求 2 ~ n 之内尽量大的前面的质数的乘积。

不知道为什么写暴力会RE 。 打表就AC了。。应该写得丑了 。

暴力打表的程序 :

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <vector>
#include <queue>

using namespace std ;
typedef long long LL ;
typedef pair<int,int> pii;
#define X first
#define Y second
const int N = 50100 ;
struct node {
    string s ;
    int id ;
} e[N];
int prime[1000010] ,tot ;
bool check[1000010];

string Mul( string s , int a ) {
    int c = 0 ;
    for( int i = 0 ; i < s.length() ; ++i ) {
        c = ( s[i] - ‘0‘ ) * a + c ;
        s[i] = (char)( c % 10 ) + ‘0‘ ;
        c /= 10 ;
    }
    while( c > 0 ) {
        s += (char)( c % 10 + ‘0‘ ) ;
        c /= 10 ;
    }
    return s;
}
void Output( string s ) {
    for( int j = s.length() - 1 ; j >=0 ; --j )
        cout << s[j] ;
}

inline bool Compare( const string a , const string b ) {
    if( a.length() < b.length() ) return true ;
    else if( a.length() > b.length() ) return false ;
    else {
        for( int i = a.length() - 1 ; i >= 0 ; --i ){
            if( a[i] > b[i] ) return false ;
            else if( b[i] > a[i] )return true ;
        }
        return true ;
    }
}

int main ()  {
    #ifdef LOCAL
//        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif // LOCAL
    tot = 0 ;
    for( int i = 2 ; i < 1000000 ; ++i ) if( !check[i] ){
        prime[tot++] = i ;
        for( int j = i + i ; j < 1000000 ; j += i )
            check[j] = true ;
    }
    string s = "1" , str = "" , ans = "1" ; int cnt = 0 ;
    for( int i = 1 ; i < 100 ; ++i ) str += "0" ; str += "1";
    while( Compare( s , str ) ) {
        s = Mul( s , prime[cnt++] );
        cout <<‘"‘; Output(s); cout <<"\"\,"<<endl;
    }
}

表 (AC程序):

#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <vector>
#include <map>
#include <vector>
#include <queue>

using namespace std ;

char s[][550] = {
"2",
"6",
"30",
"210",
"2310",
"30030",
"510510",
"9699690",
"223092870",
"6469693230",
"200560490130",
"7420738134810",
"304250263527210",
"13082761331670030",
"614889782588491410",
"32589158477190044730",
"1922760350154212639070",
"117288381359406970983270",
"7858321551080267055879090",
"557940830126698960967415390",
"40729680599249024150621323470",
"3217644767340672907899084554130",
"267064515689275851355624017992790",
"23768741896345550770650537601358310",
"2305567963945518424753102147331756070",
"232862364358497360900063316880507363070",
"23984823528925228172706521638692258396210",
"2566376117594999414479597815340071648394470",
"279734996817854936178276161872067809674997230",
"31610054640417607788145206291543662493274686990",
"4014476939333036189094441199026045136645885247730",
"525896479052627740771371797072411912900610967452630",
"72047817630210000485677936198920432067383702541010310",
"10014646650599190067509233131649940057366334653200433090",
"1492182350939279320058875736615841068547583863326864530410",
"225319534991831177328890236228992001350685163362356544091910",
"35375166993717494840635767087951744212057570647889977422429870",
"5766152219975951659023630035336134306565384015606066319856068810",
"962947420735983927056946215901134429196419130606213075415963491270",
"166589903787325219380851695350896256250980509594874862046961683989710",
"29819592777931214269172453467810429868925511217482600306406141434158090",
"5397346292805549782720214077673687806275517530364350655459511599582614290",
"1030893141925860008499560888835674370998623848299590975192766715520279329390",
"198962376391690981640415251545285153602734402721821058212203976095413910572270",
"39195588149163123383161804554421175259738677336198748467804183290796540382737190",
"7799922041683461553249199106329813876687996789903550945093032474868511536164700810",
"1645783550795210387735581011435590727981167322669649249414629852197255934130751870910",
"367009731827331916465034565550136732339800312955331782619462457039988073311157667212930",
"83311209124804345037562846379881038241134671040860314654617977748077292641632790457335110",
"19078266889580195013601891820992757757219839668357012055907516904309700014933909014729740190",
"4445236185272185438169240794291312557432222642727183809026451438704160103479600800432029464270",
"1062411448280052319722448549835623701226301211611796930357321893850294264731624591303255041960530",
"256041159035492609053110100510385311995538591998443060216114576417920917800321526504084465112487730",
"64266330917908644872330635228106713310880186591609208114244758680898150367880703152525200743234420230"
};
int main ()  {
    int _ ; scanf("%d",&_) ;
    char s1[550]; int len[54] ;
    for( int i = 0 ; i < 54 ; ++i )
        len[i] = strlen(s[i]);

    while( _-- ) {
        scanf("%s",s1); int id = 0 ;
        int slen = strlen(s1);
        for( int i = 0 ; i < 54 ; ++i ) {
            if( len[i] < slen ) id = i ;
            else if( len[i] > slen ) break ;
            else {
                if( strcmp( s1 , s[i]) >= 0 ) id = i ;
                else break ;
            }
        }
        printf("%s\n",s[id]);
    }
}

时间: 2024-08-23 12:04:46

hdu 4002 Find the maximum的相关文章

HDU 4002 Find the maximum(数论-欧拉函数)

Find the maximum Problem Description Euler's Totient function, φ (n) [sometimes called the phi function], is used to determine the number of numbers less than n which are relatively prime to n . For example, as 1, 2, 4, 5, 7, and 8, are all less than

HDU 4002 Find the maximum (欧拉函数-积性函数的性质(2011年大连赛区网络赛第二题)

[题目链接]:click here~~ [题目大意]: 给出一个整数n,求一个数x,x在1到n之间,并且x/φ(x)最大(其中φ(x)为x的欧拉函数). [思路]: 由欧拉函数为积性函数,即:如果 则有: 且: 则有: 要使f(x)最大,须使x含尽量多的不同素数因子. 代码: /* * Problem: HDU No.4002 * Running time: 1700MS * Complier: java * Author: javaherongwei * Create Time: 0:08 2

HDU 1839 Delay Constrained Maximum Capacity Path(二分+最短路)

题目地址:HDU 1839 我去..原来这题这么简单...网络流中这种二分建图的方式做了一大堆了..这种题还能难倒我吗...白天一直没怎么看懂题,对题意懵懵懂懂的...晚上好好看了看题,这不就是网络流中练的最多的那种二分建图模型吗....只是把网络流算法改成最短路就行了..但是两个地方手残了没能在实验室当场A掉..sad... 这题就是二分最小容量,对满足容量的加边,对时间求最短路.如果最短时间比规定时间少的话就可以继续增加容量,直到不能增加为止. 代码如下: #include <iostrea

hdu 5052 Yaoge’s maximum profit

Yaoge’s maximum profit Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 442    Accepted Submission(s): 122 Problem Description Yaoge likes to eat chicken chops late at night. Yaoge has eaten too

hdu 1839 Delay Constrained Maximum Capacity Path(spfa+二分)

Delay Constrained Maximum Capacity Path Time Limit: 10000/10000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 1790    Accepted Submission(s): 577 Problem Description Consider an undirected graph with N vertices, nu

hdu 4002 欧拉函数

题意:求1-n内最大的x/phi(x) 通式:φ(x)=x*(1-1/p1)*(1-1/p2)*(1-1/p3)*(1-1/p4)…..(1-1/pn),其中p1, p2……pn为x的所有质因数,x是不为0的整数.φ(1)=1(唯一和1互质的数就是1本身). 因此含质因数最多的即为所求,打表求出前n个积,之后找到比自己小的最大积 大数打表 1 #include<cstdio> 2 #include<iostream> 3 #include<algorithm> 4 #i

Hdu 5052 Yaoge’s maximum profit(树链剖分)

题目大意: 给出一棵树,每个点有商店,每个商店都有一个价格,Yaoge每次从x走到y都可以在一个倒卖商品,从中得取利益,当然,买一顶要在卖之前.但是没次走过一条路,这条路上的所有商品都会增加一个v. 输出每次的最大利益. 思路分析: 很容易想到树链剖分,可是关键在于如何维护这样一个变量,使得每次都要让买的再卖的前面. 维护变量 ltr 和 rtl ,表示从左去右和从右去左. 剖分熟练的时候,判断x 和 y的深度,一步一步往上爬. 然后维护区间最大值和最小值,爬的时候更新答案. ...4921ms

hdu 1839 Delay Constrained Maximum Capacity Path

最短路+二分. 对容量进行二分,因为容量和时间是单调关系的,容量越多,能用的边越少,时间会不变或者增加. 因为直接暴力一个一个容量去算会TLE,所以采用二分. #include<cstdio> #include<vector> #include<cstring> #include<queue> #include<map> #include<algorithm> using namespace std; const int maxn =

HDU 5052 Yaoge’s maximum profit 裸树链剖分 2014 ACM/ICPC Asia Regional Shanghai Online

题意: 给定n个点的带点权树. 下面n行给出每个点点权表示每个点买卖鸡腿的价格 下面n-1行给出树边 下面Q个操作 Q行 u, v, val 从u走到v,过程中可以买一个鸡腿,然后到后面卖掉,输出max(0, 最大的收益) 然后给[u,v]路径上点点权+=val 思路: 树链剖分裸题 屌丝题解:点击打开链接 #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include &