Codeforces #481 D. Perfect Groups (number theory, math,dp) (medium)

题目链接

D. Perfect Groups

分析

首先考虑完全平方数 s 的特征, 那么有 s 的任意一个质因子的次数一定是偶数,也就是说对于 a i = p 1 r 1 p k r k 我们可以去掉偶数次幂

e.g.: 20 = 2 2 5 ,20可变为 5,完全等价

那么对于只有奇数次幂的数来说,他们要在同一个组,if and only if 他们的值相等。因此变量所有的区间统计不同数字的次数就可以了。

java code

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.StringTokenizer;




public class Main {
    public static final int INF = 0x3f3f3f3f;
    public static final long INF64 = Long.MAX_VALUE/2;
    public static InputReader in = new InputReader(System.in);
    public static PrintWriter out = new PrintWriter(new OutputStreamWriter(System.out));
    public static int[] a;
    public static int n;
    public static int[] ans;
    public static void main(String[] args) {
        n = in.nextInt();
        a = new int[n+5];
        ans = new int[n+5];
        for(int i=1 ; i<=n ; ++i)
            a[i] = in.nextInt();
        for(int i=1 ; i<=n ; ++i)
            a[i] = delSqure(a[i]);
        solve();
        for(int i=1 ; i<=n ; ++i)
            out.print(ans[i]+" ");
        out.flush();
    }
    public static int delSqure(int val){
        int fuhao = 1;
        if(val <0){
            fuhao =-1;
            val=-val;
        }
        for(int i=2,sqr = i*i ; sqr<= val ; i++){
            sqr = i*i;
            while (val % sqr ==0)val /=sqr;
        }
        return fuhao*val;
    }
    public static void solve() {
        Arrays.fill(ans,0);

        Set<Integer> set = new HashSet<>();
        for(int l = 1 ; l<=n ; ++l){
            set.clear();
            for(int r = l ; r <=n ; ++r){
                set.add(a[r]);
                if(set.size()>1 && set.contains(0))++ans[set.size()-1];
                else ++ans[set.size()];
            }
        }
    }
}

class InputReader{
    private final static int BUF_SZ = 65536;
    BufferedReader in;
    StringTokenizer tokenizer;
    public InputReader(InputStream in) {
        super();
        this.in = new BufferedReader(new InputStreamReader(in),BUF_SZ);
        tokenizer = new StringTokenizer("");
    }
    private String next() {
        while (!tokenizer.hasMoreTokens()) {
            try {
                tokenizer = new StringTokenizer(in.readLine());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return tokenizer.nextToken();
    }
    public int nextInt() {
        return Integer.parseInt(next());
    }
    public long nextLong() {
        return Long.parseLong(next());
    }
    public double nextDouble() {
        return Double.parseDouble(next());
    }
    public BigInteger nextBigInteger() {
        return new BigInteger(next());
    }
}

猜你喜欢

转载自blog.csdn.net/dylan_frank/article/details/80328443