P3811 【模板】乘法逆元

题目背景

这是一道模板题

题目描述

给定 n,p求 1∼n 中所有整数在模 p 意义下的乘法逆元。

输入格式

一行两个正整数 n,p。

输出格式

输出 n 行,第 i 行表示 i 在模 p 下的乘法逆元。

输入输出样例

输入 #1

10 13

输出 #1

1
7
9
10
8
11
2
5
3
4

说明/提示

1 <= n <= 3 * 10 ^ 6 ,n<p<20000528

输入保证 p 为质数。

这篇文章里有逆元的证明→数论基础篇

但对于此题求一连串数字modp的逆元,一个一个的求会超时(废话),所以就用到了逆元的线性递推公式,

 然后直接套用公式就可以了,注意数据范围要开ll,而且cin,cout会超时

AC代码:

#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <map>
#include <vector>
#include <queue>
using namespace std;
typedef long long ll;
typedef vector<int> VI;
typedef pair<int, int> PII;

ll n, p;
ll inv[3000010];
int main()
{
	cin >> n >> p;
	inv[1] = 1;
	printf("1\n");
	for (int i = 2; i <= n; i++) {
		inv[i] = p - p / i * inv[p % i] % p;
		printf("%d\n", inv[i]);
	}
	return 0;
}

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值