跳到正文

个人记录

模意义下的乘法逆元

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

文章信息

发布
2026年1月29日
收录
2026年4月24日
更新
2026年11月6日
篇幅
约 1 分钟

版本

  1. v1当前
C++
#include<bits/stdc++.h>
#define MAXN 3000010
#define ll long long
using namespace std;
int inv[MAXN],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]);
    }
}
原文