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 ( 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)

在一个高度发达的外星文明中,有着近乎无限维度的生存空间。
在这颗星球的历史中,有道古老的谜题。
你有一条长x个单位长度的线段表示一个维度。这条线段可以被拆分为若干小线段:a1,a2, … (x= a1+a2+…)并分配为不同的维度。然后,多维空间就建立起来了。现在,这个空间有两个限制:
1.两个不同的小线段不能相等( ai≠aj when i≠j)。
2.多维空间的大小s要尽可能大(s= a1∗a2*...)。注意,各维度只能保持一种。也就是说,ai的值必须唯一。
现在的你能解决这个问题并找出最大的空间吗?(结果可能很大,输出模10^9+7)

CN

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

第一行为一个整数T,描述测试用例的数量。
随后T行。每行有一个整数x。
1≤T≤10^6, 1≤x≤10^9

CN

Output - 输出

  Maximum s you can get modulo 10^9+7. Note that we wants to be greatest product before modulo 10^9+7.

s的最大值需要模10^9+7。注意,模10^9+7是在获得最大乘积后。

CN

Sample Input - 输入样例

1
4

Sample Output - 输出样例

4

题解
  先猜一发最优策略:2+3+4+5+6+……

  然后再猜一发对于剩下数的分配策略:每次从后往前,对每个数+1。

  接着就发现似乎策略就是这样了。

  后面需要做的处理:求前n项和,求前n项积。

  最后遇到(a % mod)/(b % mod)的时候需要用逆元。

  把(a/b)%mod转化为(a * inv b)%mod 不嫌弃速度的话可以用费马小定理:

    mod为质数时,inv a = a^(mod - 2)

  或者用其他方法…………

代码 C++

 1 #include <cstdio>
 2 #include <algorithm>
 3 #define mod 1000000007
 4 #define mx 44722
 5 __int64 mul[mx] = { 1 }, sum[mx];
 6 __int64 qMod(__int64 a, int n){
 7     __int64 opt = 1;
 8     while (n){
 9         if (n & 1) opt = (opt*a) % mod;
10         n >>= 1;
11         a = (a*a) % mod;
12     }
13     return opt;
14 }
15 __int64 lr(int l, int r){//[l, r]
16     return (mul[r] * qMod(mul[l - 1], mod - 2)) % mod;
17 }
18 void rdy(){
19     int i, j;
20     for (i = 1, j = 2; i < mx; ++i, ++j){
21         sum[i] = j + sum[i - 1];
22         mul[i] = (j * mul[i - 1]) % mod;
23     }
24 }
25 int main(){
26     rdy();
27     int t, len, w, l, r;
28     __int64 x, opt;
29     scanf("%d", &t);
30     while (t--){
31         scanf("%I64d", &x);
32         if (x < 5) opt = x;
33         else{
34             len = std::upper_bound(sum, sum + mx, x) - sum - 1;
35             r = len + (x - sum[len]) / len;
36             w = (x - sum[len]) % len;
37             opt = lr(r - len + 1, r - w);
38             if (w) opt *= lr(r + 2 - w, r + 1), opt %= mod;
39         }
40         printf("%I64d\n", opt);
41     }
42     return 0;
43 }

转载于:https://www.cnblogs.com/Simon-X/p/6279899.html

HDU 5976 Detachment(拆分)相关推荐

  1. HDU 5976 Detachment

    这道题目是向这位博主学习的,链接贴出来:☆HDU 5976 Detachment 详细题解(贪心+逆元+前缀和,积),讲的很细致,很感谢: 思路: 1.题意是将数x分成若干个正整数和,使得这些整数的乘 ...

  2. HDU - 5976 Detachment(贪心思维)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5976 题意:把一个数拆成若干个不相同的数之和,问这些数的乘积最大是多少. 思路:一般要使乘积最大,那么 ...

  3. HDU 5976 Detachment(数学+结论)

    原题地址:http://acm.hdu.edu.cn/showproblem.php?pid=5976 参考博客:https://blog.csdn.net/qq_34374664/article/d ...

  4. HDU 5976 Detachment 题解(贪心+逆元+前缀和,积)

    个人体会:无脑取模,最为致命!因为把存放前缀和的数组取了模,导致一些本来不等的元素变成了相同的元素,二分搜索出错...而且C++会T,G++就ac了.. http://acm.hdu.edu.cn/s ...

  5. HDU - 5976 Detachment(逆元)

    题意:将一个数x拆成a1+a2+a3+--,ai不等于aj,求最大的a1*a2*a3*--. 分析: 1.预处理前缀和前缀积,因为拆成1对乘积没有贡献,所以从2开始拆起. 2.找到一个id,使得2+3 ...

  6. HDU 5976 Detachment 逆元

    题意: 把一个数分解分解成若干个不相等的数,怎么分解使得这若干个数的积最大,输出这个积mod(1e+9) 分析: 考虑x=20的情况 如果不分解 那么 max ans===>20 如果分解成2个 ...

  7. HDU Problem - 5976 Detachment(逆元,阶乘打表,数学)

    题目链接 Problem Description In a highly developed alien society, the habitats are almost infinite dimen ...

  8. HDU 5976 2016ICPC大连 F: Detachment(找规律)

    题意: 将n拆成a1+a2+a3+-+ax的形式(ai≠aj),让a1*a2*a3*-*ax的值最大,求这个最大值 找规律 n=35:2+3+4+5+6+7+8:      max:2*3*4*5*6 ...

  9. Detachment HDU - 5976(数学+费马小定理求逆元+前缀和前缀积)

    题意:给定一个数,让你分成互不相等的n个数(n为自然数),使这些数的乘积最大,输出最大乘积. 题解:本文参考传送门 首先:那就是不能分出1来,因为1乘任何数都是它本身,而因为分出了1,另一部分也变小了 ...

最新文章

  1. 干货 :数据可视化的10个关键术语
  2. 深刻剖析与实战BCELoss详解(主)和BCEWithLogitsLoss(次)以及与普通CrossEntropyLoss的区别(次)
  3. dtoj#4178. 配对(pair)
  4. SpringBoot-MyBatis
  5. wait()和sleep()
  6. spring自动装配、注解
  7. panel,dialog,window组件越界问题汇总
  8. antd 的form 表单怎么回显数据_antd design Form动态增减表单项(多个),组装数据及编辑回显,选择初始值,控制添加减少表单项数量等...
  9. MXY-单点登陆系统
  10. 产生式系统的设计及代码实现(植物识别系统)
  11. Kubernetes pull requests
  12. 【机器学习理论】换底公式--以e,2,10为底的对数关系转化
  13. 信念就是一种观念对不对_信念与观念一字之差天差地别
  14. 2020年中考英语计算机考试,2020年中考英语听说测试考生问答
  15. 效率系列(二) Win10常用快捷键
  16. 正则表达式包含某字符串且不包含某些字符串
  17. 【限时】21天学习挑战赛 - 经典算法
  18. 家庭网络搭建_家庭网络
  19. 江南大学大作业答案 计算机网络,江南大学大作业答案 计算机网络
  20. 超级实用的PLSQLDEV客户端总结

热门文章

  1. 一个人开发手游有多难
  2. 共用体的使用_C++
  3. hadoop之理解tmp目录下的文件
  4. Python使用pngquant实现批量压缩图片
  5. 十年数据标注:缺席的独角兽与走不出的围城
  6. AssetBundle文件结构浅析
  7. Calibre物理验证技术点滴 (中)
  8. power_supply子系统
  9. TiDB HTAP 深度解读
  10. ssm毕设项目大学生就业管理系统5t101(java+VUE+Mybatis+Maven+Mysql+sprnig)