HDU 5976 贪心+逆元

xiaoxiao2021-02-28  94

F - Detachment

  HDU - 5976 

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 a1,a2, … (x=  a1+a2 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 (  aiaj ai≠aj when i≠j).  2.Make this multidimensional space size s as large as possible (s=  a1a2 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

#include <iostream> #include <stdio.h> #include <stdlib.h> #include <string.h> #include <algorithm> #include <math.h> using namespace std; typedef long long LL; #define MOD 1000000007 #define maxn 50000 LL a[maxn]; LL sum[maxn]; LL mul[maxn]; void init() { a[0]=sum[0]=a[1]=sum[1]=0; a[2]=2;sum[2]=2; for(int i=3;i<maxn;i++) { a[i]=i; sum[i]=a[i]+sum[i-1]; } mul[0]=0;mul[1]=1; for(int i=2;i<maxn;i++) mul[i]=(mul[i-1]*i)%MOD; } LL quickpow(LL m,LL n) { LL b=1; while(n>0) { if(n&1)b=(b*m)%MOD; n=n>>1; m=(m*m)%MOD; }return b; } int main() { int t,n; scanf("%d",&t); init(); //for(int i=0;i<13;i++) //cout<<mul[i]<<endl; while(t--) { scanf("%d",&n); if(n<=4){cout<<n<<endl;continue;} int biao=upper_bound(sum,sum+maxn,n)-sum; int remain=n-sum[biao-1]; LL ans=0; if(remain==biao-1) ans=((mul[biao+1]*quickpow(2,MOD-2))%MOD*quickpow(biao,MOD-2))%MOD; else { int temp=biao-remain; ans=(mul[biao]*quickpow(temp,MOD-2))%MOD; } printf("%lld\n",ans); } return 0; }

转载请注明原文地址: https://www.6miu.com/read-96947.html

最新回复(0)