[NOIP 2024 模拟2]表
[NOIP 2024 模拟2]表
题意
给定质数
思路
70 pts
暴力建图跑 Floyd,时间复杂度
100 pts
通过 70 pts 代码打表发现,答案不超过
时间复杂度
代码
#include <bits/stdc++.h>
#define pii pair<int,int>
#define fi first
#define se second
using namespace std;
const int N = 2005;
const int mod = 998244353;
int f[N][N], P, t;
vector <pii> E[N];
struct node {int id, dis;};
bool operator < (node a, node b) {
return a.dis > b.dis;
}
priority_queue <node> q;
bool vis[N];
void spfa(int s) {
memset(vis, 0, sizeof(vis));
f[s][s] = 0;
q.push({s, 0});
while (!q.empty()) {
int x = q.top().id; q.pop();
if (vis[x]) continue;
vis[x] = 1;
for (auto y : E[x]) {
if (f[s][y.fi] > f[s][x] + y.se) {
f[s][y.fi] = f[s][x] + y.se;
q.push({y.fi, f[s][y.fi]});
}
}
}
}
signed main() {
freopen("newb.in", "r", stdin);
freopen("newb.out", "w", stdout);
scanf("%d%d", &P, &t);
memset(f, 0x3f, sizeof(f));
for (int i = 1; i < P; i ++)
for (int j = max(1, i - 20); j <= min(P - 1, i + 20); j ++)
E[i].push_back({i * j % P, abs(i - j)});
for (int i = 1; i < P; i ++) spfa(i);
int ans = 0;
for (int i = 1; i < P; i ++)
for (int j = 1; j < P; j ++) {
int res = 1, a = t, b = (i - 1) * (P - 1) + j - 1;
for (; b; b >>= 1, a = 1ll * a * a % mod)
if (b & 1) res = 1ll * res * a % mod;
ans = (ans + 1ll * f[i][j] * res % mod) % mod;
}
printf("%d\n", ans);
return 0;
}
本文来自博客园,作者:maniubi,转载请注明原文链接:https://www.cnblogs.com/maniubi/p/18410932,orz
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 地球OL攻略 —— 某应届生求职总结
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 提示词工程——AI应用必不可少的技术
· .NET周刊【3月第1期 2025-03-02】