結果

問題 No.1307 Rotate and Accumulate
ユーザー さかぽんさかぽん
提出日時 2021-02-22 09:29:15
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 1,384 ms / 5,000 ms
コード長 2,384 bytes
コンパイル時間 4,899 ms
コンパイル使用メモリ 112,904 KB
実行使用メモリ 107,372 KB
最終ジャッジ日時 2023-10-20 11:21:51
合計ジャッジ時間 16,449 ms
ジャッジサーバーID
(参考情報)
judge15 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 36 ms
26,168 KB
testcase_01 AC 36 ms
26,168 KB
testcase_02 AC 37 ms
26,168 KB
testcase_03 AC 35 ms
26,140 KB
testcase_04 AC 37 ms
26,152 KB
testcase_05 AC 35 ms
26,144 KB
testcase_06 AC 35 ms
26,140 KB
testcase_07 AC 36 ms
26,164 KB
testcase_08 AC 632 ms
77,952 KB
testcase_09 AC 642 ms
77,492 KB
testcase_10 AC 654 ms
75,984 KB
testcase_11 AC 619 ms
76,764 KB
testcase_12 AC 650 ms
76,332 KB
testcase_13 AC 125 ms
46,492 KB
testcase_14 AC 324 ms
60,516 KB
testcase_15 AC 1,348 ms
107,372 KB
testcase_16 AC 1,361 ms
107,372 KB
testcase_17 AC 1,357 ms
107,308 KB
testcase_18 AC 1,325 ms
107,256 KB
testcase_19 AC 1,384 ms
107,204 KB
testcase_20 AC 1,349 ms
107,268 KB
testcase_21 AC 36 ms
26,092 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc)
Copyright (C) Microsoft Corporation. All rights reserved.

ソースコード

diff #

using System;
using System.Linq;
using System.Numerics;

class D
{
	static int[] Read() => Array.ConvertAll(Console.ReadLine().Split(), int.Parse);
	static (int, int) Read2() { var a = Read(); return (a[0], a[1]); }
	static long[] ReadL() => Array.ConvertAll(Console.ReadLine().Split(), long.Parse);
	static void Main()
	{
		var (n, q) = Read2();
		var a = ReadL();
		var r = Read();

		a = a.Concat(a).ToArray();
		var b = Tally(r, n - 1);
		Array.Reverse(b);
		var c = Dft.Convolution(a, b);

		Console.WriteLine(string.Join(" ", c[(n - 1)..(2 * n - 1)]));
	}

	static long[] Tally(int[] a, int max)
	{
		var c = new long[max + 1];
		foreach (var x in a) ++c[x];
		return c;
	}
}

public class Dft
{
	public static long[] ToLong(Complex[] a) => Array.ConvertAll(a, c => (long)Math.Round(c.Real));
	public static Complex[] ToComplex(long[] a) => Array.ConvertAll(a, c => new Complex(c, 0));

	public static int ToPowerOf2(int length)
	{
		var n = 1;
		while (n < length) n <<= 1;
		return n;
	}

	static Complex[] NthRoots(int n)
	{
		var r = new Complex[n + 1];
		for (int i = 0; i <= n; ++i) r[i] = Complex.Exp(new Complex(0, i * 2 * Math.PI / n));
		return r;
	}

	int n;
	Complex[] roots;
	public Dft(int length)
	{
		n = ToPowerOf2(length);
		roots = NthRoots(n);
	}

	void FftInternal(Complex[] c, bool inverse)
	{
		var m = c.Length;
		if (m == 1) return;

		var m2 = m / 2;
		var nm = n / m;
		var c1 = new Complex[m2];
		var c2 = new Complex[m2];
		for (int i = 0; i < m2; ++i)
		{
			c1[i] = c[2 * i];
			c2[i] = c[2 * i + 1];
		}

		FftInternal(c1, inverse);
		FftInternal(c2, inverse);

		for (int i = 0; i < m2; ++i)
		{
			var z = c2[i] * roots[nm * (inverse ? m - i : i)];
			c[i] = c1[i] + z;
			c[m2 + i] = c1[i] - z;
		}
	}

	// { f(w^i) }
	// 長さは n 以下で OK。
	public Complex[] Fft(Complex[] c, bool inverse = false)
	{
		var r = new Complex[n];
		c.CopyTo(r, 0);
		FftInternal(r, inverse);
		if (inverse) for (int i = 0; i < n; ++i) r[i] /= n;
		return r;
	}

	// 長さは n 以下で OK。
	public static Complex[] Convolution(Complex[] a, Complex[] b)
	{
		var dft = new Dft(a.Length + b.Length - 1);
		var fa = dft.Fft(a);
		var fb = dft.Fft(b);
		for (int i = 0; i < dft.n; ++i) fa[i] *= fb[i];
		return dft.Fft(fa, true);
	}

	public static long[] Convolution(long[] a, long[] b)
	{
		return ToLong(Convolution(ToComplex(a), ToComplex(b)));
	}
}
0