HDU 5976 Detachment (逆元)
来源:互联网 发布:淘宝联盟提现手续费 编辑:程序博客网 时间:2024/05/18 03:50
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
14
Sample Output
4
题意
把一个数分解成若干不相等的数,使得这若干个数的积最大,输出最大的积 mod(1e9+7)
。
思路
对于 x
,拆的数越多,积越大,但是因子中不能有 1
。
当 a+b=n
时,我们知道, a=b=n/2
时,乘积最大,题目中要求所有的数不能相等,于是我们只要让这些数互相靠近即可。
有三种情况:
x=∑ni=2i ,比如当x=20=2+3+4+5+6 ,所有数的积为2×3×4×5×6 ,此时数中没有1
,并且达到饱和状态,因此乘积最大,最大乘积为n! 。x=∑ni=2i−1 ,比如当x=19=2+3+4+5+6−1 ,多出的-1
我们没有处理,这时要考虑舍弃掉最小的一个数2
,然后让最大的数加一,也就是19=3+4+5+7 ,因为贪心的思想这样得到的数的个数保证了最大,并且互相靠近,因此乘积最大,最大乘积为(n+1)!2×n 。x=∑ni=2i−k (k>1) ,比如当x=15=2+3+4+5+6−5 ,同样的道理,数中多出的-5
我们要想办法除掉,方法即抵消掉序列中的两个5
,也就是15=2+3+4+6 ,此时乘积最大,最大乘积为n!(∑ni=2i)−x 。
做题的时候首先预处理出前 n
项的和与阶乘 mod(1e9+7)
,然后在计算中出现除法的时候要使用逆元,因为 mod
的关系,所以不能直接使用除法。
AC 代码
#include<stdio.h>#include<string.h>#include<stdlib.h>#include<cmath>#include<iostream>using namespace std;#define maxn 50000const int mod = 1e9+7;typedef __int64 LL;LL sum[maxn];LL mu[maxn];void init(){ mu[1]=1; for(int i=2; i<maxn; i++) { sum[i]=sum[i-1]+i; mu[i]=(mu[i-1]*i)%mod; } sum[1]=1;}LL mult(LL a,int n) // 快速幂{ LL ans=1; while(n) { if(n&1)ans=(ans*a)%mod; a=(a*a)%mod; n>>=1; } return ans;}int main(){ init(); int T; scanf("%d",&T); while(T--) { LL x; scanf("%I64d",&x); int index=lower_bound(sum,sum+maxn,x)-sum; LL ans; if(sum[index]==x) ans=mu[index]; else { if(sum[index]==x+1) ans=((mu[index+1]*mult(index,mod-2))%mod*mult(2,mod-2))%mod; else ans=(mu[index]*mult(sum[index]-x,mod-2))%mod; } printf("%I64d\n",ans); } return 0;}
- HDU 5976 Detachment (逆元)
- HDU 5976 Detachment 逆元
- hdu 5976 Detachment 逆元+二分+数学
- HDU 5976 Detachment (数学规律+逆元)
- HDU 5976 Detachment(逆元)+逆元总结
- HDU 5976 Detachment(逆元+二分优化+数学分析)
- hdu 5976 Detachment(乘法逆元)(附测试用例)
- ☆HDU 5976 Detachment 详细题解(贪心+逆元+前缀和,积)
- hdu 5976 Detachment(逆元+贪心+数论+前缀和+二分 待整理)
- HDU 5976 Detachment
- HDU 5976 Detachment
- HDU 5976 Detachment
- hdu 5976 Detachment
- HDU 5976 Detachment
- HDU 5976 Detachment
- hdu5976 Detachment(逆元+二分优化+数学思维)
- 【数论】【逆元】【贪心】HDU5976 Detachment
- HDU 5976 && 2016ICPC大连 F: Detachment(找规律)
- 细说分布式数据库的过去、现在与未来
- JavaScript中的宽松相等和严格相等
- hpuoj寻找单身狗
- Unity实例.003官方示例Survival Shooter Tutorial核心代码学习
- Android 手机识别
- HDU 5976 Detachment (逆元)
- 【CodeForces】792D Paths in a Complete Binary Tree
- php 操作 redis 常用方法代码例子
- PHP实现文章定时发布设置
- linux初级学习之系统恢复2-11
- 关于move_uploaded_file()出错的问题
- 异常
- nodejs npm install -g 全局安装和非全局安装的区别
- 啥是UGC和PGC