hdu 5976 a1*a2*a3...

来源:互联网 发布:高清网络球型摄像机 编辑:程序博客网 时间:2024/04/28 03:59

传送门

题意:

给你一个x=a1+a2+a3+...+an

让你求一个s=a1*a2*a3*...*an最大。(ai!=aj 当且仅当i!=j)

ans%1e9+7


思路:

经过分析得到要使s最大,那么a1*a2*a3*..一定要是连续的一串数的乘积,比如9就是 2*3*4  14就是2*3*4*5 如果这个数刚好在

9-14中间怎么办呢,经过一阵观察,可发现x-9剩下的可以在2*3*4的2 3 4里动手脚,比如10 我还剩了1,那么我是选择给4 +1还是

选择给2 +1呢,显然我给4 +1得到的结果是+2*3   +6 ,给2 +1的话会导致3重复,那么按照题意,就只能给4 +1 那么11怎么办

在10的基础上 2 3 5 那么我可以给3 +1 依次这样,那么 我如果是13怎么办呢

在 12的基础上 3 4 5 我再+1只能给5 +1 得到 3 4 6 ,那么14的话就是 2 3 4 5来的答案更大。

于是就得到了以下代码:

//china no.1#pragma comment(linker, "/STACK:1024000000,1024000000")#include <vector>#include <iostream>#include <string>#include <map>#include <stack>#include <cstring>#include <queue>#include <list>#include <stdio.h>#include <set>#include <algorithm>#include <cstdlib>#include <cmath>#include <iomanip>#include <cctype>#include <sstream>#include <functional>#include <stdlib.h>#include <time.h>#include <bitset>using namespace std;#define pi acos(-1)#define s_1(x) scanf("%d",&x)#define s_2(x,y) scanf("%d%d",&x,&y)#define s_3(x,y,z) scanf("%d%d%d",&x,&y,&z)#define PI acos(-1)#define endl '\n'#define srand() srand(time(0));#define me(x,y) memset(x,y,sizeof(x));#define foreach(it,a) for(__typeof((a).begin()) it=(a).begin();it!=(a).end();it++)#define close() ios::sync_with_stdio(0); cin.tie(0);#define FOR(x,n,i) for(int i=x;i<=n;i++)#define FOr(x,n,i) for(int i=x;i<n;i++)#define fOR(n,x,i) for(int i=n;i>=x;i--)#define fOr(n,x,i) for(int i=n;i>x;i--)#define W while#define sgn(x) ((x) < 0 ? -1 : (x) > 0)#define bug printf("***********\n");#define db double#define ll long long#define mp make_pair#define pb push_backtypedef pair<long long int,long long int> ii;typedef long long LL;const int INF=0x3f3f3f3f;const LL LINF=0x3f3f3f3f3f3f3f3fLL;const int dx[]={-1,0,1,0,1,-1,-1,1};const int dy[]={0,1,0,-1,-1,1,-1,1};const int maxn=1e5+10;const int maxx=600005;const double EPS=1e-8;const double eps=1e-8;const int mod=1e9+7;template<class T>inline T min(T a,T b,T c) { return min(min(a,b),c);}template<class T>inline T max(T a,T b,T c) { return max(max(a,b),c);}template<class T>inline T min(T a,T b,T c,T d) { return min(min(a,b),min(c,d));}template<class T>inline T max(T a,T b,T c,T d) { return max(max(a,b),max(c,d));}template <class T>inline bool scan_d(T &ret){char c;int sgn;if (c = getchar(), c == EOF){return 0;}while (c != '-' && (c < '0' || c > '9')){c = getchar();}sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');while (c = getchar(), c >= '0' && c <= '9'){ret = ret * 10 + (c - '0');}ret *= sgn;return 1;}inline bool scan_lf(double &num){char in;double Dec=0.1;bool IsN=false,IsD=false;in=getchar();if(in==EOF) return false;while(in!='-'&&in!='.'&&(in<'0'||in>'9'))in=getchar();if(in=='-'){IsN=true;num=0;}else if(in=='.'){IsD=true;num=0;}else num=in-'0';if(!IsD){while(in=getchar(),in>='0'&&in<='9'){num*=10;num+=in-'0';}}if(in!='.'){if(IsN) num=-num;return true;}else{while(in=getchar(),in>='0'&&in<='9'){num+=Dec*(in-'0');Dec*=0.1;}}if(IsN) num=-num;return true;}void Out(LL a){if(a < 0) { putchar('-'); a = -a; }if(a >= 10) Out(a / 10);putchar(a % 10 + '0');}void print(LL a){ Out(a),puts("");}//freopen( "in.txt" , "r" , stdin );//freopen( "data.txt" , "w" , stdout );//cerr << "run time is " << clock() << endl;LL fac[maxn];LL a[maxn];void ex_gcd(LL a, LL b, LL &x, LL &y, LL &d){    if (!b) {d = a, x = 1, y = 0;}    else{        ex_gcd(b, a % b, y, x, d);        y -= x * (a / b);    }}LL inv2(LL t, LL p){//如果不存在,返回-1    LL d, x, y;    ex_gcd(t, p, x, y, d);    return d == 1 ? (x % p + p) % p : -1;}void init(){    fac[0]=1;    for(int i=1;i<=maxn;i++)    {        fac[i]=fac[i-1]*i;        fac[i]%=mod;    }    for(int i=1;i<=maxn;i++)    {        a[i]=a[i-1]+i+1;    }}int t,x;int main(){    //freopen( "in.txt" , "r" , stdin );    //freopen( "1.txt" , "w" , stdout );    init();    s_1(t);    W(t--)    {        s_1(x);        if(x==1)        {            puts("1");            continue;        }        int pos=lower_bound(a+1,a+maxn+1,x)-a;        if(a[pos]==x)            print(fac[pos+1]);        else        {            x-=a[pos-1];            LL ans=1;            if(x<=pos-1)            {                ans*=fac[pos-x];                ans=ans*fac[pos+1]%mod*inv2(fac[pos+1-x],mod);            }            else            {                ans*=fac[pos+2];                ans=ans*fac[pos]%mod*inv2(fac[pos+1]*fac[2]%mod,mod);             }            ans=(ans+mod)%mod;            print(ans);        }    }}


阅读全文
0 0
原创粉丝点击