Violent problem: equivariance sequence

 

AC code 1: (Violently enumerate the first item, tolerance, and then use the formula of tolerance to judge whether 10 prime numbers meet the continuation of tolerance d, and update the minimum tolerance)

import java.io.*;
import java.math.BigInteger;
import java.util.*;
import java.util.stream.Collectors;

public class Main
{
    static PrintWriter pw = new PrintWriter(new BufferedWriter(new OutputStreamWriter(System.out)));
    static int N = (int)1e3 + 10;
    static int INF = 0x3f3f3f3f;
    static math math_me = new math();
    static Set<Integer> set = new HashSet<>();

    public static void main(String[] args ) throws IOException
    {
        int min_d = INF;
        int k;
        for(int i = 2 ; i <= N ; i ++)  // 枚举每一个首项
        {
            for(int j = 2 ; j <= N ; j ++) // 枚举每一个公差
            {
                for(k = 0 ; k <= 9 ; k ++) // an = i + k*j
                {
                    if(!math_me.check_isPrime(i + k * j))  break; // 在检测到公差为j的10个数是否都是宿舍的过程中,发现有不是宿舍的,跳出循环
                }
                if(k == 10)  // 可构成连续个10个素数
                {
                    if(j < min_d)  min_d = j; //更新公差
                }
            }
        }
        pw.println(min_d);
        pw.flush();
    }
}

class rd
{
    static BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
    static StringTokenizer tokenizer = new StringTokenizer("");

    static String nextLine() throws IOException { return reader.readLine(); }
    static String next() throws IOException
    {
        while(!tokenizer.hasMoreTokens())  tokenizer = new StringTokenizer(reader.readLine());
        return tokenizer.nextToken();
    }
    static int nextInt() throws IOException { return Integer.parseInt(next()); }
    static double nextDouble() throws IOException { return Double.parseDouble(next()); }
    static long nextLong() throws IOException { return Long.parseLong(next()); }
    static BigInteger nextBigInteger() throws IOException
    {
        BigInteger d = new BigInteger(rd.nextLine());
        return d;
    }
}

class math
{
    int gcd(int a,int b)
    {
        if(b == 0)  return a;
        else return gcd(b,a % b);
    }

    int lcm(int a,int b)
    {
        return a * b / gcd(a, b);
    }

    // 求n的所有约数
    List get_factor(int n)
    {
        List<Long> a = new ArrayList<>();
        for(long i = 1; i <= Math.sqrt(n) ; i ++)
        {
            if(n % i == 0)
            {
                a.add(i);
                if(i != n / i)  a.add(n / i);  // // 避免一下的情况:x = 16时,i = 4 ,x / i = 4的情况,这样会加入两种情况  ^-^复杂度能减少多少是多少
            }
        }

        // 相同因子去重,这个方法,完美
        a = a.stream().distinct().collect(Collectors.toList());

        // 对因子排序(升序)
        Collections.sort(a);

        return a;
    }

    // 判断是否是质数
    boolean check_isPrime(int n)
    {
        if(n < 2) return false;
        for(int i = 2 ; i <= n / i; i ++)  if (n % i == 0) return false;
        return true;
    }
}

class PII implements Comparable<PII>
{
    int x,y;
    public PII(int x ,int y)
    {
        this.x = x;
        this.y = y;
    }
    public int compareTo(PII a)
    {
        if(this.x-a.x != 0)
            return this.x-a.x;  //按x升序排序
        else return this.y-a.y;  //如果x相同,按y升序排序
    }
}

class Edge
{
    int a,b,c;
    public Edge(int a ,int b, int c)
    {
        this.a = a;
        this.b = b;
        this.c = c;
    }
}

class Line implements Comparable<Line>
{
    double k; // 斜率
    double b; // 截距

    public Line(double k, double b)
    {
        this.k = k;
        this.b = b;
    }

    @Override
    public int compareTo(Line o)
    {
        if (this.k > o.k) return 1;
        if (this.k == o.k)
        {
            if (this.b > o.b) return 1;
            return -1;
        }
        return -1;
    }
}

class mqm
{
    int fa[] = new int[1005];
    void init()
    {
        for(int i = 1 ; i <= 1000 ; i ++)  fa[i] = i;
    }
    void merge(int x, int y) { fa[find(x)] = find(y); }
    int find(int x)
    {
        if(x != fa[x])  fa[x] = find(fa[x]);
        return fa[x];
    }
    boolean query(int x, int y) { return find(x) == find(y); }
}

Guess you like

Origin blog.csdn.net/RGHLY21/article/details/129782717