跳到正文

题解

题解 P3811 【【模板】乘法逆元】

zcysky约 1 分钟
原文
文章信息与版本

文章信息

发布
2017年7月13日
收录
2025年12月12日
更新
2026年11月13日
篇幅
约 1 分钟
获赞
312
题目
P3811

版本

  1. v1当前

害怕 不懂为什么底下能扯到线性筛什么的……

普通球逆元就是naive的pow(x,mod-2)注意此时mod必须为质数

这题一下子叫你求这么多很明显不是这么做的嘛

逆元有线性递推的公式,推一下就好了呀。

C++
#include<bits/stdc++.h>
#define N 3000010
typedef long long ll;
using namespace std;
int inv[N],n,p;
inline int read(){
    int f=1,x=0;char ch;
    do{ch=getchar();if(ch=='-')f=-1;}while(ch<'0'||ch>'9');
    do{x=x*10+ch-'0';ch=getchar();}while(ch>='0'&&ch<='9');
    return f*x;
}
int main(){
    n=read();p=read();inv[1]=1;puts("1");
    for(int i=2;i<=n;i++){
        inv[i]=(ll)(p-p/i)*inv[p%i]%p;
        printf("%d\n",inv[i]);
    }
}