标签:des style color java os io strong for
4 1 1 5 3 2 1 2 3 3 2 -1 2 3 3 1 -3 -3 -3
Case 1: 6 Case 2: 18 Case 3: 6 Case 4: -18
#include <iostream> #include <cstdio> #include <cstring> #include <string> #include <algorithm> #include <vector> #include <queue> #include <set> using namespace std; typedef long long ll; const int maxn = 100000+10; const int MOD = 1000000007; const int inf = 1e9; int num[maxn]; int n; ll m; int neg_cnt; ll ans; ll pow_mod(ll n,ll m){ ll ret = 1,tmp = n; while(m>0){ if(m&1) ret = (ret*tmp)%MOD; tmp = (tmp*tmp)%MOD; m>>=1; } return ret; } void init(){ ans = 1; neg_cnt = 0; } void input(){ scanf("%d%I64d",&n,&m); for(int i = 0; i < n; i++){ scanf("%d",&num[i]); if(num[i]<0) neg_cnt++; } sort(num,num+n); } void output(){ for(int i = 0; i < n; i++){ ans = (ans*num[i])%MOD; } printf("%I64d\n",ans); } void compute(){ for(int i = 0; i < n && m>0; i++){ if(num[i]==0){ ++num[i]; --m; } } for(int i = 0; i < n && m >0; i++){ if(num[i]==1){ ++num[i]; --m; } } for(int i = 0; i < n && m > 0; i++){ if(num[i]==2){ ++num[i]; --m; } } ll cnt_3; if(m >= 2){ if(m%3==0){ cnt_3 = m/3; ans = pow_mod((ll)3,cnt_3); }else{ int remain = m%3; if(remain==1){ cnt_3 = m/3-1; ans = pow_mod((ll)3,cnt_3); ans = (ans*4)%MOD; }else{ cnt_3 = m/3; ans = pow_mod((ll)3,cnt_3); ans = (ans*2)%MOD; } } } if(m==1){ int absMin = inf,minIdx = 0; for(int i = 0; i < n; i++){ if(num[i] != -1){ if(absMin > abs(num[i])){ absMin = abs(num[i]); minIdx = i; } } } num[minIdx]++; } output(); } void solve(){ if(neg_cnt&1){ int k = 0; while(k<n&&num[k]<0) k++; --k; if(m+num[k]<=0){ num[k] += m; output(); }else{ m += num[k]; num[k] = 0; compute(); } }else{ compute(); } } int main(){ int ncase,T=1; cin >> ncase; while(ncase--){ init(); input(); printf("Case %d: ",T++); solve(); } return 0; }
HDU4038-Stone(思维题),布布扣,bubuko.com
标签:des style color java os io strong for
原文地址:http://blog.csdn.net/mowayao/article/details/38663283