結果
問題 |
No.911 ラッキーソート
|
ユーザー |
|
提出日時 | 2019-10-18 23:27:49 |
言語 | D (dmd 2.109.1) |
結果 |
AC
|
実行時間 | 777 ms / 2,000 ms |
コード長 | 2,047 bytes |
コンパイル時間 | 1,078 ms |
コンパイル使用メモリ | 129,364 KB |
実行使用メモリ | 20,228 KB |
最終ジャッジ日時 | 2024-06-22 02:52:23 |
合計ジャッジ時間 | 14,284 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge5 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 3 |
other | AC * 46 |
ソースコード
import std.stdio, std.array, std.string, std.conv, std.algorithm; import std.typecons, std.range, std.random, std.math, std.container; import std.numeric, std.bigint, core.bitop, core.stdc.string, core.stdc.stdlib; void main() { auto s = readln.split.map!(to!long); auto N = s[0].to!int; auto L = s[1]; auto R = s[2]; auto A = readln.split.map!(to!long).array; immutable int M = 61; auto possible = new int[][](M+1); void rec(int l, int r, int keta, int si) { if (keta < 0) return; if (r - l == 1) { possible[si-1] ~= 2; return; } auto B = A[l..r].map!(a => (a & (1L << keta)) > 0).array; auto x = B.uniq.array.length; if (x > 2) { writeln(0); exit(0); } else if (x == 2) { possible[si-1] ~= B.front; int cut = B.countUntil(!B.front).to!int; rec(l, l+cut, keta-1, si+1); rec(l+cut, r, keta-1, si+1); } else { possible[si-1] ~= 2; rec(l, r, keta-1, si+1); } } rec(0, N, M-1, 1); long solve(long ub) { auto dp = new long[][](M+1, 2); dp[0][0] = 1; foreach (i; 0..M) { auto hoge = possible[i].sort().uniq.array; int[] nexts; if (hoge.canFind(0) && hoge.canFind(1)) { writeln(0); exit(0); } else if (hoge.canFind(0)) { nexts ~= 0; } else if (hoge.canFind(1)) { nexts ~= 1; } else { nexts ~= [0, 1]; } bool b = (ub & (1L << (M - i - 1))) > 0; foreach (under; 0..2) { foreach (next; nexts) { if (!under && next && !b) continue; dp[i+1][under||(next<b)] += dp[i][under]; } } } return dp[M].sum; } auto ansR = solve(R); auto ansL = L == 0 ? 0 : solve(L - 1); writeln(ansR - ansL); }