AtCoder Typical Contest 001

Submission #6907971

Source codeソースコード

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
import java.math.BigInteger;

public class Main implements Runnable {
	
	//static int mod = 1000000007;
	
    public static void main(String[] args) {
    	new Thread(null, new Main(), "", 1024 * 1024 * 1024).start();
    }
    
    public void run() {
        PrintWriter out = new PrintWriter(System.out);
        FastScanner sc = new FastScanner();
        
        int n = sc.nextInt();
        
        Complex[] a = new Complex[n];
        Complex[] b = new Complex[n];
        
        for(int i=0;i<n;i++){
        	int ai = sc.nextInt();
        	int bi = sc.nextInt();
        	a[i] = new Complex(ai,0);
        	b[i] = new Complex(bi,0);
        }
        
        Complex[] ans = FastFourierTransform.multiply(a, b);
        
        out.println(0);
        for(int i=0;i<2*n-1;i++){
        	out.println(Math.round(ans[i].real));
        }
        
        out.flush();
    }
    
	static void test(int[] a){
		for(int i=0;i<a.length-1;i++){
			System.out.print(a[i]);
			System.out.print(" ");
		}
		System.out.println(a[a.length-1]);
	}
	static void test(double[] a){
		for(int i=0;i<a.length-1;i++){
			System.out.print(a[i]);
			System.out.print(" ");
		}
		System.out.println(a[a.length-1]);
	}
	
}

class FastFourierTransform {
	
	static Complex[] dft(Complex[] f, int n){
		Complex[] nf = new Complex[n];
		for(int i=0;i<f.length;i++){
			nf[i] = f[i];
		}
		for(int i=f.length;i<n;i++){
			nf[i] = new Complex(0,0);
		}
		
		fft(nf,false);
		return nf;
	}
	
	static Complex[] invdft(Complex[] f, int n){
		Complex[] nf = new Complex[n];
		for(int i=0;i<f.length;i++){
			nf[i] = f[i];
		}
		for(int i=f.length;i<n;i++){
			nf[i] = new Complex(0,0);
		}
		
		fft(nf,true);

		return nf;
	}
	
	static Complex[] multiply(Complex[] g, Complex[] h){
		int n = Integer.highestOneBit(Math.max(g.length, h.length) - 1) << 2;
		
		Complex[] gg = dft(g,n);
		Complex[] hh = dft(h,n);
		
		Complex[] ff = new Complex[n];
		for(int i=0;i<n;i++){
			ff[i] = gg[i].multiply(hh[i]);
		}
		
		return invdft(ff,n);
	}
	
	static long[] multiply(long[] a, long[] b){
        Complex[] x = new Complex[a.length];
        Complex[] y = new Complex[b.length];
        
        for(int i=0;i<a.length;i++){
        	x[i] = new Complex(a[i],0);
        }
        
        for(int i=0;i<b.length;i++){
        	y[i] = new Complex(b[i],0);
        }
        
        Complex[] tmp = multiply(x, y);
        
        long[] ans = new long[a.length + b.length - 1];
        
        for(int i=0;i<a.length + b.length - 1;i++){
        	ans[i] = Math.round(tmp[i].real);
        }
        
        return ans;
	}
	
	static void fft(Complex[] f, boolean inv){
		int n = f.length; //2冪
		
		//添字のバタフライ
		for(int i=1,j=0; i<n; i++){
			int m = n>>1;
			for(; j>=m; m>>=1){
				j -= m;
			}
			j+=m;
			if(i<j){
				Complex tmp = new Complex(f[i].real, f[i].img);
				f[i] = f[j];
				f[j] = tmp;
			}
		}

		for(int len = 2;len<=n; len <<= 1){
			int h = len>>1;
			double theta = 2*Math.PI / len;
			if(inv){
				theta = -theta;
			}
			
			Complex z = new Complex(Math.cos(theta), Math.sin(theta));
			
			for(int i=0;i<n;i+=len){
				Complex w = new Complex(1,0);
				
				for(int j=0;j<h;j++){
					Complex u = new Complex(f[i+j].real,f[i+j].img);
					Complex v = w.multiply(f[i+j+h]);
					f[i+j] = u.plus(v);
					f[i+j+h] = u.minus(v);
					w = w.multiply(z);
				}
			}
		}
		
		if(inv){
			for(int i=0;i<n;i++){
				f[i] = f[i].divide(n);
			}
		}
		
		System.gc();
	}
	
}

class Complex {
	double real;
	double img;
	
	public Complex(double real, double img){
		this.real = real;
		this.img = img;
	}
	
	Complex plus(Complex y){
		return new Complex(this.real+y.real, this.img+y.img);
	}
	
	Complex minus(Complex y){
		return new Complex(this.real - y.real, this.img - y.img);
	}
	
	Complex multiply(Complex y){
		return new Complex(this.real*y.real - this.img*y.img, this.real*y.img + this.img*y.real);
	}
	
	//共役
	Complex conjugate(){
		return new Complex(this.real, -this.img);
	}
	
	Complex divide(double y){
		return new Complex(this.real/y, this.img/y);
	}
}


class FastScanner {
	private final InputStream in = System.in;
	private final byte[] buffer = new byte[1024];
	private int ptr = 0;
	private int buflen = 0;
	private boolean hasNextByte() {
		if (ptr < buflen) {
			return true;
		} else {
			ptr = 0;
			try {
				buflen = in.read(buffer);
			} catch (IOException e) {
				e.printStackTrace();
			}
			if (buflen <= 0) {
				return false;
			}
		}
		return true;
	}
	private int readByte() {
		if (hasNextByte())
			return buffer[ptr++];
		else
			return -1;
	}
	private static boolean isPrintableChar(int c) {
		return 33 <= c && c <= 126;
	}
	public boolean hasNext() {
		while (hasNextByte() && !isPrintableChar(buffer[ptr]))
			ptr++;
		return hasNextByte();
	}
	public String next() {
		if (!hasNext())
			throw new NoSuchElementException();
		StringBuilder sb = new StringBuilder();
		int b = readByte();
		while (isPrintableChar(b)) {
			sb.appendCodePoint(b);
			b = readByte();
		}
		return sb.toString();
	}
	public long nextLong() {
		if (!hasNext())
			throw new NoSuchElementException();
		long n = 0;
		boolean minus = false;
		int b = readByte();
		if (b == '-') {
			minus = true;
			b = readByte();
		}
		if (b < '0' || '9' < b) {
			throw new NumberFormatException();
		}
		while (true) {
			if ('0' <= b && b <= '9') {
				n *= 10;
				n += b - '0';
			} else if (b == -1 || !isPrintableChar(b)) {
				return minus ? -n : n;
			} else {
				throw new NumberFormatException();
			}
			b = readByte();
		}
	}
	public int nextInt() {
		long nl = nextLong();
		if (nl < Integer.MIN_VALUE || nl > Integer.MAX_VALUE)
			throw new NumberFormatException();
		return (int) nl;
	}
	public int[] nextintArray(int n){
		int[] a = new int[n];
		for(int i=0;i<n;i++){
			a[i] = nextInt();
		}
		return a;
	}
	public long[] nextlongArray(int n){
		long[] a = new long[n];
		for(int i=0;i<n;i++){
			a[i] = nextLong();
		}
		return a;
	}
	public Integer[] nextIntegerArray(int n){
		Integer[] a = new Integer[n];
		for(int i=0;i<n;i++){
			a[i] = nextInt();
		}
		return a;
	}
	public int[][] nextintMatrix(int h, int w){
		int[][] mat = new int[h][w];
		for(int i=0;i<h;i++){
			for(int j=0;j<w;j++){
				mat[i][j] = nextInt();
			}
		}
		return mat;
	}
	public double nextDouble() {
		return Double.parseDouble(next());
	}
}

Submission

Task問題 C - 高速フーリエ変換
User nameユーザ名 warachia
Created time投稿日時
Language言語 Java8 (OpenJDK 1.8.0)
Status状態 WA
Score得点 0
Source lengthソースコード長 6787 Byte
File nameファイル名
Exec time実行時間 ms
Memory usageメモリ使用量 -

Test case

Set

Set name Score得点 / Max score Cases
Sample - 00_sample_01
All 0 / 100 00_sample_01,01_00_01,01_01_19,01_02_31,01_03_22,01_04_31,01_05_40,01_06_15,01_07_39,01_08_28,01_09_30,01_10_23,01_11_33,01_12_11,01_13_28,01_14_41,01_15_26,01_16_49,01_17_34,01_18_02,01_19_33,01_20_29,02_00_51254,02_01_82431,02_02_17056,02_03_34866,02_04_6779,02_05_65534,02_06_65535,02_07_65536,02_08_65537,02_09_65538,02_10_100000

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
00_sample_01 AC 88 ms 37204 KB
01_00_01 WA
01_01_19 AC 86 ms 39764 KB
01_02_31 AC 87 ms 35284 KB
01_03_22 AC 85 ms 33364 KB
01_04_31 AC 85 ms 34768 KB
01_05_40 AC 92 ms 37076 KB
01_06_15 AC 85 ms 35924 KB
01_07_39 AC 86 ms 34132 KB
01_08_28 AC 86 ms 35920 KB
01_09_30 AC 88 ms 38996 KB
01_10_23 AC 89 ms 32340 KB
01_11_33 AC 87 ms 35924 KB
01_12_11 AC 86 ms 37588 KB
01_13_28 AC 89 ms 38996 KB
01_14_41 AC 89 ms 39892 KB
01_15_26 AC 87 ms 35540 KB
01_16_49 AC 88 ms 32724 KB
01_17_34 AC 88 ms 35412 KB
01_18_02 AC 85 ms 39892 KB
01_19_33 AC 85 ms 36180 KB
01_20_29 AC 87 ms 34900 KB
02_00_51254 AC 549 ms 141224 KB
02_01_82431 MLE
02_02_17056 AC 366 ms 104196 KB
02_03_34866 AC 547 ms 190960 KB
02_04_6779 AC 204 ms 55940 KB
02_05_65534 AC 563 ms 141388 KB
02_06_65535 AC 560 ms 143372 KB
02_07_65536 AC 566 ms 139940 KB
02_08_65537 MLE
02_09_65538 MLE
02_10_100000 MLE