AtCoder Beginner Contest 025

Submission #1590937

Source codeソースコード

#include <iostream>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <numeric>
#include <functional>
#include <set>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cmath>
#include <cctype>
#include <climits>
#include <fstream>
#include <bitset>
#include <time.h>
#include <assert.h>
#include <unordered_map>

#define LL long long
#define VI vector<int>
#define VL vector<long long>
#define FOR(i,a,b) for(int i= (a); i<((int)b); ++i)
#define RFOR(i,a) for(int i=(a); i >= 0; --i)
#define FOE(i,a) for(auto i : a)
#define ALL(c) (c).begin(), (c).end()
#define RALL(c) (c).rbegin(), (c).rend()
#define DUMP(x)  cerr << #x << " = " << (x) << endl;
#define SUM(x) std::accumulate(ALL(x), 0LL)
#define MIN(v) *std::min_element(v.begin(), v.end())
#define MAX(v) *std::max_element(v.begin(), v.end())
#define EXIST(v,x) (std::find(v.begin(), v.end(), x) != v.end())
#define BIT(n) (1LL<<(n))
#define UNIQUE(v) v.erase(unique(v.begin(), v.end()), v.end());
#define bitcount(b) __builtin_popcount(b)
const double EPS = 1e-10;
const double PI = acos(-1.0);
#define MOD 1000000007

using namespace std;


#define XY(x, y) (1 << ((y) * 5 + (x)))
bool can_set(int board, int pos) {
    int y = pos / 5;
    int x = pos % 5;

    if ((board & (1 << pos))) { return false; } // posに置かれてる
    if ((1 <= y && y <= 3) && (!(board & XY(x, y - 1)) ^ !(board & XY(x, y + 1)))) { return false; }    // 上下ともに置かれてるorともにおかれてない
    if ((1 <= x && x <= 3) && (!(board & XY(x - 1, y)) ^ !(board & XY(x + 1, y)))) { return false; }    // 左右ともに置かれてるorともにおかれてない
    return true;
}

int dp[1 << 25] = {0};
int main(int argc, char *argv[]) {
    cin.tie(0);
    ios::sync_with_stdio(false);

    int N = 5 * 5;
    vector<int> field(N, -1);   // i が j マス目においてある
    int used = 0;
    FOR(i, 0, N) {
        int x;
        cin >> x;
        if (x != 0) {
            field[x - 1] = i;
            used |= 1 << i;
        }
    }

    dp[0] = 1;
    FOR(board, 0, 1 << N) {
        if (dp[board] == 0) { continue; }

        int n = bitcount(board); // 次に置く数値

        // 場所が決まってる
        if (field[n] != -1) {
            if (!can_set(board, field[n])) { continue; }
            int new_board = board | (1 << field[n]);
            dp[new_board] = (dp[new_board] + dp[board]) % MOD;
        }
        else {
            FOR(j, 0, N) {
                if ((used & (1 << j)) == 1) { continue; }
                if (!can_set(board, j)) { continue; }
                int new_board = board | (1 << j);
                dp[new_board] = (dp[new_board] + dp[board]) % MOD;
            }
        }
    }

    cout << dp[(1 << N) - 1] << endl;
    return 0;
}

Submission

Task問題 D - 25個の整数
User nameユーザ名 MitI_7
Created time投稿日時
Language言語 C++14 (GCC 5.4.1)
Status状態 AC
Score得点 100
Source lengthソースコード長 2954 Byte
File nameファイル名
Exec time実行時間 311 ms
Memory usageメモリ使用量 118528 KB

Test case

Set

Set name Score得点 / Max score Cases
Sample - sample-01.txt,sample-02.txt,sample-03.txt,sample-04.txt
Subtask1 30 / 30 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 70 / 70 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

Test case

Case name Status状態 Exec time実行時間 Memory usageメモリ使用量
sample-01.txt AC 28 ms 24832 KB
sample-02.txt AC 100 ms 116352 KB
sample-03.txt AC 23 ms 256 KB
sample-04.txt AC 26 ms 12544 KB
test-1-01.txt AC 31 ms 39296 KB
test-1-02.txt AC 29 ms 28928 KB
test-1-03.txt AC 29 ms 30976 KB
test-1-04.txt AC 45 ms 106880 KB
test-1-05.txt AC 32 ms 43264 KB
test-1-06.txt AC 42 ms 90496 KB
test-1-07.txt AC 24 ms 2304 KB
test-1-08.txt AC 42 ms 94720 KB
test-1-09.txt AC 30 ms 35072 KB
test-1-10.txt AC 26 ms 12544 KB
test-1-11.txt AC 28 ms 22784 KB
test-1-12.txt AC 44 ms 100992 KB
test-1-13.txt AC 27 ms 18688 KB
test-1-14.txt AC 32 ms 41216 KB
test-1-15.txt AC 36 ms 61824 KB
test-2-01.txt AC 41 ms 47360 KB
test-2-02.txt AC 76 ms 113408 KB
test-2-03.txt AC 33 ms 43264 KB
test-2-04.txt AC 52 ms 117760 KB
test-2-05.txt AC 128 ms 114176 KB
test-2-06.txt AC 311 ms 113664 KB
test-2-07.txt AC 105 ms 92416 KB
test-2-08.txt AC 212 ms 115968 KB
test-2-09.txt AC 267 ms 118528 KB
test-2-10.txt AC 303 ms 103040 KB