結果
| 問題 | 
                            No.1307 Rotate and Accumulate
                             | 
                    
| コンテスト | |
| ユーザー | 
                             | 
                    
| 提出日時 | 2021-02-22 09:29:15 | 
| 言語 | C#(csc)  (csc 3.9.0)  | 
                    
| 結果 | 
                             
                                AC
                                 
                             
                            
                         | 
                    
| 実行時間 | 1,277 ms / 5,000 ms | 
| コード長 | 2,384 bytes | 
| コンパイル時間 | 5,018 ms | 
| コンパイル使用メモリ | 114,720 KB | 
| 実行使用メモリ | 101,528 KB | 
| 最終ジャッジ日時 | 2024-09-20 06:55:01 | 
| 合計ジャッジ時間 | 18,276 ms | 
| 
                            ジャッジサーバーID (参考情報)  | 
                        judge3 / judge1 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| sample | AC * 3 | 
| other | AC * 19 | 
コンパイルメッセージ
Microsoft (R) Visual C# Compiler version 3.9.0-6.21124.20 (db94f4cc) Copyright (C) Microsoft Corporation. All rights reserved.
ソースコード
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)));
	}
}