题解:P16814 [蓝桥杯 2026 国 Python B] 蓝桥大富翁
ToastBread · · 题解
这是一篇使用逆元进行计算的题解。
记第
直接模拟显然是不行的,因为会爆 long long。
考虑在更新的过程中动态取模,我们需要使用 逆元 来完成除法操作。这样就解决了除法的问题。
时间复杂度为
:::info[计算答案(C++)]
#include<bits/stdc++.h>
using namespace std;
#define homo_number 114514
#define mod 998244353
//#define mod 1000000007
#define int long long
int fpow(int x,int y)
{
int ret=1;
while(y)
{
if(y&1)
{
ret*=x;
ret%=mod;
}
x*=x;
x%=mod;
y>>=1;
}
return ret;
}
int f[3005],tot=1;
signed main()
{
for(int i = 1; i <= 2026; i++)
{
f[i]=i;
tot*=i;
tot%=mod;
}
for(int i = 1; i <= 2026; i++)
{
for(int j = 1; j <= 2026; j++) f[j]=tot*fpow(f[j],mod-2)%mod;
tot=1;
for(int j = 1; j <= 2026; j++) tot*=f[j],tot%=mod;
}
cout<<tot;
return 0;
}
::: :::info[计算答案(Python)]
MOD = 998244353
def fpow(x, y):
ret = 1
while y:
if y & 1:
ret = (ret * x) % MOD
x = (x * x) % MOD
y >>= 1
return ret
def main():
f = [0] * 3005
tot = 1
for i in range(1, 2027):
f[i] = i
tot = (tot * i) % MOD
for i in range(1, 2027):
for j in range(1, 2027):
f[j] = (tot * fpow(f[j], MOD - 2)) % MOD
tot = 1
for j in range(1, 2027):
tot = (tot * f[j]) % MOD
print(tot)
if __name__ == "__main__":
main()
:::
:::info[输出答案(C++,Python)]
C++
```cpp
#include<bits/stdc++.h>
using namespace std;
signed main()
{
cout<<146753746<<endl;
}
Python
print("146753746")
:::
AI 使用说明:本题解使用了 DeepSeek 进行 C++ 至 Python 代码的转换。