题目链接

题意

将\(x\)拆成\(a_1+a_2+...+\)的形式,且\(a_1\lt a_2\lt...\),使得\(a_1*a_2*...\)取到最大值

思路

大胆猜结论。

首先拆分的形式中肯定不能有\(1\).

于是预处理出前缀和\(a[i]=\sum_{k=2}^{i}k\),

找到\(\geq x\)的最小的\(a[id]\),接下来:

  1. 如果\(a[id]==x\),意味着\(2+3+...+id=x\),那么答案就是\(2*3*...*id=factorial(id)\)
  2. 否则,求和时就省去超过部分对应的项\(a[id]-x\),即$$2+3+...+(a[id]-x-1)+(a[id]+x+1)+...+id=x$$答案就是\(factorial(id)/(a[id]-x)\)

且慢!如果超过的部分是\(1\),本来就不在求和项中怎么办啊?

(继续猜)那么就将\(2\)省去,再给最后一项加上\(1\),即\(3+4+...+(id-1)+(id+1)=x\),答案就是\(factorial(id-1)/2*(id+1)\).

就此,三种情况完毕,猜完,\(A\)了。

Code

#include <bits/stdc++.h>
#define maxn 50000
using namespace std;
typedef long long LL;
const LL mod = 1e9+7;
LL a[maxn+10], fac[maxn+10];
LL poww(LL a, LL b) {
LL ret = 1;
while (b) {
if (b & 1) (ret *= a) %= mod;
(a *= a) %= mod;
b >>= 1;
}
return ret;
}
void init() {
a[0] = a[1] = 0, a[2] = 2;
for (int i = 3; i <= maxn; ++i) a[i] = a[i-1] + i;
fac[1] = 1;
for (int i = 2; i <= maxn; ++i) fac[i] = fac[i-1] * i % mod;
}
void work() {
int n;
scanf("%d", &n);
if (n == 1) { printf("1\n"); return; }
int id = lower_bound(a, a+maxn, n) - a;
LL ans;
if (a[id] == n) ans = fac[id];
else if (a[id]-n > 1) ans = fac[id] * poww(a[id]-n, mod-2) % mod;
else ans = fac[id-1] * poww(2, mod-2) % mod * (id+1) % mod;
printf("%lld\n", ans);
}
int main() {
init();
int T;
scanf("%d", &T);
while (T--) work();
return 0;
}

hdu 5976 Detachment 脑洞题 猜结论的更多相关文章

  1. HDU 5976 Detachment(拆分)

    HDU 5976 Detachment(拆分) 00 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)   Problem D ...

  2. HDU 5976 Detachment 打表找规律

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5976 Detachment Time Limit: 4000/2000 MS (Java/Other ...

  3. HDU 5976 Detachment 【贪心】 (2016ACM/ICPC亚洲区大连站)

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

  4. hdu 5976 Detachment

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

  5. HDU - 5976 Detachment(逆元)

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

  6. HDU 5976 数学,逆元

    1.HDU 5976 Detachment 2.题意:给一个正整数x,把x拆分成多个正整数的和,这些数不能有重复,要使这些数的积尽可能的大,输出积. 3.总结:首先我们要把数拆得尽可能小,这样积才会更 ...

  7. Atcoder Grand Contest 031 D - A Sequence of Permutations(置换+猜结论)

    Atcoder 题面传送门 & 洛谷题面传送门 猜结论神题. 首先考虑探究题目中 \(f\) 函数的性质,\(f(p,q)_{p_i}=q_i\leftarrow f(p,q)\circ p= ...

  8. 图论(KM算法,脑洞题):HNOI 2014 画框(frame)

    aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAABPoAAANFCAIAAABtIwXVAAAgAElEQVR4nOydeVxTV/r/n9ertaJEC4

  9. D. Minimum Diameter Tree 思维+猜结论

    D. Minimum Diameter Tree 思维+猜结论 题意 给出一颗树 和一个值v 把该值任意分配到任意边上 使得\(\sum\limits_{i,j}p_{ij}=v\) 使得 这颗树任意 ...

随机推荐

  1. GTA5(侠盗猎车5)中文版破解版

    )中文版破解版迅雷下载地址(使用迅雷新建任务填上地址): magnet:?xt=urn:btih:65F16B126D8A656E4FC825DE204EBFAF04B070FC

  2. Nginx: ubuntu系统上查找nginx.conf配置文件的路径

    问题描述:在ubuntu系统上,找到nginx.conf文件的位置. 解决方法:在终端窗口中,输入命令:nginx -t 回显中就可以看到nginx.conf文件的路径了. 参考:https://bl ...

  3. comboBox 下拉宽度自适应

    ///适用combobox绑定datatable private void comboBox_DataSourceChanged(object sender, EventArgs e) { Combo ...

  4. centos7重启后/etc/resolv.conf 被还原解决办法

    每次重启服务器后,/etc/resolv.conf文件就被自动还原了,最后发现是被Network Manager修改了. 查看Network Manager服务状态 systemctl status ...

  5. LeetCode(287)Find the Duplicate Number

    题目 Given an array nums containing n + 1 integers where each integer is between 1 and n (inclusive), ...

  6. LeetCode(290) Word Pattern

    题目 Given a pattern and a string str, find if str follows the same pattern. Here follow means a full ...

  7. LeetCode(149) Max Points on a Line

    题目 Given n points on a 2D plane, find the maximum number of points that lie on the same straight lin ...

  8. JVM执行子系统探究——类文件结构初窥

    类文件(.class)是搞java的都非常熟悉的文件,一般我们在编写java之后文件之后,首先通过javac工具生成.class类字节码文件,而后在执行程序的时候由虚拟机加载执行.那么为什么要生成.c ...

  9. 《Scrum实战》第1课【知易行难】全团课后任务汇总

          1组 孟帅(班长)   kecyru 2017-7-5 http://kecyru.blog.163.com/blog/static/27416617320176411513013 htt ...

  10. git仓库删除所有提交历史记录

    stackoverflow原问题地址:http://stackoverflow.com/questions/13716658/how-to-delete-all-commit-history-in-g ...