Submission #609879


Source Code Expand

using System;
using System.Linq;
using System.Collections.Generic;
using Debug = System.Diagnostics.Debug;
using StringBuilder = System.Text.StringBuilder;
using System.Numerics;
using Number = System.Int64;

namespace Program
{

    public class Solver
    {
        public void Solve()
        {
            var a = sc.Integer(25);
            var dp = new ModInteger[1 << 25];
            var masks = Enumerate(26, x => new List<int>());
            for (int i = 0; i < 1 << 25; i++)
            {
                var cnt = BitOperation.PopCount(i);

                masks[cnt].Add(i);

            }


            dp[0] = 1;
            for (int i = 1; i <= 25; i++)
            {

                var next = new ModInteger[1 << 25];
                var p = -1;
                for (int j = 0; j < 25; j++)
                    if (a[j] == i) p = j;

                foreach (var j in masks[i - 1])
                {
                    if (dp[j].num == 0) continue;
                    if (p == -1)
                    {
                        for (int k = 0; k < 25; k++)
                        {
                            if (a[k] > 0) continue;
                            if (ok(j, k))
                                next[j | (1 << k)] += dp[j];
                        }
                    }
                    else
                    {
                        if (ok(j, p))
                        {
                            next[j | (1 << p)] += dp[j];
                            var val = next[j | (1 << p)];
                        }
                    }

                }
                dp = next;
            }
            var ans = dp[(1 << 25) - 1];
            IO.Printer.Out.WriteLine(ans);

        }
        public bool ok(int i, int j)
        {
            if ((i >> j & 1) == 1) return false;

            var x = j / 5;
            var y = j % 5;
            var u = x - 1;
            var d = x + 1;
            var l = y - 1;
            var r = y + 1;
            var tate = false;
            var yoko = false;
            if (u < 0 || d >= 5) tate = true;
            if (l < 0 || r >= 5) yoko = true;

            if (!tate)
                tate = (i >> (u * 5 + y) & 1) == (i >> (d * 5 + y) & 1);
            if (!yoko)
                yoko = (i >> (x * 5 + l) & 1) == (i >> (x * 5 + r) & 1);
            return tate && yoko;

        }

        public IO.StreamScanner sc = new IO.StreamScanner(Console.OpenStandardInput());
        static T[] Enumerate<T>(int n, Func<int, T> f) { var a = new T[n]; for (int i = 0; i < n; ++i) a[i] = f(i); return a; }
        static public void Swap<T>(ref T a, ref T b) { var tmp = a; a = b; b = tmp; }

    }
}

#region main
static class Ex
{
    static public string AsString(this IEnumerable<char> ie) { return new string(System.Linq.Enumerable.ToArray(ie)); }
    static public string AsJoinedString<T>(this IEnumerable<T> ie, string st = " ") { return string.Join(st, ie); }
    static public void Main()
    {
        var solver = new Program.Solver();
        solver.Solve();
        Program.IO.Printer.Out.Flush();
    }
}
#endregion
#region Ex
namespace Program.IO
{
    using System.IO;
    using System.Text;
    using System.Globalization;
    public class Printer : StreamWriter
    {
        static Printer() { Out = new Printer(Console.OpenStandardOutput()) { AutoFlush = false }; }
        public static Printer Out { get; set; }
        public override IFormatProvider FormatProvider { get { return CultureInfo.InvariantCulture; } }
        public Printer(System.IO.Stream stream) : base(stream, new UTF8Encoding(false, true)) { }
        public Printer(System.IO.Stream stream, Encoding encoding) : base(stream, encoding) { }
        public void Write<T>(string format, T[] source) { base.Write(format, source.OfType<object>().ToArray()); }
        public void WriteLine<T>(string format, T[] source) { base.WriteLine(format, source.OfType<object>().ToArray()); }
    }
    public class StreamScanner
    {
        public StreamScanner(Stream stream) { str = stream; }
        public readonly Stream str;
        private readonly byte[] buf = new byte[1024];
        private int len, ptr;
        public bool isEof = false;
        public bool IsEndOfStream { get { return isEof; } }
        private byte read()
        {
            if (isEof) return 0;
            if (ptr >= len) { ptr = 0; if ((len = str.Read(buf, 0, 1024)) <= 0) { isEof = true; return 0; } }
            return buf[ptr++];
        }
        public char Char() { byte b = 0; do b = read(); while ((b < 33 || 126 < b) && !isEof); return (char)b; }

        public string Scan()
        {
            var sb = new StringBuilder();
            for (var b = Char(); b >= 33 && b <= 126; b = (char)read())
                sb.Append(b);
            return sb.ToString();
        }
        public string ScanLine()
        {
            var sb = new StringBuilder();
            for (var b = Char(); b != '\n'; b = (char)read())
                if (b == 0) break;
                else if (b != '\r') sb.Append(b);
            return sb.ToString();
        }
        public long Long()
        {
            if (isEof) return long.MinValue;
            long ret = 0; byte b = 0; var ng = false;
            do b = read();
            while (b != 0 && b != '-' && (b < '0' || '9' < b));
            if (b == 0) return long.MinValue;
            if (b == '-') { ng = true; b = read(); }
            for (; true; b = read())
            {
                if (b < '0' || '9' < b)
                    return ng ? -ret : ret;
                else ret = ret * 10 + b - '0';
            }
        }
        public int Integer() { return (isEof) ? int.MinValue : (int)Long(); }
        public double Double() { var s = Scan(); return s != "" ? double.Parse(Scan(), CultureInfo.InvariantCulture) : double.NaN; }
        private T[] enumerate<T>(int n, Func<T> f)
        {
            var a = new T[n];
            for (int i = 0; i < n; ++i) a[i] = f();
            return a;
        }

        public char[] Char(int n) { return enumerate(n, Char); }
        public string[] Scan(int n) { return enumerate(n, Scan); }
        public double[] Double(int n) { return enumerate(n, Double); }
        public int[] Integer(int n) { return enumerate(n, Integer); }
        public long[] Long(int n) { return enumerate(n, Long); }
    }
}
#endregion
#region ModNumber
public partial struct ModInteger
{
    public const long Mod = (long)1e9 + 7;
    public long num;
    public ModInteger(long n) : this() { num = n % Mod; if (num < 0) num += Mod; }
    public override string ToString() { return num.ToString(); }
    public static ModInteger operator +(ModInteger l, ModInteger r) { var n = l.num + r.num; if (n >= Mod) n -= Mod; return new ModInteger() { num = n }; }
    public static ModInteger operator -(ModInteger l, ModInteger r) { var n = l.num + Mod - r.num; if (n >= Mod) n -= Mod; return new ModInteger() { num = n }; }
    public static ModInteger operator *(ModInteger l, ModInteger r) { return new ModInteger(l.num * r.num); }
    public static implicit operator ModInteger(long n) { return new ModInteger(n); }
}
#endregion
#region BitOperation
static public partial class BitOperation
{
    static public int PopCount(ulong i)
    {
        i = i - ((i >> 1) & 0x5555555555555555UL);
        i = (i & 0x3333333333333333ul) + ((i >> 2) & 0x3333333333333333ul);
        return (int)(unchecked(((i + (i >> 4)) & 0xF0F0F0F0F0F0F0Ful) * 0x101010101010101ul) >> 56);
    }
    static public int PopCount(long i)
    {
#if DEBUG || TRACE
        unchecked
        {
#endif
            {
                i = i - ((i >> 1) & 0x5555555555555555);
                i = (i & 0x3333333333333333) + ((i >> 2) & 0x3333333333333333);
                return (int)(((i + (i >> 4)) & 0xF0F0F0F0F0F0F0F) * 0x101010101010101) >> 56;
            }
#if DEBUG || TRACE
        }
#endif
    }
    /// <summary>
    /// 最上位ビットの取得
    /// </summary>
    static public int Highest(int n)
    {
        n = n | (n >> 1);
        n = n | (n >> 2);
        n = n | (n >> 4);
        n = n | (n >> 8);
        n = n | (n >> 16);
        return n ^ (n >> 1);

    }

    /// <summary>
    /// iのkビット目を0にする
    /// </summary>
    static public long Down(long i, int k)
    {
        return i & ~(1L << k);
    }
    /// <summary>
    /// iのkビット目を1にする
    /// </summary>
    static public long Up(long i, int k)
    {
        return i | (1L << k);
    }

}
#endregion

Submission Info

Submission Time
Task D - 25個の整数
User camypaper
Language C# (Mono 3.2.1.0)
Score 30
Code Size 8807 Byte
Status TLE
Exec Time 5063 ms
Memory 213532 KB

Judge Result

Set Name Sample Subtask1 Subtask2
Score / Max Score 0 / 0 30 / 30 0 / 70
Status
AC × 4
AC × 19
AC × 27
TLE × 2
Set Name Test Cases
Sample sample-01.txt, sample-02.txt, sample-03.txt, sample-04.txt
Subtask1 sample-01.txt, sample-02.txt, sample-03.txt, sample-04.txt, test-1-01.txt, test-1-02.txt, test-1-03.txt, test-1-04.txt, test-1-05.txt, test-1-06.txt, test-1-07.txt, test-1-08.txt, test-1-09.txt, test-1-10.txt, test-1-11.txt, test-1-12.txt, test-1-13.txt, test-1-14.txt, test-1-15.txt
Subtask2 sample-01.txt, sample-02.txt, sample-03.txt, sample-04.txt, test-1-01.txt, test-1-02.txt, test-1-03.txt, test-1-04.txt, test-1-05.txt, test-1-06.txt, test-1-07.txt, test-1-08.txt, test-1-09.txt, test-1-10.txt, test-1-11.txt, test-1-12.txt, test-1-13.txt, test-1-14.txt, test-1-15.txt, test-2-01.txt, test-2-02.txt, test-2-03.txt, test-2-04.txt, test-2-05.txt, test-2-06.txt, test-2-07.txt, test-2-08.txt, test-2-09.txt, test-2-10.txt
Case Name Status Exec Time Memory
sample-01.txt AC 4476 ms 198312 KB
sample-02.txt AC 4462 ms 198312 KB
sample-03.txt AC 4455 ms 198328 KB
sample-04.txt AC 4462 ms 198328 KB
test-1-01.txt AC 4498 ms 198312 KB
test-1-02.txt AC 4455 ms 198328 KB
test-1-03.txt AC 4464 ms 198196 KB
test-1-04.txt AC 4498 ms 198332 KB
test-1-05.txt AC 4522 ms 198328 KB
test-1-06.txt AC 4852 ms 198212 KB
test-1-07.txt AC 4580 ms 198240 KB
test-1-08.txt AC 4608 ms 198212 KB
test-1-09.txt AC 4495 ms 198196 KB
test-1-10.txt AC 4610 ms 198328 KB
test-1-11.txt AC 4633 ms 198324 KB
test-1-12.txt AC 4449 ms 198328 KB
test-1-13.txt AC 4679 ms 198196 KB
test-1-14.txt AC 4490 ms 198196 KB
test-1-15.txt AC 4514 ms 198328 KB
test-2-01.txt AC 4747 ms 198308 KB
test-2-02.txt AC 4698 ms 198328 KB
test-2-03.txt AC 4470 ms 198312 KB
test-2-04.txt AC 4753 ms 198312 KB
test-2-05.txt AC 4690 ms 198312 KB
test-2-06.txt AC 4908 ms 198324 KB
test-2-07.txt AC 4991 ms 198328 KB
test-2-08.txt TLE 5062 ms 198324 KB
test-2-09.txt AC 4990 ms 198320 KB
test-2-10.txt TLE 5063 ms 213532 KB