Submission #1363632


Source Code Expand

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

import std.stdio, std.algorithm, std.range, std.conv;
// import dcomp.foundation, dcomp.scanner, dcomp.array;
import std.container.rbtree;
// import dcomp.modint;
// import dcomp.numeric.primitive;

alias Mint = ModInt!(10^^9 + 7);

int n;
bool[][] g;
int[] a;
//int[] cnt;

long[] buf1, buf2;
void first() {
    a = new int[n];
//    cnt = new int[n+1];
    buf1 = new long[n+1];
    buf2 = new long[n+1];
}

Mint dfs(int p, Mint sm) {
    int[15] cnt;
    if (p == n) {
        void calc(int q, long[] buf) {        
            buf[] = 0;
            cnt[] = 0;
            int fr = 1;
            foreach (i; 0..p) {
                if (g[q][i]) {
                    cnt[a[i]]++;
                    fr *= 2;
                }
            }
            int c = 0;
            while (c < n && cnt[c]) c++;
            int s = 0;
            foreach (i; 0..c+1) {
                fr >>= cnt[i]; // divide
                buf[i] = fr;
                fr *= (1<<cnt[i]) - 1;
            }
        }
        calc(n, buf1); calc(n+1, buf2);
        long ans = buf1.sum * buf2.sum;
        foreach (i; 0..n+1) {
            ans -= buf1[i] * buf2[i];
        }
        return Mint(ans)*sm;
    }
    int fr = 1;
    cnt[] = 0;
    foreach (i; 0..p) {
        if (g[p][i]) {
            cnt[a[i]]++;
            fr *= 2;
        }
    }

    int c = 0;
    while (c < n && cnt[c]) c++;
    Mint s = 0;

    foreach (i; 0..c+1) {
        a[p] = i;
        fr >>= cnt[i]; // divide        
        s += dfs(p+1, sm*Mint(fr));
        fr *= (1<<cnt[i]) - 1;
    }
    return s;
}


int main() {
    auto sc = new Scanner(stdin);
    int m;
    sc.read(n, m);
    g = new bool[][](n, n);
    foreach (i; 0..m) {
        int a, b;
        sc.read(a, b); a--; b--;
        g[n-1-a][n-1-b] = true;
    }
    //for debug
//    foreach (i; 0..n) g[i][] = true;
    if (n == 2) {
        writeln(m);
        return 0;
    }
    n -= 2;
    first();

    Mint off = Mint(0);
    if (g[n+1][n]) {
        off += pow(Mint(2), m-1);
    }

    writeln(off + dfs(0, Mint(1)));
    return 0;
}
/* 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;}
}

 
 

 

 
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;}
    static auto normS(uint x) {return (x<MD)?x:x-MD;}
    static auto make(uint x) {DModInt m; m.MD = MD; m.v = x; return m;}
     
    auto opBinary(string op:"+")(DModInt r) const {return make(normS(v+r.v));}
     
    auto opBinary(string op:"-")(DModInt r) const {return make(normS(v+MD-r.v));}
     
    auto opBinary(string op:"*")(DModInt r) const {return make((long(v)*r.v%MD).to!uint);}
     
    auto opBinary(string op:"/")(DModInt r) const {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;}
}

 
 

 

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;
}

 
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;
}

 
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/foundation.d */
// module dcomp.foundation;
 
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);
            }
        }
    }
     
}
version (X86) static if (__VERSION__ < 2071) {
    import core.bitop : bsf, bsr, popcnt;
    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) {
                 
                 
                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) {
                     
                    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);
        }
    }
}



 

 
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/numeric/primitive.d */
// module dcomp.numeric.primitive;

import std.traits;
import std.bigint;

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

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

 

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

 

 
 
T[3] extGcd(T)(in T a, in T b) 
if (!isIntegral!T || isSigned!T)  
{
    if (b==0) {
        return [T(1), T(0), a];
    } else {
        auto e = extGcd(b, a%b);
        return [e[1], e[0]-a/b*e[1], e[2]];
    }
}

 
/* IMPORT /home/yosupo/Program/dcomp/source/dcomp/array.d */
// module dcomp.array;

 
T[N] fixed(T, size_t N)(T[N] a) {return a;}

 
 

 
 
struct FastAppender(A) {
    import std.algorithm : max;
    import std.range.primitives : ElementEncodingType;
    import core.stdc.string : memcpy;

    private alias T = ElementEncodingType!A;
    private T* _data;
    private size_t len, cap;
     
    @property size_t length() {return len;}
     
    void reserve(size_t nlen) {
        import core.memory : GC;
        if (nlen <= cap) return;
        
        void* nx = GC.malloc(nlen * T.sizeof);

        cap = nlen;
        if (len) memcpy(nx, _data, len * T.sizeof);
        _data = cast(T*)(nx);
    }
     
    void opOpAssign(string op : "~")(T item) {
        if (len == cap) {
            reserve(max(4, cap*2));
        }
        _data[len++] = item;
    }
     
    void clear() {
        len = 0;
    }
     
    T[] data() {
        return (_data) ? _data[0..len] : null;
    }
}

 

Submission Info

Submission Time
Task F - Games on DAG
User yosupo
Language D (LDC 0.17.0)
Score 1600
Code Size 9961 Byte
Status AC
Exec Time 4681 ms
Memory 256 KB

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 1600 / 1600
Status
AC × 4
AC × 40
Set Name Test Cases
Sample 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt
All 0_00.txt, 0_01.txt, 0_02.txt, 0_03.txt, 1_00.txt, 1_01.txt, 1_02.txt, 1_03.txt, 1_04.txt, 1_05.txt, 1_06.txt, 1_07.txt, 1_08.txt, 1_09.txt, 1_10.txt, 1_11.txt, 1_12.txt, 1_13.txt, 1_14.txt, 1_15.txt, 1_16.txt, 1_17.txt, 1_18.txt, 1_19.txt, 1_20.txt, 1_21.txt, 1_22.txt, 1_23.txt, 1_24.txt, 1_25.txt, 1_26.txt, 1_27.txt, 1_28.txt, 1_29.txt, 1_30.txt, 1_31.txt, 1_32.txt, 1_33.txt, 1_34.txt, 1_35.txt
Case Name Status Exec Time Memory
0_00.txt AC 1 ms 256 KB
0_01.txt AC 1 ms 256 KB
0_02.txt AC 1 ms 256 KB
0_03.txt AC 1 ms 256 KB
1_00.txt AC 1 ms 256 KB
1_01.txt AC 1 ms 256 KB
1_02.txt AC 1 ms 256 KB
1_03.txt AC 4641 ms 256 KB
1_04.txt AC 2677 ms 256 KB
1_05.txt AC 4681 ms 256 KB
1_06.txt AC 612 ms 256 KB
1_07.txt AC 1223 ms 256 KB
1_08.txt AC 4617 ms 256 KB
1_09.txt AC 1978 ms 256 KB
1_10.txt AC 2462 ms 256 KB
1_11.txt AC 1669 ms 256 KB
1_12.txt AC 3381 ms 256 KB
1_13.txt AC 2875 ms 256 KB
1_14.txt AC 3833 ms 256 KB
1_15.txt AC 1217 ms 256 KB
1_16.txt AC 1020 ms 256 KB
1_17.txt AC 4122 ms 256 KB
1_18.txt AC 290 ms 256 KB
1_19.txt AC 3885 ms 256 KB
1_20.txt AC 1 ms 256 KB
1_21.txt AC 2 ms 256 KB
1_22.txt AC 13 ms 256 KB
1_23.txt AC 90 ms 256 KB
1_24.txt AC 1 ms 256 KB
1_25.txt AC 1 ms 256 KB
1_26.txt AC 425 ms 256 KB
1_27.txt AC 2 ms 256 KB
1_28.txt AC 1 ms 256 KB
1_29.txt AC 57 ms 256 KB
1_30.txt AC 1 ms 256 KB
1_31.txt AC 1313 ms 256 KB
1_32.txt AC 61 ms 256 KB
1_33.txt AC 1 ms 256 KB
1_34.txt AC 8 ms 256 KB
1_35.txt AC 27 ms 256 KB