Stone
Time Limit: 3000/2000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others)
Total Submission(s): 46 Accepted Submission(s): 9
Problem Description
Given an array of integers {xi}. Each time you can apply one of the following operations to the array:
1. Choose an integer x from the array, replace it with x+1.
2. Add a new integer 1 to the array.
Define p as the product of all integers in the set. i.e. p=x1*x2*x3*...
What's the maximum possible value of p after exactly M operations?
1. Choose an integer x from the array, replace it with x+1.
2. Add a new integer 1 to the array.
Define p as the product of all integers in the set. i.e. p=x1*x2*x3*...
What's the maximum possible value of p after exactly M operations?
Input
First line is a integer T (T ≤ 100), the number of test cases.
The first line of each test case contains two integers N and M, the number of integers in the initial set, and the number of operations.
The second line is N integers xi initially in the set.
1 ≤ N ≤ 100000
0 ≤ M ≤ 10^18
-10000 ≤ xi ≤ 10000
The first line of each test case contains two integers N and M, the number of integers in the initial set, and the number of operations.
The second line is N integers xi initially in the set.
1 ≤ N ≤ 100000
0 ≤ M ≤ 10^18
-10000 ≤ xi ≤ 10000
Output
For each case, you should output “Case k: ” first, where k indicates the case number and counts from one. Then the maximum product mod 1000000007.
Sample Input
4
1 1
5
3 2
1 2 3
3 2
-1 2 3
3 1
-3 -3 -3
4
1 1
5
3 2
1 2 3
3 2
-1 2 3
3 1
-3 -3 -3
Sample Output
Case 1: 6
Case 2: 18
Case 3: 6
Case 4: -18
Case 2: 18
Case 3: 6
Case 4: -18
Source
Recommend
lcy
尽量的把数分成3。
偶数个负数不用处理,奇数个负数先把最大的一个加成0,所有0先加成正数在处理。
原来不足3的先平均的加到3。剩下的分成一个个3,如果剩下1把它加到一个3里,剩下2单独算一个数。
代码:
#include<cstdio>
#include<algorithm>
#define M 1000000007
using namespace std;
typedef __int64 ll;
ll mod(ll a,ll n,ll m)
{
ll s=1;
while(n)
{
if(n&1)
s=(s%m*a%m)%m;
a=(a%m)*(a%m)%m;
n>>=1;
}
return s;
}
int t,tt,n,m,a,b[100005],c[100005];
int main()
{
scanf("%d",&t);
while(t--)
{
int i,x,n1=0,n2=0;
ll s=1;
scanf("%d%d",&n,&m);
for(i=0;i<n;i++)
{
scanf("%d",&a);
if(a<0)//负数
c[n2++]=a;
else
b[n1++]=a;
}
if(n2&1)//奇数个负数
{
sort(c,c+n2);
b[n1++]=c[n2-1];
}
if(n1)
{
sort(b,b+n1);
if(b[0]<0)
{
x=min(m,-b[0]);
m-=x;
b[0]+=x;
}
for(i=0;m&&i<n1;i++)
if(!b[i])
{
b[i]++;
m--;
}
i=0;
while(m)
{
int f=1;
for(i=0;i<n1&&m;i++)
if(b[i]<3)
{
f=0;
b[i]++;
m--;
}
if(f)
{
if(m==1)
{
b[0]++;
m--;
}
break;
}
}
}
if(m>1)
{
if(m%3==0)
s=mod(3,m/3,M);
else if(m%3==2)
s=mod(3,m/3,M)*2;
else
s=mod(3,m/3-1,M)*4;
}
for(i=0;i<n1;i++)
s=(s*b[i])%M;
for(i=0;i<n2-(n2&1);i++)
s=(s*c[i])%M;
printf("Case %d: %I64d\n",++tt,s%M);
}
}