Find the maximum(规律,大数)

本文介绍了一个基于欧拉φ函数的数学游戏,通过编程求解在给定范围内使n/φ(n)达到最大值的整数n。使用Java实现并考虑了大数情况。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Find the maximum

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


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.
 

 

Source
题解:先算出来前100个数;找规律;由于数太大,用java;
代码:
import java.math.BigInteger;
import java.util.Scanner;



public class Main {
    static int vis[] = new int[1010];
    static int p[] = new int[1010];
    static BigInteger a[] = new BigInteger[110];
    static void getp()
    {
        for(int i = 0; i < 1010; i++)
            vis[i] = 0;
        vis[1] = 1;
        for(int i = 2; i <= 1000; i++)
        {
            if(vis[i] == 0)
            for(int j = i * i; j <= 1000; j += i)
            {
                vis[j] = 1;
            }
        }
        int tp = 0;
        for(int i = 1; i <= 1000; i++)
        {
            if(vis[i] == 0)
                p[tp++] = i;
        }
    }
    public static void main(String[] args){
        Scanner cin = new Scanner(System.in);
        getp();
        a[0]=BigInteger.valueOf(1);
        for(int i=1;i<=100;i++)
        {
            a[i] = a[i-1].multiply(BigInteger.valueOf(p[i-1]));
        }
        int t = cin.nextInt();
        while(t-- > 0)
        {
            BigInteger x;
            x = cin.nextBigInteger();
//            for(int i = 0; i <= 10; i++){
//                System.out.println(a[i]);
//            }
            if(x.compareTo(BigInteger.valueOf(6)) < 0){
                System.out.println("2");
                continue;
            }
            for(int i=0;i<=100;i++)
            {
                if(a[i].equals(x)){
                    System.out.println(a[i]);
                    break;
                }
                else if(a[i].compareTo(x) > 0)
                {
                    System.out.println(a[i-1]);
                    break;
                }
            }
        }
    }
}

 

An array can be used to store large integers one digit at a time. For example, the integer 1234 could be stored in the array a by setting a[0] to 1, a[1] to 2, a[2] to 3, and a[3] to 4. However, for this exercise you might find it more useful to store the digits backward, that is, place 4 in a[0], 3 in a[1], 2 in a[2], and 1 in a[3]. In this exercise you will write a program that reads in two positive integers that are 20 or fewer digits in length and then outputs the sum of the two numbers. Your program will read the digits as values of type char so that the number 1234 is read as the four characters '1', '2', '3', and '4'. After they are read into the program, the characters are changed to values of type int. The digits will be read into a partially filled array, and you might find it useful to reverse the order of the elements in the array after the array is filled with data from the keyboard. (Whether or not you reverse the order of the elements in the array is up to you. It can be done either way, and each way has its advantages and disadvantages.) Your program will perform the addition by implementing the usual paper-and-pencil addition algorithm. The result of the addition is stored in an array of size 20, and the result is then written to the screen. If the result of the addition is an integer with more than the maximum number of digits (that is, more than 20 digits), then your program should issue a message saying that it has encountered “integer overflow.” You should be able to change the maximum length of the integers by changing only one globally defined constant. Include a loop that allows the user to continue to do more additions until the user says the program should end.我是一名cpp初学者,请翻译并为我解答
03-26
### Problem Statement There are $N$ scaffolds numbered from $1$ to $N$ arranged in a line. The height of scaffold $i(1 \leq i \leq N)$ is $H_i$. Takahashi decides to play a game of moving on the scaffolds. Initially, he freely chooses an integer $i(1 \leq i \leq N)$ and gets on scaffold $i$. When he is on scaffold $i$ at some point, he can choose an integer $j(1 \leq j \leq N)$ satisfying the following condition and move to scaffold $j$: - $ H_j \leq H_i - D$ and $1 \leq |i-j| \leq R$. Find the maximum number of moves he can make when he repeats moving until he can no longer move. ### Constraints - $1 \leq N \leq 5 \times 10^5$ - $1 \leq D,R \leq N$ - $H$ is a permutation of $(1,2,\ldots,N)$. - All input values are integers. ### Input The input is given from Standard Input in the following format: ``` $N$ $D$ $R$ $H_1$ $H_2$ $\ldots$ $H_N$ ``` ### Output Output the answer. ### Sample Input 5 2 1 5 3 1 4 2 ### Sample Output 2 Takahashi initially gets on scaffold $1$ and can move between the scaffolds as follows: - First move: Since $H_2 \leq H_1 -D$ and $|2-1| \leq R$, he can move to scaffold $2$. Move from scaffold $1$ to scaffold $2$. - Second move: Since $H_3 \leq H_2 -D$ and $|3-2| \leq R$, he can move to scaffold $3$. Move from scaffold $2$ to scaffold $3$. - Since the height of scaffold $3$ is $1$, he can no longer move. As shown above, he can move $2$ times. Also, no matter how he chooses the scaffolds to move to, he cannot move $3$ or more times. Therefore, output $2$. ### Sample Input 13 3 2 13 7 10 1 9 5 4 11 12 2 8 6 3 ### Sample Output 3 写出这道题目的c++代码,并保证通过所有样例,运行时间再2s内,运行内存在256mb内
最新发布
06-01
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值