(逆元,线段树区间求积)hdu 5976&2016ICPC大连F - Detachment

Detachment
 

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)
 

 

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
 

 

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
1
4
Sample Output
4
 
 

题目大意: 多组数据,给定一个x,将x拆成多个不同的数(也可以一个),求这些数的最大乘积

思路:要最大的乘积,肯定就要尽量将x拆成尽量多的的数,所以就是从2开始的连续的数,

(2+k)*(k-1)/2<=x

这样x减去连续的数后可能还有多,就需要将剩下的都加到前面的连续的数中,即res=2*3*...(x-1)*(x+1)*...k*(k+1),很明显每个数都加1会让乘积更大.

如果剩下的等于k,那k要加2,前面的数全部加1,即3一直乘到k,最后再乘一个k+2。

我是用了线段树区间求积的,后来发现也可以用阶乘加逆元的方法

 

ps.vj上的好像数据有问题,我人都wa傻了,把别人的ac代码交上去也是wa,但在杭电上就能过

#include <bits/stdc++.h>
#define LL long long
#define MOD 1000000007
using namespace std;
const int maxn = 50005;
int T;
int n,x=1;
LL mu[maxn << 2];
void build(int l, int r, int rt) {
	if(l == r) {
		mu[rt]=x++;
		return;
	}
	int mid = (l + r) >> 1;
	build(l, mid, rt << 1);
	build(mid + 1, r, rt << 1 | 1);
	mu[rt] = (mu[rt << 1] * mu[rt << 1 | 1]) % MOD;
}
LL query(int L, int R, int l, int r, int rt) {
	if(L <= l && r <= R) {
		return mu[rt];
	}
	LL ret = 1;
	int mid = (l + r) >> 1;
	if(mid >= L) ret = (ret * query(L, R, l, mid, rt << 1)) % MOD;
	if(mid + 1 <= R) ret = (ret * query(L, R, mid + 1, r, rt << 1 | 1)) % MOD;
	return ret;
}
/*void update(int p, int v, int l, int r, int rt) {
	if(l == r) {
		mu[rt] = v;
		return;
	}
	int mid = (l + r) >> 1;
	if(p <= mid) update(p, v, l, mid, rt << 1);
	else update(p, v, mid + 1, r, rt << 1 | 1);
	mu[rt] = (mu[rt << 1] * mu[rt << 1 | 1]) % MOD;
}*/
int main() 
{
	LL s;
	int N=50000;
	double k;
	scanf("%d", &T);
	build(1, N, 1);
	while(T--) 
	{
		scanf("%d", &n);
		if(n==1)
		{
			printf("1\n");
			continue;
		}
		k=n;
		k=sqrt(2*k+2.25)-0.5;
		s=k;
		n-=(s+2)*(s-2+1)/2;
		//cout<<s<<" s"<<endl;
		if(n)
		{
			if(n==s)
				s=query(3,s,1,N,1)*(s+2)%MOD;
			else
				s=query(2,s-n,1,N,1)*query(s-n+2,s+1,1,N,1)%MOD;
		}
		else
			s=query(2,s,1,N,1);
		printf("%lld\n",s);
	}
	return 0;
}

推导过程:

res=2*3*...(x-1)*(x+1)*...k*(k+1)

x*x^{-1}\equiv 1(mod 1e9+7)

res=pos!/x

\Rightarrow res*x^{-1}\equiv res(mod 1e9+7)

阶乘乘以逆元的代码:

#include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
using namespace std;
ll a[100055];
ll n,ans;
bool ok(ll mid)
{
    return (mid+1)*mid/2-1>=n;
}
ll power(ll aa,ll  n)   //a的n次方mod
{
    ll ans=1;
    aa=aa%mod;
    while (n)
    {
        if(n&1)
        ans=(ans*aa)%mod;
        n>>=1;
        aa=(aa*aa)%mod;
    }
    return ans;
}
int main()
{
    ll t,i,j,l,r,pos,mid;
    ll tt;
    a[0]=1;
    for(i=1;i<=100005;i++)
        a[i]=(a[i-1]*i)%mod;
    scanf("%lld",&t);
    while(t--)
    {
        scanf("%lld",&n);
        l=1;
        r=n;
        if(n<=4)
        {
            printf("%lld\n",n);
            continue;
        }
        while(l<=r)
        {
            mid=(l+r)>>1;
            if(ok(mid))
            {
                r=mid-1;
                ans=mid;
            }
            else
                l=mid+1;
        }
        pos=ans;
        ans=ans*(ans+1)/2-1-n;
        if(ans==0)
        {
            printf("%lld\n",a[pos]);
            continue;
        }
        if(ans==1)
        {
            tt=a[pos-1]*(pos+1)%mod;
            tt=tt*power(2,mod-2)%mod;
            printf("%lld\n",tt);
            continue;
        }
        else
        {
            tt=a[pos];
            tt=tt*power(ans,mod-2)%mod;
            printf("%lld\n",tt);
        }
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值