Find the maximum(规律,大数)

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

The 36th ACM/ICPC Asia Regional Dalian Site —— Online Contest

题解:先算出来前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;
                }
            }
        }
    }
}
时间: 2024-07-31 02:57:56

Find the maximum(规律,大数)的相关文章

BZOJ1002(找规律+大数)

1002: [FJOI2007]轮状病毒 Time Limit: 1 Sec  Memory Limit: 162 MB Submit: 2427  Solved: 1333 [Submit][Status] Description 给定n(N<=100),编程计算有多少个不同的n轮状病毒. Input 第一行有1个正整数n. Output 将编程计算出的不同的n轮状病毒数输出 Sample Input 3 Sample Output 16 题意:RT 思路:dp[n]表示为n的方案数 打表可知

hdu 5351 规律+大数

题目大意:定义了一种fib字符串,问第n个fib串的前m个字母前后相等串的最大长度,大约就是这样的 其实主要读完题意的时候并没有思路,但是列几个fib字符串就会发现,除了fib1以外,所有串的前面都是一样的,后面的串就只是在前面串的基础上再贴一个串而已,因此很明显,这里的n其实读下来并没有什么用,基本只是用来告诉我们总串长是第1000个fib数列的数,因此要大数,我就试着写了java大数.然后这个结果就直接列一些,打表就会发现规律了```然后就没有然后了.恩,别忘了mod 1 import ja

HDOJ-1041 Computer Transformation(找规律+大数运算)

http://acm.hdu.edu.cn/showproblem.php?pid=1041 有一个初始只有一个1的串 每次都按①0 -> 10;②1 -> 01;这两条规则进行替换 形如:n = 1  1 n = 2  01 n = 3  1001 ... 求经过n步替换之后 串中只含复数个0的连续子串(不难发现,这种子串只能是‘00’)的出现次数 因为0<n<=1000的限制 在最坏情况下(n==1000)串的长度将达到2^1000位 排除了直接模拟上述替换过程的可能 列出前几

Doom 规律+大数

Doom 比赛的时候没有做出来,补题. 题意:题目定义了一个斐波那契串 1) fib1=b; 2) fib2=a; 3) fibi=fibi-1fibi-2,i>2 举例,fib3=ab,fib4=aba,fib5=abaab 我们暂时将字符串sisi+1si+2si+3…sj记做s[i:j] 求满足s[1:i]=s[m-i+1:m](i<m)的i的最大值,记做LBorderm 例如m=5时,LBorderm=2,因为abaab中前两个和末尾两个相同,即黑色部分 解题思路: 一看到题目的数据这

Resistors in Parallel(找规律+大数)

题意:https://codeforces.com/group/ikIh7rsWAl/contest/254825/problem/E 给你一个n,计算n / Sigma(1~n)的d(是n的只出现一次的因数). 思路: 反正就是打表找规律,3组数据也能找规律,你们是真的nb嗷. 1 import java.math.BigInteger; 2 import java.util.Scanner; 3 4 public class Main{ 5 public static void main(S

简单递推专题

Computer Transformation Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 6084    Accepted Submission(s): 2222 Problem Description A sequence consisting of one digit, the number 1 is initially wri

UVALive 6270 Edge Case(找规律,大数相加)

转载请注明出处:http://blog.csdn.net/u012860063?viewmode=contents 找规律,前两个数的和等于后一个数的值: 其实就是大菲波数: 代码如下: #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> using namespace std; #include<cstdio> #include<cst

Acdream 1210 Chinese Girls&#39; Amusement(大数模板运算 + 找规律)

传送门 Chinese Girls' Amusement Time Limit: 2000/1000MS (Java/Others) Memory Limit: 128000/64000KB (Java/Others) Submit Statistic Next Problem Problem Description You must have heard that the Chinese culture is quite different from that of Europe or Rus

大数+找规律 ACdream1210 Chinese Girls&#39; Amusement

传送门:点击打开链接 题意:对于n个点围成的圈.从一个点出发,顺时针数K个位置,一直进行这个操作直到回到最初的那个点时,恰好把所有的点都访问了一遍,问最大的K(K<=n/2) 思路:很容易就想到了一种方法,找到K<=n/2,且gcd(K,n)=1,有人是用java从n/2向1去枚举的,感觉好暴力,所以当时不敢这样写 后来发现其实是有规律的,从n=3一直算下去,会得到一个这样的序列1 1 2 1 3 3 4 3 5 5 6 5 7 7 8 7 9 9 10 9..... 很明显以4个为一组,一下