原来一直都理解错了题意……
记录一下每层有多少个有楼梯的房间
比如有 3 个房间有楼梯, 需要找到第 4 个房间, 那就相当于找到第 1 个房间
通过这样减少模拟次数。
代码
#include <cstdio>
#include <cstring>
#include <iostream>
#include <cstdlib>
using namespace std;
#define MAXN (10100)
#define MAXM (110)
#define mod (20123)
bool dr[MAXN][MAXM];
int nxt[MAXN][MAXM], cnt[MAXN];
int main()
{
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; ++ i)
for(int j = 0; j < m; ++ j)
cin >> dr[i][j], scanf("%d", &nxt[i][j]);
for(int i = 1; i <= n; ++ i)
for(int j = 0; j < m; ++ j)
if(dr[i][j])
cnt[i] ++;
int fang;
cin >> fang;
int ans = 0;
for(int ceng = 1; ceng <= n; ++ ceng)
{
ans = (ans%mod + nxt[ceng][fang]%mod)%mod;
int x = nxt[ceng][fang]%cnt[ceng];
if(!x) x = cnt[ceng];
while(x)
{
if(dr[ceng][fang]) x --;
if(x) fang ++; fang%=m;
}
}
cout << ans%mod << endl;
return 0;
}
大样例:
input
10 10
1 3
0 8
0 8
0 4
1 8
1 9
0 4
1 10
1 4
0 7
1 3
1 10
0 5
0 4
1 1
0 10
1 4
0 6
0 4
0 1
1 3
1 9
1 5
1 8
1 6
1 4
1 7
1 8
0 7
0 10
1 9
1 7
1 1
1 1
0 6
0 4
1 7
0 1
1 7
1 3
1 1
1 8
1 3
0 3
1 3
0 3
1 8
1 5
1 3
1 8
1 4
1 8
0 10
0 10
1 6
0 7
1 7
1 9
1 7
1 6
1 4
1 7
0 10
1 7
1 8
0 2
1 10
0 8
0 9
0 10
1 2
1 2
0 3
0 4
0 2
1 1
1 2
0 5
1 7
1 6
1 6
1 10
1 5
0 6
0 3
0 3
1 1
1 6
0 4
1 4
1 8
1 3
1 7
1 6
0 2
1 3
0 3
0 2
1 1
1 1
8
output
50