ZCMU 1887 数论(数字拆解)

题目链接

题意:

给一个数字x要求将x拆分成a1+a2+a3+...+an,n个数字,数字个数随意可以为1个。求某一拆分方法使得a1*a2*a3*...*an最大,输出这个最大值。

思路:

除了1,2,3,4以外任何数字将其从一个数字拆成两个适当的数字再相乘都能使得结果变大,那么一个数字要想结果尽量大必定要使得拆分出来的数字尽量多。数字尽量多就会使得每个数字尽量小并且数字就会连续,因此可以考虑使用数字的阶乘来表示此题的结果。

对于给定x,先进行二分等式t*(t+1)/2-1,得到一个数字t,t*(t+1)/2-1小于等于x且尽量接近x,此外还有一个剩余数字r=x-t*(t+1)/2+1。r是个余数我们要充分的去利用它使得结果变大,变大的方式当然是加给1,2,3,。。。,t中的某个数字。不难发现将r加给某个数字ti,最终结果增大2*3*4...*ti-1*ti+1*...*tt*r,因此要想结果尽量大选择的ti当然要尽量小,但也不能太小起码要使得ti+r>t,故最好的选择为t+1-r。

有一个小问题需要考虑当t+1-r等于1时,因为在选择数字时为使得结果尽量大1已经被我们舍弃了,所以这种情况应该特殊处理。

C++代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 100010;
const int mod  = 1e9+7;

LL a[maxn],b[maxn];

LL qpow( LL x , LL y )
{
    LL res = 1;
    while ( y )
    {
        if ( y&1 ) res = res*x%mod;
        y >>= 1; x = x*x%mod;
    }
    return res;
}

int main()
{
    a[0] = 1;
    for ( int i=1 ; i<maxn ; i++ )
    {
        a[i] = a[i-1]*i%mod;
        b[i] = qpow( i , mod-2 );
    }
    int T; scanf ( "%d" , &T );
    while ( T-- )
    {
        LL x; scanf ( "%lld" , &x );
        if ( x<=4 )
        {
            printf ( "%lld\n" , x );
        }
        else
        {
            LL l=1,r=100000,mid;
            while( l<=r )
            {
                mid = (l+r)>>1;
                if( mid*(mid+1)/2-(LL)1>x )
                    r = mid-1;
                else
                    l = mid+1;
            }
            LL t = x-( r*(r+1)/2-(LL)1 );
            LL c = r+1-t;
            if ( c>1 )
                printf ( "%lld\n" , a[r+1]*b[c]%mod );
            else
                printf ( "%lld\n" , a[r+2]*b[2]%mod*b[r+1]%mod );
        }
    }
    return 0;
}


猜你喜欢

转载自blog.csdn.net/game_acm/article/details/80872142