hdu5976(前缀+逆元)
来源:互联网 发布:新能源就业前景 知乎 编辑:程序博客网 时间:2024/06/05 12:43
Detachment
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1313 Accepted Submission(s): 376
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)
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:
1.Two different small line segments cannot be equal (
2.Make this multidimensional space size s as large as possible (s=
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
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
题意:
给你一个数n,要求将n分成任意不同的数,并使其乘积s最大,结果s对1000000007取模
解题思路:
易知n<=4时,s=n
当n>4时,先写几个数可知道规律
5=2*3
6=2*4→2*3*1(将多出的1加到3上)
7=3*4→2*3*2(多余的2分别加到2和3上)
8=3*5→2*3*3(多余的3先分别加到2和3上但还是多1,再加到3上,于是2变成3,3变成5)
9=2*3*4
10=2*3*5→2*3*4*1(同理1加到4上)
发现规律后分析一下:
对于大于4的n能将其拆分成
n=2+3+4+…+k+△x (△x<=k)
即2+3+4+…+k<=n<2+3+4+…+k+1
当将△x分配完之后我们都会得到以下两个式子中的一个
①2*3*...*(i-1)*(i+1)*...*k*(k+1)
②3*4*...*i*(i+1)*...*k*(k+2)
式子由两个连续的积组成,所以我们可以用两个阶乘除来求解,又涉及到取模所以要用逆元
用前缀和和前缀积二分查找来优化,求逆元用扩展欧几里得
#include<iostream>#include<cstdio>#include<cstdlib>#include<cmath>#include <algorithm>using namespace std;typedef long long LL;const LL mod=1e9+7;LL sum[50000];LL mul[50000];void inti(){ sum[0]=sum[1]=0; mul[0]=mul[1]=1; for(int i=2;i<50000;i++) { sum[i]=sum[i-1]+i; mul[i]=((mul[i-1]%mod)*(i%mod))%mod; }}void exgcd(LL a, LL b, LL& d, LL& x, LL& y){ if(!b){ d = a; x = 1; y = 0;} else{ exgcd(b, a%b, d, y, x); y -= x*(a/b); }}LL inv(LL a, LL n){ LL d, x, y; exgcd(a, n, d, x, y); return (x+n)%n;}int main(){ inti(); int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); if(n<=4) { printf("%d\n",n); continue; } int pos=upper_bound(sum,sum+50000,n)-sum-1; int num=n-sum[pos]; long long x,y,ans; if(num==pos) { ans=(mul[pos]%mod*inv(2,mod)%mod*(pos+2)%mod)%mod; } else if(num==0) ans=mul[pos]; else { ans=(mul[pos+1]%mod*inv(mul[pos-num+1],mod)%mod*mul[pos-num]%mod)%mod; } printf("%lld\n",ans); } return 0;}
阅读全文
0 0
- hdu5976(前缀+逆元)
- hdu5976贪心乘法逆元
- 【数论】【逆元】【贪心】HDU5976 Detachment
- hdu5976 Detachment(逆元+二分优化+数学思维)
- HDU5976
- HDU 5685 (前缀+逆元 水~)
- HDU5976 Detachment
- 贪心-HDU5976
- Astar2016-Round1 Problem A(前缀积+乘法逆元+快速幂取余)
- ☆HDU 5976 Detachment 详细题解(贪心+逆元+前缀和,积)
- hdu 5976 Detachment(逆元+贪心+数论+前缀和+二分 待整理)
- 【前缀和】 洛谷1371 NOI元丹
- 哑元与运算符重载:前缀++与后缀++
- maven的插件前缀元数据存储位置
- 前缀
- 前缀
- HDU5976(找规律+费马小定理求逆元)
- 逆元
- HSV与RGB
- 数据结构实验之链表八:Farey序列
- main :处理命令行选项
- c#使用WPD读取便携式设备信息一(枚举设备、连接设备及读取设备信息)
- sql server学习总结一
- hdu5976(前缀+逆元)
- zookeeper 安装的三种模式
- BZOJ 1003: [ZJOI2006]物流运输
- 暑期集训第一阶段总结(附带杂题总结+字符串总结)
- 将请求转换为json请求
- Blackhat 2017&Defcon 25学习笔记
- 在java中关于使用scanner接受char类型字符的方法
- C# WPD PortableDeviceApiLib获取便携设备列表
- BZOJ 1050: [HAOI2006]旅行comf