Detachment (Hdu5976)2016大连现场赛F题
2017-06-04 17:27
399 查看
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,a2a1,a2,
… (x= a1+a2a1+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≠ajai≠aj when
i≠j).
2.Make this multidimensional space size s as large as possible (s= a1∗a2a1∗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)
InputThe 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
OutputMaximum s you can get modulo 10^9+7. Note that we wants to be greatest product before modulo 10^9+7.
Sample Input
Sample Output
4
给出一个正整数,给这个数分解n段,但是各段不能有相同的数,求这些分法中各段乘积最大的答案;
这道题最开始想到分段,第一段是2,第二段是3,第三段是4,以此类推,这样是乘积最大的分段方法,
如果最后还剩一个数,就把这个分成n个1,分别加到每个头上,例如12,分成2,3,4后剩3,就变成3,4,5;
由于测试组数过多,分段要利用等差数列前n项求和公式求出分到第几项,不能每次算得时候都重新乘,取余,所以 要先用数组打表记录从2乘到N的乘积,然后中间空哪个数就在求这个数的逆元,在取余就好了;例如12,分成3,4,5;利用打表求得是2乘到5,由于多乘了2,所以求出2的逆元,再重新乘取余就好了;注意求得逆元是负数,要变成正数。
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long ll;
#define mod 1000000007
ll f[50000];
void Ex_gcd(ll a, ll b, ll &x, ll &y)
{
if(b == 0)//递归出口
{
x = 1;
y = 0;
return;
}
ll x1, y1;
Ex_gcd(b, a%b, x1, y1);
x = y1;
y = x1-(a/b)*y1;
}
ll su[50000];
void dabiao()
{
su[1]=1;
su[2]=2;
for(int i=3;i<=45000;i++)
{
su[i]=(su[i-1]*i)%mod;
}
}
int main()
{
int T;
ll a;
scanf("%d",&T);
dabiao();
while(T--)
{
ll ans=1;
scanf("%lld",&a);
if(a<=4)
printf("%lld\n",a);
else
{
f[0]=1;
ll k=(ll)((-1+sqrt(9+8*a))/2);
// cout<<k<<endl;
ll tmp=a-((k+2)*(k-1)/2);
//cout<<tmp<<endl;
if(tmp<=k-1)
{
ll p=0,q;
Ex_gcd(k-tmp+1,mod,p,q);
if(p<0) p=(p%mod+mod)%mod;
// cout<<p<<endl;
ans=(su[k+1]*p)%mod;
//cout<<su[k+1]<<endl;
}
else
{
ll p,q;
Ex_gcd(2,mod,p,q);
if(p<0) p=(p%mod+mod)%mod;
//cout<<p<<endl;
ans=(su[k]*p)%mod;
ans=(ans*(k+2))%mod;
}
printf("%lld\n",ans);
}
}
return 0;
}
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,a2a1,a2,
… (x= a1+a2a1+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≠ajai≠aj when
i≠j).
2.Make this multidimensional space size s as large as possible (s= a1∗a2a1∗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)
InputThe 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
OutputMaximum s you can get modulo 10^9+7. Note that we wants to be greatest product before modulo 10^9+7.
Sample Input
1 4
Sample Output
4
给出一个正整数,给这个数分解n段,但是各段不能有相同的数,求这些分法中各段乘积最大的答案;
这道题最开始想到分段,第一段是2,第二段是3,第三段是4,以此类推,这样是乘积最大的分段方法,
如果最后还剩一个数,就把这个分成n个1,分别加到每个头上,例如12,分成2,3,4后剩3,就变成3,4,5;
由于测试组数过多,分段要利用等差数列前n项求和公式求出分到第几项,不能每次算得时候都重新乘,取余,所以 要先用数组打表记录从2乘到N的乘积,然后中间空哪个数就在求这个数的逆元,在取余就好了;例如12,分成3,4,5;利用打表求得是2乘到5,由于多乘了2,所以求出2的逆元,再重新乘取余就好了;注意求得逆元是负数,要变成正数。
#include <iostream>
#include <cstdio>
#include <cmath>
using namespace std;
typedef long long ll;
#define mod 1000000007
ll f[50000];
void Ex_gcd(ll a, ll b, ll &x, ll &y)
{
if(b == 0)//递归出口
{
x = 1;
y = 0;
return;
}
ll x1, y1;
Ex_gcd(b, a%b, x1, y1);
x = y1;
y = x1-(a/b)*y1;
}
ll su[50000];
void dabiao()
{
su[1]=1;
su[2]=2;
for(int i=3;i<=45000;i++)
{
su[i]=(su[i-1]*i)%mod;
}
}
int main()
{
int T;
ll a;
scanf("%d",&T);
dabiao();
while(T--)
{
ll ans=1;
scanf("%lld",&a);
if(a<=4)
printf("%lld\n",a);
else
{
f[0]=1;
ll k=(ll)((-1+sqrt(9+8*a))/2);
// cout<<k<<endl;
ll tmp=a-((k+2)*(k-1)/2);
//cout<<tmp<<endl;
if(tmp<=k-1)
{
ll p=0,q;
Ex_gcd(k-tmp+1,mod,p,q);
if(p<0) p=(p%mod+mod)%mod;
// cout<<p<<endl;
ans=(su[k+1]*p)%mod;
//cout<<su[k+1]<<endl;
}
else
{
ll p,q;
Ex_gcd(2,mod,p,q);
if(p<0) p=(p%mod+mod)%mod;
//cout<<p<<endl;
ans=(su[k]*p)%mod;
ans=(ans*(k+2))%mod;
}
printf("%lld\n",ans);
}
}
return 0;
}
相关文章推荐
- 2016 ICPC大连赛区 hdu5976 Detachment
- HDOJ 5976 Detachment【2016大连现场】【数学好题】
- 第一次参加acm-icpc现场赛(2016大连赛区)总结
- HDU - 5976 Detachment(ICPC2016 大连现场,脑洞加逆元)
- Game of Taking Stones (hdu5973)——2016大连现场赛C题 威佐夫博弈
- HDOJ 5971 Wrestling Match【2016大连现场】【dfs二分染色】
- hdu 5972 Regular Number (bitset优化匹配) 2016大连现场赛
- 2016/41届ACM/ICPC大连现场赛D题&hdoj5974
- HDU 5976 && 2016ICPC大连 F: Detachment(找规律)
- 【2016-大连赛区网络赛-F】水题(Football Games,hdu 5873)
- 2016 ICPC 大连网络赛 HDU 5877 Weak Pair
- hdu 5876 - Sparse Graph(2016大连网络赛) bfs
- ZOJ 3542 2011大连现场赛D题(简单模拟)
- HDOJ 5916 Harmonic Value Description 【2016CCPC长春现场赛】数学+构造
- HDU 5532 2015ACM-ICPC长春赛区现场赛F题
- 2016 ACM/ICPC亚洲区青岛站现场赛(部分题解)
- 2016 ICPC大连赛区 hdu5974 Convex
- 【数论】【逆元】【贪心】HDU5976 Detachment
- HDU 5981 && 2016ICPC大连 K: Guess the number(推理)
- 【2016-大连赛区网络赛-I】补图最短路(Sparse Graph,hdu 5876)