DZY Loves Partition HDU - 5646
原题链接
考察:二分
思路:
很明显尽量取相邻的,一开始我还以为和\(n,k\)的奇偶有关,实际是没有关系的.
(1) 无解:\(n<\frac{(1+k)*k}{2}\)
(2) 将1~k偏移到\(<=n\)的最大\(k\)长度连续和,剩下的余数从大开始补.
Code
#include <iostream>
#include <cstring>
using namespace std;
typedef long long LL;
const int M = 1e9 + 7;
int n, k;
LL get(int m)
{
return (2 * m - k + 1) * k >> 1;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d%d", &n, &k);
if(n<1ll*(1+k)*k/2)
{
printf("-1\n");
continue;
}
LL l = k, r = n;
while(l<r)
{
LL mid = l + r + 1 >> 1;
if(get(mid)<=n)
l = mid;
else
r = mid - 1;
}
LL d = n-get(r),res = 1;
for (int i = r; i >= r - k + 1;i--)
{
if(d)
res = res * (i + 1)%M, d--;
else
res = res * i%M;
}
printf("%lld\n", res);
}
return 0;
}