結果

問題 No.2581 [Cherry Anniversary 3] 28輪の桜のブーケ
ユーザー laneguelanegue
提出日時 2023-12-15 08:34:54
言語 D
(dmd 2.106.1)
結果
AC  
実行時間 69 ms / 3,000 ms
コード長 1,709 bytes
コンパイル時間 2,834 ms
コンパイル使用メモリ 102,656 KB
実行使用メモリ 6,548 KB
最終ジャッジ日時 2023-12-15 08:34:59
合計ジャッジ時間 3,456 ms
ジャッジサーバーID
(参考情報)
judge11 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 7 ms
6,548 KB
testcase_01 AC 10 ms
6,548 KB
testcase_02 AC 12 ms
6,548 KB
testcase_03 AC 27 ms
6,548 KB
testcase_04 AC 19 ms
6,548 KB
testcase_05 AC 10 ms
6,548 KB
testcase_06 AC 23 ms
6,548 KB
testcase_07 AC 24 ms
6,548 KB
testcase_08 AC 13 ms
6,548 KB
testcase_09 AC 18 ms
6,548 KB
testcase_10 AC 23 ms
6,548 KB
testcase_11 AC 11 ms
6,548 KB
testcase_12 AC 28 ms
6,548 KB
testcase_13 AC 30 ms
6,548 KB
testcase_14 AC 27 ms
6,548 KB
testcase_15 AC 26 ms
6,548 KB
testcase_16 AC 27 ms
6,548 KB
testcase_17 AC 27 ms
6,548 KB
testcase_18 AC 30 ms
6,548 KB
testcase_19 AC 32 ms
6,548 KB
testcase_20 AC 31 ms
6,548 KB
testcase_21 AC 30 ms
6,548 KB
testcase_22 AC 69 ms
6,548 KB
testcase_23 AC 66 ms
6,548 KB
testcase_24 AC 67 ms
6,548 KB
testcase_25 AC 65 ms
6,548 KB
testcase_26 AC 68 ms
6,548 KB
testcase_27 AC 10 ms
6,548 KB
testcase_28 AC 11 ms
6,548 KB
testcase_29 AC 53 ms
6,548 KB
testcase_30 AC 16 ms
6,548 KB
testcase_31 AC 28 ms
6,548 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import std.stdio;
import std.string;
import std.conv;
import std.algorithm;
import std.array;
import std.range;
import core.bitop;

const ulong N = 28;

ulong[][] combinations(ulong m, ulong[] g, ulong[] h){
  ulong[][] result = new ulong[][N / 2 + 1];
  for(ulong i = 0; i < 2 ^^ (N / 2); i++){
    ulong s = 0;
    for(auto j = 0; j < N / 2; j++){
      if((i >> j) & 1){
        s += g[j];
      }else{
        s += h[j];
      }
    }
    result[i.popcnt] ~= s % m;
  }
  for(auto i = 0; i < N / 2; i++){
    result[i].sort;
  }
  return result;
}

ulong solve(ulong m, ulong k, ulong x, ulong[][] h1, ulong[][] h2){
  ulong result = 0;
  for(auto i = (k < N / 2) ? 0 : (k - N / 2); i <= k && i <= N / 2; i++){
    long min = h1[i].length - 1;
    long max = h1[i].length - 1;
    for(auto j = 0; j < h2[k - i].length; j++){
      while(min >= 0 && h1[i][min] + h2[k - i][j] >= x){
        min--;
      }
      while(max > min && (h1[i][max] + h2[k - i][j]) >= m){
        max--;
      }
      result += max - min;
    }
    min = 0;
    max = h1[i].length;
    for(long j = h2[k - i].length - 1; j >= 0; j--){
      while(min < max && h1[i][min] + h2[k - i][j] < m + x){
        min++;
      }
      result += max - min;
    }
  }
  return result;
}

void main(){
  auto M = readln.chomp.to!ulong;
  auto G = readln.chomp.split.to!(ulong[]);
  auto H = readln.chomp.split.to!(ulong[]);
  auto h1 = combinations(M, G[0 .. N / 2], H[0 .. N / 2]);
  auto h2 = combinations(M, G[N / 2 .. N], H[N / 2 .. N]);
  auto Q = readln.chomp.to!int;
  for(auto q = 0; q < Q; q++){
    auto input = readln.chomp.split.to!(long[]);
    auto k = input[0];
    auto x = input[1];
    solve(M, k, x, h1, h2).writeln;
  }
}
0