CODE FESTIVAL 2016 Grand Final

Submission #1220817

Source codeソースコード

/+ dub.sdl:
    name "A"
    dependency "dcomp" version=">=0.6.0"
+/

import std.stdio, std.algorithm, std.range, std.conv;
import std.typecons, std.math;
// import dcomp.foundation, dcomp.scanner;
// import dcomp.numeric.primitive;
// import dcomp.modint;

alias Mint = ModInt!(10^^9 + 7);
Mint[] p2, fact, ifac;
static this() {
    p2 = new Mint[10000];
    p2[0] = Mint(1);
    foreach (i; 1..10000) {
        p2[i] = p2[i-1]*Mint(2);
    }
    fact = factTable!Mint(10000);
    ifac = invFactTable!Mint(10000);
}

int n;
Mint[] mtc, basec;

void first() {
    mtc = cntMt();
    basec = cntBase();
}

Mint vecvec(int a, int c) {
    Mint[] dp = new Mint[c+1];
    dp[0] = mtc[a];
    Mint sm = 0;
    foreach (i; 0..n+1) {
        if (n+c < a+i) break;
        sm += dp[c]*mtc[i]/basec[i];
        foreach_reverse (j; 0..c+1) {
            dp[j] = dp[j] * (p2[n-a+j] - p2[i]);
            if (j) dp[j] += dp[j-1] * p2[n-a] * (p2[a] - p2[j-1]);
        }
    }
    return sm;
}

int main() {
    auto sc = new Scanner(stdin);
    sc.read(n);
    first();
//    writeln(mtc);
//    writeln(basec);
/*    foreach (c; 0..n+1) {
        Mint sm = 0;
        foreach (a; 0..n+1) {
            sm += vecvec(a, c);
        }
        writeln(c, "=", sm/mtc[c]);
        writeln(); writeln();
    }*/
    bool[][] mt = new bool[][](n, n);
    foreach (i; 0..n) {
        foreach (j; 0..n) {
            int d;
            sc.read(d);
            mt[i][j] = d == 1;
        }
    }
//    writeln(rnk(mt));
    int c = rnk(mt);
    Mint sm = 0;
    foreach (a; 0..n+1) {
        sm += vecvec(a, c);
    }
    writeln(sm/mtc[c]);
    return 0;
}

Mint[] cntMt() {
    auto res = new Mint[n+1];
    res[0] = Mint(1);
    foreach (i; 0..n) {
        foreach_reverse(j; 0..n+1) {
            res[j] *= p2[j];
            if (j) res[j] += res[j-1] * (p2[n] - p2[j-1]);
        }
    }
    return res;
}
Mint[] cntBase() {
    auto res = new Mint[n+1];
    res[0] = Mint(1);
    foreach (i; 1..n+1) {
        res[i] = res[i-1] * (p2[n] - p2[i-1]);
    }
    return res;
}

int rnk(bool[][] mt) {
    int c = 0;
    foreach (i; 0..n) {
        int ni = -1;
        foreach (j; c..n) {
            if (mt[j][i]) {
                ni = j;
                break;
            }
        }
        if (ni == -1) continue;
        swap(mt[c], mt[ni]);
        foreach (j; c+1..n) {
            if (!mt[j][i]) continue;
            foreach (k; 0..n) {
                mt[j][k] ^= mt[c][k];
            }
        }
        c++;
    }
    return c;
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/modint.d */
// module dcomp.modint;

// import dcomp.numeric.primitive;

struct ModInt(uint MD) if (MD < int.max) {
    import std.conv : to;
    uint v;
    this(int v) {this(long(v));}
    this(long v) {this.v = (v%MD+MD)%MD;}
    static auto normS(uint x) {return (x<MD)?x:x-MD;}
    static auto make(uint x) {ModInt m; m.v = x; return m;}
    auto opBinary(string op:"+")(ModInt r) const {return make(normS(v+r.v));}
    auto opBinary(string op:"-")(ModInt r) const {return make(normS(v+MD-r.v));}
    auto opBinary(string op:"*")(ModInt r) const {return make( (long(v)*r.v%MD).to!uint );}
    auto opBinary(string op:"/")(ModInt r) const {return this*inv(r);}
    auto opOpAssign(string op)(ModInt r) {return mixin ("this=this"~op~"r");}
    static ModInt inv(ModInt x) {return ModInt(extGcd!int(x.v, MD)[0]);}
    string toString() {return v.to!string;}
}

unittest {
    static assert( is(ModInt!(uint(1000000000) * 2))); //not overflow
    static assert(!is(ModInt!(uint(1145141919) * 2))); //overflow!
    alias Mint = ModInt!(10^^9+7);
    // negative check
    assert(Mint(-1).v == 10^^9 + 6);
    assert(Mint(-1L).v == 10^^9 + 6);

    Mint a = 48;
    Mint b = Mint.inv(a);
    assert(b.v == 520833337);

    Mint c = Mint(15);
    Mint d = Mint(3);
    assert((c/d).v == 5);
}

struct DModInt(string name) {
    import std.conv : to;
    static uint MD;
    uint v;
    this(int v) {this(long(v));}
    this(long v) {this.v = ((v%MD+MD)%MD).to!uint;}
    auto normS(uint x) {return (x<MD)?x:x-MD;}
    auto make(uint x) {DModInt m; m.MD = MD; m.v = x; return m;}
    auto opBinary(string op:"+")(DModInt r) {
        return make(normS(v+r.v));
    }
    auto opBinary(string op:"-")(DModInt r) {
        return make(normS(v+MD-r.v));
    }
    auto opBinary(string op:"*")(DModInt r) {
        return make((long(v)*r.v%MD).to!uint);
    }
    auto opBinary(string op:"/")(DModInt r) {
        return this*inv(r);
    }
    auto opOpAssign(string op)(DModInt r) {return mixin ("this=this"~op~"r");}
    static DModInt inv(DModInt x) {
        return DModInt(extGcd!int(x.v, MD)[0]);
    }
    string toString() {return v.to!string;}
}

unittest {
    alias Mint = DModInt!("default");
    Mint.MD = 10^^9 + 7;
    //negative check
    assert(Mint(-1).v == 10^^9 + 6);
    assert(Mint(-1L).v == 10^^9 + 6);
    Mint a = Mint(48);
    Mint b = Mint.inv(a);
    assert(b.v == 520833337);
    Mint c = Mint(15);
    Mint d = Mint(3);
    assert((c/d).v == 5);
}

template isModInt(T) {
    const isModInt =
        is(T : ModInt!MD, uint MD) || is(S : DModInt!S, string s);
}


T[] factTable(T)(size_t length) if (isModInt!T) {
    import std.range : take, recurrence;
    import std.array : array;
    return T(1).recurrence!((a, n) => a[n-1]*T(n)).take(length).array;
}

// optimize
T[] invFactTable(T)(size_t length) if (isModInt!T) {
    import std.algorithm : map, reduce;
    import std.range : take, recurrence, iota;
    import std.array : array;
    auto res = new T[length];
    res[$-1] = T(1) / iota(1, length).map!T.reduce!"a*b";
    foreach_reverse (i, v; res[0..$-1]) {
        res[i] = res[i+1] * T(i+1);
    }
    return res;
}

T[] invTable(T)(size_t length) if (isModInt!T) {
    auto f = factTable!T(length);
    auto invf = invFactTable!T(length);
    auto res = new T[length];
    foreach (i; 1..length) {
        res[i] = invf[i] * f[i-1];
    }
    return res;
}

unittest {
    import std.stdio;
    alias Mint = ModInt!(10^^9 + 7);
    auto r = factTable!Mint(20);
    Mint a = 1;
    assert(r[0] == Mint(1));
    foreach (i; 1..20) {
        a *= Mint(i);
        assert(r[i] == a);
    }
    auto p = invFactTable!Mint(20);
    foreach (i; 1..20) {
        assert((r[i]*p[i]).v == 1);
    }
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
//fold(for old compiler)
static if (__VERSION__ <= 2070) {
    template fold(fun...) if (fun.length >= 1) {
        auto fold(R, S...)(R r, S seed) {
            import std.algorithm : reduce;
            static if (S.length < 2) {
                return reduce!fun(seed, r);
            } else {
                import std.typecons : tuple;
                return reduce!fun(tuple(seed), r);
            }
        }
    }
    unittest {
        import std.stdio;
        auto l = [1, 2, 3, 4, 5];
        assert(l.fold!"a+b"(10) == 25);
    }
}
version (X86) static if (__VERSION__ < 2071) {
    int bsf(ulong v) {
        foreach (i; 0..64) {
            if (v & (1UL << i)) return i;
        }
        return -1;
    }
    int bsr(ulong v) {
        foreach_reverse (i; 0..64) {
            if (v & (1UL << i)) return i;
        }
        return -1;   
    }
    int popcnt(ulong v) {
        int c = 0;
        foreach (i; 0..64) {
            if (v & (1UL << i)) c++;
        }
        return c;
    }
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/scanner.d */
// module dcomp.scanner;

class Scanner {
    import std.stdio : File;
    import std.conv : to;
    import std.range : front, popFront, array, ElementType;
    import std.array : split;
    import std.traits : isSomeChar, isStaticArray, isArray; 
    import std.algorithm : map;
    File f;
    this(File f) {
        this.f = f;
    }
    char[512] lineBuf;
    char[] line;
    private bool succ() {
        import std.range.primitives : empty, front, popFront;
        import std.ascii : isWhite;
        while (true) {
            while (!line.empty && line.front.isWhite) {
                line.popFront;
            }
            if (!line.empty) break;
            if (f.eof) return false;
            line = lineBuf[];
            f.readln(line);
        }
        return true;
    }

    private bool readSingle(T)(ref T x) {
        import std.algorithm : findSplitBefore;
        import std.string : strip;
        import std.conv : parse;
        if (!succ()) return false;
        static if (isArray!T) {
            alias E = ElementType!T;
            static if (isSomeChar!E) {
                //string or char[10] etc
                //todo optimize
                auto r = line.findSplitBefore(" ");
                x = r[0].strip.dup;
                line = r[1];
            } else {
                auto buf = line.split.map!(to!E).array;
                static if (isStaticArray!T) {
                    //static
                    assert(buf.length == T.length);
                }
                x = buf;
                line.length = 0;
            }
        } else {
            x = line.parse!T;
        }
        return true;
    }
    int read(T, Args...)(ref T x, auto ref Args args) {
        if (!readSingle(x)) return 0;
        static if (args.length == 0) {
            return 1;
        } else {
            return 1 + read(args);
        }
    }
}



unittest {
    import std.path : buildPath;
    import std.file : tempDir;
    import std.algorithm : equal;
    import std.stdio : File;
    string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
    auto fout = File(fileName, "w");
    fout.writeln("1 2 3");
    fout.writeln("ab cde");
    fout.writeln("1.0 1.0 2.0");
    fout.close;
    Scanner sc = new Scanner(File(fileName, "r"));
    int a;
    int[2] b;
    char[2] c;
    string d;
    double e;
    double[] f;
    sc.read(a, b, c, d, e, f);
    assert(a == 1);
    assert(equal(b[], [2, 3]));
    assert(equal(c[], "ab"));
    assert(equal(d, "cde"));
    assert(e == 1.0);
    assert(equal(f, [1.0, 2.0]));
}

unittest {
    import std.path : buildPath;
    import std.file : tempDir;
    import std.algorithm : equal;
    import std.stdio : File, writeln;
    import std.datetime;
    string fileName = buildPath(tempDir, "kyuridenanmaida.txt");
    auto fout = File(fileName, "w");
    foreach (i; 0..1_000_000) {
        fout.writeln(3*i, " ", 3*i+1, " ", 3*i+2);
    }
    fout.close;
    writeln("Scanner Speed Test(3*1,000,000 int)");
    StopWatch sw;
    sw.start;
    Scanner sc = new Scanner(File(fileName, "r"));
    foreach (i; 0..500_000) {
        int a, b, c;
        sc.read(a, b, c);
        assert(a == 3*i);
        assert(b == 3*i+1);
        assert(c == 3*i+2);
    }
    foreach (i; 500_000..700_000) {
        int[3] d;
        sc.read(d);
        int a = d[0], b = d[1], c = d[2];
        assert(a == 3*i);
        assert(b == 3*i+1);
        assert(c == 3*i+2);
    }
    foreach (i; 700_000..1_000_000) {
        int[] d;
        sc.read(d);
        assert(d.length == 3);
        int a = d[0], b = d[1], c = d[2];
        assert(a == 3*i);
        assert(b == 3*i+1);
        assert(c == 3*i+2);
    }
    writeln(sw.peek.msecs, "ms");
}
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/numeric/primitive.d */
// module dcomp.numeric.primitive;

import std.traits;

T pow(T, U)(T x, U n) if (!isFloatingPoint!T && isIntegral!U) {
    return pow(x, n, T(1));
}

T pow(T, U)(T x, U n, T e) if (isIntegral!U) {
    while (n) {
        if (n & 1) e *= x;
        x *= x;
        n /= 2;
    }
    return e;
}

unittest {
    assert(pow(3, 5) == 243);
    assert(pow(3, 5, 2) == 486);
}

T lcm(T)(in T a, in T b) {
    import std.numeric : gcd;
    return a / gcd(a,b) * b;
}

unittest {
    assert(lcm(2, 4) == 4);
    assert(lcm(3, 5) == 15);
    assert(lcm(1, 1) == 1);
    assert(lcm(0, 100) == 0);
}

//a*T[0]+b*T[1]=T[2], T[2]=gcd
//todo: to binary extgcd
T[3] extGcd(T)(in T a, in T b) 
if (!isIntegral!T || isSigned!T) //unsignedはNG
{
    if (b==0) {
        return [1, 0, a];
    } else {
        auto e = extGcd(b, a%b);
        return [e[1], e[0]-a/b*e[1], e[2]];
    }
}

unittest {
    import std.numeric : gcd;
    foreach (i; 0..100) {
        foreach (j; 0..100) {
            auto e = extGcd(i, j);
            assert(e[2] == gcd(i, j));
            assert(e[0] * i + e[1] * j == e[2]);
        }
    }
}

Submission

Task問題 H - AB=C Problem
User nameユーザ名 †yosupo†
Created time投稿日時
Language言語 D (LDC 0.17.0)
Status状態 AC
Score得点 1500
Source lengthソースコード長 12868 Byte
File nameファイル名
Exec time実行時間 453 ms
Memory usageメモリ使用量 3452 KB

Test case

Set

Set name Score得点 / Max score Cases
Sample - example0.txt,example1.txt
All 1500 / 1500 000.txt,001.txt,002.txt,003.txt,004.txt,005.txt,006.txt,007.txt,008.txt,009.txt,010.txt,011.txt,012.txt,013.txt,014.txt,015.txt,016.txt,017.txt,018.txt,019.txt,020.txt,021.txt,022.txt,023.txt,024.txt,025.txt,026.txt,027.txt,028.txt,029.txt,030.txt,031.txt,032.txt,033.txt,034.txt,035.txt,036.txt,037.txt,038.txt,039.txt,example0.txt,example1.txt

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
000.txt AC 1 ms 380 KB
001.txt AC 1 ms 380 KB
002.txt AC 1 ms 380 KB
003.txt AC 1 ms 380 KB
004.txt AC 2 ms 2300 KB
005.txt AC 1 ms 380 KB
006.txt AC 2 ms 2428 KB
007.txt AC 1 ms 380 KB
008.txt AC 1 ms 380 KB
009.txt AC 1 ms 380 KB
010.txt AC 1 ms 380 KB
011.txt AC 1 ms 380 KB
012.txt AC 1 ms 380 KB
013.txt AC 1 ms 380 KB
014.txt AC 26 ms 508 KB
015.txt AC 10 ms 380 KB
016.txt AC 5 ms 380 KB
017.txt AC 9 ms 380 KB
018.txt AC 3 ms 2428 KB
019.txt AC 1 ms 380 KB
020.txt AC 38 ms 508 KB
021.txt AC 250 ms 3196 KB
022.txt AC 3 ms 380 KB
023.txt AC 56 ms 892 KB
024.txt AC 432 ms 3324 KB
025.txt AC 216 ms 1148 KB
026.txt AC 303 ms 1148 KB
027.txt AC 157 ms 1020 KB
028.txt AC 30 ms 892 KB
029.txt AC 237 ms 1148 KB
030.txt AC 293 ms 3068 KB
031.txt AC 195 ms 1148 KB
032.txt AC 19 ms 892 KB
033.txt AC 344 ms 1148 KB
034.txt AC 17 ms 892 KB
035.txt AC 11 ms 508 KB
036.txt AC 453 ms 3452 KB
037.txt AC 357 ms 1404 KB
038.txt AC 21 ms 892 KB
039.txt AC 424 ms 1404 KB
example0.txt AC 1 ms 380 KB
example1.txt AC 1 ms 380 KB