NOIP模板复习 _ 线性推逆元 – [模板]

#include 
const int N = 3000010;
int inv[N], n, p;
char pbuf[1000000], *pp = pbuf;
inline void push(const char c) {
    *pp++ = c; 
    if(pp-pbuf == 1000000) fwrite(pbuf, 1, 1000000, stdout), pp = pbuf;
}
inline void write(int x) {
    char sta[10]; int top = 0;
    do {sta[++top] = x%10^48; x/=10;} while(x);
    while(top) push(sta[top--]);
    push('\n');
}
int main() {
    scanf("%d%d", &n, &p);
    inv[1] = 1; puts("1");
    for(int i = 2; i <= n; i ++ ) {
        inv[i] = ((long long)p-p/i)*inv[i%p]%p;
        write(inv[i]);
    }
    fwrite(pbuf, 1, pp-pbuf, stdout);
}

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注