前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >数论-同余与逆元

数论-同余与逆元

作者头像
唔仄lo咚锵
发布2020-09-15 15:02:01
6840
发布2020-09-15 15:02:01
举报

文章目录

  • 同余
    • 一元线性同余方程
  • 逆元
    • 逆元与除法取模
  • 例题
    • HDU-5976

同余

代码语言:javascript
复制
ll inv(ll a, ll m){
	ll x, y;
	ll gcd = extend_gcd(a, m, x, y);
	return	(x % m + m) % m;//注意负数
}

递推求逆元:

代码语言:javascript
复制
inv[1] = 1;
for (i = 2; i < n; i++)
    inv[i] = inv[mod % i] * (mod - mod / i) % mod;

逆元与除法取模

逆元一重要作用就是求除法的模,首先我们看一下模运算:

例题


HDU-5976

HDU-5976 Detachment

Problem Description In a highly developed alien society, the habitats are almost infinite dimensional space. In the history of this planet,there is an old puzzle. You have a line segment with x units’ length representing one dimension.The line segment can be split into a number of small line segments: a1,a2, … (x= a1+a2+…) assigned to different dimensions. And then, the multidimensional space has been established. Now there are two requirements for this space: 1.Two different small line segments cannot be equal ( ai≠aj when i≠j). 2.Make this multidimensional space size s as large as possible (s= a1∗a2*…).Note that it allows to keep one dimension.That’s to say, the number of ai can be only one. Now can you solve this question and find the maximum size of the space?(For the final number is too large,your answer will be modulo 10^9+7) Input The first line is an integer T,meaning the number of test cases. Then T lines follow. Each line contains one integer x. 1≤T≤10^6, 1≤x≤10^9 Output Maximum s you can get modulo 10^9+7. Note that we wants to be greatest product before modulo 10^9+7. Sample Input 1 4 Sample Output 4

将一个数拆成若干数,求其乘积最大。 先上结论:一个数n拆成m个数使其乘积最大,则拆成m个n/m;如果nm不整除则拆成一段连续自然数(从2开始,剩下的往前摊);如果不限制m,则拆成最多的3,剩下的拆成2。证明参考 这题要求数不能相同,所以拆成从2开始的连续数,然后就是预处理+逆元取模即可,详见代码。

代码语言:javascript
复制
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 5e4 + 10;
const int mod = 1e9 + 7;
ll sum[maxn];
void init(){
	sum[0] = sum[1] = 1;
	for (int i = 2; i < maxn; i++)
		sum[i] = i * sum[i - 1] % mod;
}
ll extend_gcd(ll a, ll b, ll& x, ll& y){
	if (b == 0){
		x = 1; y = 0;
		return a;
	}
	ll gcd = extend_gcd(b, a % b, y, x);
	y -= x * (a / b);
	return gcd;
}
ll inv(ll a, ll m){
	ll x, y;
	ll gcd = extend_gcd(a, m, x, y);
	return	x = (x % mod + mod) % mod;
}
int main(){
	ll t, s;
	init();
	scanf("%lld", &t);
	while (t--){
		scanf("%lld", &s);
		if (s <= 4) {
			printf("%d\n", s);
			continue;
		}
		ll n = (ll)((-1 + sqrt(8 * s + 9.0)) / 2 + 0.5);
		while ((2 + n) * (n - 1) / 2 > s) n--;
		int tmp = s - (2 + n) * (n - 1) / 2;//差值,
		ll mi = 2 + tmp / (n - 1);//相乘中的最小值
		ll mx = tmp % (n - 1) ? n + tmp / (n - 1) + 1 : n + tmp / (n - 1);//相乘中的最大值
		ll temp = 2 + tmp / (n - 1) + n - 1 - tmp % (n - 1);//相乘中间断的那个数
		ll ans = inv(sum[mi - 1], mod) * sum[mx] % mod;
		if (temp <= mx)
			ans = (ans * inv(temp, mod)) % mod;
		printf("%lld\n", ans);	
	}
	return 0;
}

原创不易,请勿转载本不富裕的访问量雪上加霜 ) 博主首页:https://blog.csdn.net/qq_45034708

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2020-08-09 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 文章目录
  • 同余
    • 逆元与除法取模
    • 例题
      • HDU-5976
      领券
      问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档