H - Writing a Numeral
使用队列完成操作,并预处理把10的1-6e5次方计算出来(模上mod之后的值) 1操作:ans = (ans * 10 + x) % mod; 2操作:ans-首个元素乘以10的n次方可能为负数,所以结果要+mod再%mod,ans = (((ans - q[top] * mo[rear - top]) % mod) + mod) % mod;
typedef long long ll;
ll ans;
const int mod= 998244353;
const int N=6e5+5;
int q[N];
ll mo[N];
int top,rear;
void solve() {
int n, x;
cin >> n;
if (n == 1) {
cin >> x;
q[++rear] = x;
ans = (ans * 10 + x) % mod;
} else if (n == 2) {
ans = (((ans - q[top] * mo[rear - top]) % mod) + mod) % mod;
top++;
} else {
cout << ans << '\n';
}
}
int main() {
ios::sync_with_stdio(false);
mo[0]=1;
for(int i=1;i<=600000;i++) {
mo[i] = (mo[i - 1] * 10) % mod;
}
int t;
cin >> t;
ans = 1;
q[++top]=1;
rear++;
while (t--)
solve();
return 0;
}
G - Round Trip
题目给定范围是n*m<=1e6,所以我用了vector可变长数组来存储数据,题目需要从起点出发,再回到起点,所有点只能走一遍,所以我们从起点旁边开始搜索,看能不能回到起点另一个旁边,如果能,则输出Yes,否则输出No。
const int N=1e6+5;
typedef struct{
int x,y;
}node;
vector<char>f[N];
int dx[4]={0,0,1,-1};
int dy[4]={1,-1,0,0};
int n, m;
int ex,ey;
int bfs(int x,int y,int nx,int ny) {
if (x == nx && y == ny)return 0;
queue<node> q;
q.push(node{x, y});
vector<int> vis[n];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++)vis[i].push_back(0);
}
vis[ex][ey] = 1;
vis[x][y] = 1;
while (!q.empty()) {
x = q.front().x;
y = q.front().y;
q.pop();
if (x == nx && y == ny) {
cout << "Yes\n";
return 1;
}
for (int i = 0; i < 4; i++) {
int x1 = x + dx[i];
int y1 = y + dy[i];
if (x1 < 0 || y1 < 0 || x1 >= n || y1 >= m || f[x1][y1] == '#' || vis[x1][y1])continue;
q.push(node{x1, y1});
vis[x1][y1] = 1;
}
}
return 0;
}
void solve() {
cin >> n >> m;
int x, y;
string s;
for (int i = 0; i < n; i++) {
cin >> s;
for (int j = 0; j < m; j++) {
f[i].push_back(s[j]);
if (f[i][j] == 'S') {
x = i, y = j;
ex = x, ey = y;
}
}
}
int f1 = 0;
for (int i = 0; i < 4; i++) {
int x1 = x + dx[i];
int y1 = y + dy[i];
if (x1 < 0 || y1 < 0 || x1 >= n || y1 >= m || f[x1][y1] == '#')continue;
for (int j = 0; j < 4; j++) {
int x2 = x + dx[j];
int y2 = y + dy[j];
if (x2 < 0 || y2 < 0 || x2 >= n || y2 >= m || f[x2][y2] == '#')continue;
f1 = bfs(x1, y1, x2, y2);
if (f1)return;
}
}
cout << "No\n";
}
I - Copy and Paste Graph
用到了floyd算法,计算多源最短路径。由于k的数据范围太大,所有要把查询时输入的顶点信息变为在原矩阵的顶点。
const int inf = 1e9+5;
long long g[105][105];
int n,k;
void floyd() {
for (int z = 1; z <= n; z++)
for (int i = 1; i <= n; i++)
for (int j = 1; j <= n; j++)
if (g[i][j] > g[i][z] + g[z][j])
g[i][j] = g[i][z] + g[z][j];
}
void solve() {
cin >> n >> k;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
cin >> g[i][j];
if (g[i][j] == 0)
g[i][j] = inf;
}
}
floyd();
int q;
cin >> q;
long long u, v;
while (q--) {
cin >> u >> v;
if (u > n)u = (u % n) == 0 ? n : u % n;
if (v > n)v = (v % n) == 0 ? n : v % n;
if (g[u][v] == inf)g[u][v] = -1;
cout << g[u][v] << '\n';
}
}