結果

問題 No.206 数の積集合を求めるクエリ
ユーザー eitahoeitaho
提出日時 2016-11-23 03:35:15
言語 C#(csc)
(csc 3.9.0)
結果
AC  
実行時間 343 ms / 7,000 ms
コード長 4,431 bytes
コンパイル時間 1,227 ms
コンパイル使用メモリ 110,336 KB
実行使用メモリ 42,368 KB
最終ジャッジ日時 2024-05-05 12:25:27
合計ジャッジ時間 7,119 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 25 ms
19,200 KB
testcase_01 AC 24 ms
19,200 KB
testcase_02 AC 25 ms
19,328 KB
testcase_03 AC 23 ms
18,816 KB
testcase_04 AC 25 ms
19,072 KB
testcase_05 AC 26 ms
18,944 KB
testcase_06 AC 35 ms
19,840 KB
testcase_07 AC 35 ms
19,840 KB
testcase_08 AC 35 ms
19,968 KB
testcase_09 AC 37 ms
19,712 KB
testcase_10 AC 28 ms
18,944 KB
testcase_11 AC 26 ms
19,200 KB
testcase_12 AC 35 ms
20,224 KB
testcase_13 AC 34 ms
19,968 KB
testcase_14 AC 35 ms
19,968 KB
testcase_15 AC 34 ms
20,224 KB
testcase_16 AC 34 ms
19,968 KB
testcase_17 AC 314 ms
40,064 KB
testcase_18 AC 333 ms
30,464 KB
testcase_19 AC 339 ms
39,296 KB
testcase_20 AC 266 ms
31,232 KB
testcase_21 AC 306 ms
32,896 KB
testcase_22 AC 277 ms
32,896 KB
testcase_23 AC 341 ms
39,424 KB
testcase_24 AC 317 ms
42,368 KB
testcase_25 AC 337 ms
40,832 KB
testcase_26 AC 343 ms
37,504 KB
testcase_27 AC 320 ms
35,712 KB
testcase_28 AC 342 ms
39,808 KB
testcase_29 AC 341 ms
40,064 KB
testcase_30 AC 314 ms
36,608 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.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Collections.Generic;
using System.Diagnostics;
using System.Numerics;
using Enu = System.Linq.Enumerable;

public class Program
{
    public void Solve()
    {
        int L = Reader.Int(), M = Reader.Int(), N = Reader.Int();
        var A = Reader.IntArray(L);
        var B = Reader.IntArray(M);
        int Q = Reader.Int();
        var X = new int[N + 1];
        var Y = new int[N + 1];
        foreach (int a in A) X[a] = 1;
        foreach (int b in B) Y[N - b] = 1;
        var res = new FFTMod().Mult(X, Y);
        Console.WriteLine(string.Join("\n", res.Skip(N).Take(Q)));
    }
}

class FFTMod
{
    int Mod, PrimitiveRoot;

    public long[] Mult(int[] A, int[] B) { return Mult(A, B, 167772161, 3); }

    public long[] Mult(int[] A, int[] B, int mod, int primitiveRoot)
    {
        Mod = mod;
        PrimitiveRoot = primitiveRoot;
        int N = A.Length + B.Length - 1;
        int N2 = N;
        while ((N2 & N2 - 1) != 0) N2 += N2 & -N2;
        long[] a = new long[N2], b = new long[N2];
        for (int i = 0; i < A.Length; i++) a[i] = A[i];
        for (int i = 0; i < B.Length; i++) b[i] = B[i];
        Transform(a);
        Transform(b);
        for (int i = 0; i < N2; i++) a[i] = a[i] * b[i] % Mod;
        Transform(a, true);
        long inv = Power(N2, Mod - 2);
        for (int i = 0; i < N2; i++) a[i] = a[i] * inv % mod;
        return a;
    }

    void Transform(long[] A, bool inv = false)
    {
        int N = A.Length;
        long H = Power(PrimitiveRoot, (Mod - 1) / N);
        if (inv) H = Power(H, Mod - 2);
        for (int a = 0, b = 0; b < N; ++b)
        {
            if (b < a) { var t = A[a]; A[a] = A[b]; A[b] = t; }
            for (int k = N >> 1; k > (a ^= k); k >>= 1) { }
        }
        for (int m = 1; m < N; m <<= 1)
        {
            int m2 = m << 1;
            long b = Power(H, N / m2);
            long w = 1;
            for (int a = 0; a < m; ++a, w = w * b % Mod)
                for (int k = a; k < N; k += m2)
                {
                    long x = A[k];
                    long y = A[k + m] * w % Mod;
                    long tx = x + y;
                    long ty = x - y;
                    A[k] = tx >= Mod ? tx - Mod : tx;
                    A[k + m] = ty < 0 ? ty + Mod : ty;
                }
        }
    }

    long Power(long x, long n)
    {
        long res = 1;
        if ((x %= Mod) < 0) x += Mod;
        for (; n > 0; n >>= 1, x = x * x % Mod)
            if ((n & 1) == 1) res = res * x % Mod;
        return res;
    }
}


class Entry { static void Main() { new Program().Solve(); } }
class Reader
{
    static TextReader reader = Console.In;
    static readonly char[] separator = { ' ' };
    static readonly StringSplitOptions op = StringSplitOptions.RemoveEmptyEntries;
    static string[] A = new string[0];
    static int i;
    static void Init() { A = new string[0]; }
    public static void Set(TextReader r) { reader = r; Init(); }
    public static void Set(string file) { reader = new StreamReader(file); Init(); }
    public static bool HasNext() { return CheckNext(); }
    public static string String() { return Next(); }
    public static int Int() { return int.Parse(Next()); }
    public static long Long() { return long.Parse(Next()); }
    public static double Double() { return double.Parse(Next()); }
    public static int[] IntLine() { return Array.ConvertAll(Split(Line()), int.Parse); }
    public static int[] IntArray(int N) { return Range(N, Int); }
    public static int[][] IntTable(int H) { return Range(H, IntLine); }
    public static string[] StringArray(int N) { return Range(N, Next); }
    public static string[][] StringTable(int N) { return Range(N, () => Split(Line())); }
    public static string Line() { return reader.ReadLine().Trim(); }
    public static T[] Range<T>(int N, Func<T> f) { var r = new T[N]; for (int i = 0; i < N; r[i++] = f()) ; return r; }
    static string[] Split(string s) { return s.Split(separator, op); }
    static string Next() { CheckNext(); return A[i++]; }
    static bool CheckNext()
    {
        if (i < A.Length) return true;
        string line = reader.ReadLine();
        if (line == null) return false;
        if (line == "") return CheckNext();
        A = Split(line);
        i = 0;
        return true;
    }
}
0