HDU 5976 逆元

Detachment

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)


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 ( aiaj when i≠j).
2.Make this multidimensional space size s as large as possible (s= a1a2*...).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


题意:给你一个x,找出若干个不同的整数使得和为x,积要最大,然后求出积mod后的值


题解:对于一个数,拆的因子越小,积越大,因为当a+b=n时,根据二次函数性质知道,当b=a=n/2时,乘积最大,现在不能相等,我们只要靠近即可

所以我们可以求2+3+...+n+s=x,先求出n即2+3+...+n<x<2+3+...+n+(n+1),然后将某个数向右平移s个单位变为n+1即可。

处理出前缀积mod即可

ps:因为要把某个数变为另一个数时要用到除法,所以要用逆元。不要+1,因为1对乘积没影响,而且占用和,所以没用。


#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define mod 1000000007
typedef long long ll;
ll num[100005],top=0,ans[100005],ni[100005];
void init(){
	num[++top]=1;
	ans[top]=1;
	ll i,now=0,mul=1;
	ni[top]=1;
	for(i=2;now<=1000000000;i++){
		now+=i;
		num[++top]=now;//前缀积
		mul=mul*i%mod;
		ans[top]=mul;
		ni[i]=(mod-mod/i)*ni[mod%i]%mod;//逆元
	}
}
int main(){
	init();
	ll t;
	scanf("%lld",&t);
	while(t--){
		ll x;
		scanf("%lld",&x);
		ll s=lower_bound(num+1,num+1+top,x)-num;//找n
		if(num[s]==x){
			printf("%lld\n",ans[s]);
		}
		else{
			s--;
			ll need=x-num[s],la=ans[s];
			if(need==s){//如果s==n  那就把2拿过来,因为没有1
				la=la*ni[2]%mod;
				la=la*(s+2)%mod;
			}
			else{
				la=la*ni[s+1-need]%mod;
				la=la*(s+1)%mod;
			}
			printf("%lld\n",la);
		}
	}
	return 0;
}


### HDU OJ 排列组合问题解法 排列组合问题是算法竞赛中的常见题型之一,涉及数学基础以及高效的实现技巧。以下是关于如何解决此类问题的一些通用方法和具体实例。 #### 数学基础知识 在处理排列组合问题时,需要熟悉以下几个基本概念: - **阶乘计算**:用于求解全排列的数量 $ n! = n \times (n-1) \times ... \times 1 $[^4]。 - **组合数公式**:$ C(n, k) = \frac{n!}{k!(n-k)!} $ 表示从 $ n $ 中选取 $ k $ 的方案数[^5]。 - **快速幂运算**:当涉及到模运算时,可以利用费马小定理优化逆元的计算[^6]。 #### 题目推荐与分析 以下是一些典型的 HDU OJ 上的排列组合题目及其可能的解法: ##### 1. 基础排列组合计数 - **HDU 2039 近似数** - 描述:给定两个整数 $ a $ 和 $ b $,统计区间内的近似数数量。 - 方法:通过枚举每一位上的可能性来构建合法数字并计数[^7]。 ```cpp #include <iostream> using namespace std; long long comb(int n, int r){ if(r > n || r < 0)return 0; long long res=1; for(int i=1;i<=r;i++)res=res*(n-i+1)/i; return res; } int main(){ int t,n,k; cin>>t; while(t--){ cin>>n>>k; cout<<comb(n+k-1,k)<<endl; // 组合数应用 } } ``` ##### 2. 动态规划的应用 - **HDU 1028 Ignatius and the Princess III** - 描述:给出正整数 $ m $ 和 $ n $,问有多少种方式把 $ m $ 分成最多 $ n $ 份。 - 方法:定义状态转移方程 $ dp[i][j]=dp[i-1][j]+dp[i][j-i] $ 来表示当前总和为 $ j $ 并分成至多 $ i $ 份的情况数目[^8]。 ```cpp #include<bits/stdc++.h> using namespace std; const int MAXN=1e3+5; long long c[MAXN][MAXN]; void init(){ memset(c,0,sizeof(c)); c[0][0]=1; for(int i=1;i<MAXN;i++){ c[i][0]=c[i][i]=1; for(int j=1;j<i;j++) c[i][j]=(c[i-1][j-1]+c[i-1][j])%(1e9+7); } } int main(){ init(); int T,m,n; scanf("%d",&T); while(T--){ scanf("%d%d",&m,&n); printf("%lld\n",c[m+n-1][min(m,n)]); } } ``` #### 总结 针对不同类型的排列组合问题,可以选择合适的工具和技术加以应对。无论是简单的直接计算还是复杂的动态规划模型,都需要扎实的基础知识作为支撑。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值