[TIOJ]1546. 填方格遊戲

題目連結:http://tioj.infor.org/problems/1546

不難發現做什麼操作的順序並不重要,一定有一位必輸,因此只要把可以做幾次操作算出來再mod N即可。
因為a xor b xor b 會等於 a ,所以可以觀察出[a][b]項 其實就是 [0][0] xor [a][0] xor [0][b]。
#include <iostream>
#include <algorithm>
#include <cmath>
#include <bitset>
#include <queue>
#include <vector>
#define lld long long
#define PB push_back
#define F first
#define S second
#define jizz cin.tie(0);ios_base::sync_with_stdio(0);
#define endl '\n'
using namespace std;
typedef pair<int,int> Pair;
int u[10004],l[10004];
lld ans,p,q;
inline lld count(int x){
    if(x >= q)return q-p+1;
    if(x < p)return 0;
    return x-p+1;
}
inline int num(int x,int y){
    if(x == 0)return u[y];
    if(y == 0)return l[x];
    return u[0]^l[x]^u[y];
}
int main(){jizz
    int n,m;cin >> n >> m >> p >> q;
    ans = m*m-2*m+1;
    for(int i = 0 ; i < m ; i++)cin >> u[i];
    for(int j = 1 ; j < m ; j++)cin >> l[j];
    for(int i = 0 ; i < m ; i++){
        lld tmp = 1;
        ans += count(tmp);
        for(int j = 1 ; j < m ; j++){
            if(num(i,j) >= num(i,j-1))tmp++;
            else tmp = 1;
            ans += count(tmp);
        }
    }
    for(int j = 0 ; j < m ; j++){
        lld tmp = 1;
        ans += count(tmp);
        for(int i = 1; i < m ; i++){
            if(num(i,j) >= num(i-1,j))tmp++;
            else tmp = 1;
            ans += count(tmp);
        }
    }
    cout << ans%n << endl;
    return 0;
}

留言

這個網誌中的熱門文章

[TIOJ]1617. [Interactive] 中位數

[TIOJ]1337. 隕石

[TIOJ]1994. 冰塊線