1. 选择两个整数
2. 选择两个整数
3. 将
她希望最多进行
一行两个整数,表示数组的长度和操作的次数。
一行个整数
,表示数组的元素。
输出一个整数,表示最后数组中的元素的总和的最大值,由于答案可能很大,你只需要输出答案对取模的结果。
5 2 1 2 3 4 5
65
第一次操作后,数组变为 [1, 2, 12, 1, 5]第二次操作,数组变为 [1, 2, 60, 1, 1]
import java.util.Scanner; import java.util.Arrays; import java.math.BigInteger; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { public static void main(String[] args) { Scanner in = new Scanner(System.in); int n = in.nextInt(); int k = in.nextInt(); int[] a = new int[n]; for(int i = 0; i < n; i++){ a[i] = in.nextInt(); } Arrays.sort(a); // 从后向前遍历 BigInteger boss = new BigInteger(String.valueOf(a[n - 1])); for(int i = n - 2; i > -1 && k > 0; i--){ BigInteger employee = new BigInteger(String.valueOf(a[i])); boss = boss.multiply(employee); a[i] = 1; k--; } for(int i = 0; i < n - 1; i++){ BigInteger employee = new BigInteger(String.valueOf(a[i])); boss = boss.add(employee); } boss = boss.mod(new BigInteger(String.valueOf("1000000007"))); System.out.println(boss); } }
import java.util.*; import java.io.*; import java.math.*; // 注意类名必须为 Main, 不要有任何 package xxx 信息 public class Main { private static int mod = 1000000007; private static class MaxComparator implements Comparator<BigDecimal>{ public int compare(BigDecimal o1, BigDecimal o2){ return o2.compareTo(o1); } } public static void main(String[] args) throws Exception { BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String[] str = bf.readLine().split(" "); int n = Integer.parseInt(str[0]); int k = Integer.parseInt(str[1]); PriorityQueue<BigDecimal> queue = new PriorityQueue<>(new MaxComparator()); str = bf.readLine().split(" "); for(int i=0;i<n;i++){ queue.offer(new BigDecimal(Long.parseLong(str[i]))); } for(int i=0;i<k;i++){ BigDecimal first = queue.poll(); BigDecimal second = queue.poll(); //System.out.println("first="+first+",second="+second); BigDecimal mul = first.multiply(second); queue.offer(mul); queue.offer(new BigDecimal(1)); } BigDecimal res = new BigDecimal(0); while(!queue.isEmpty()){ BigDecimal curr = queue.poll(); res = res.add(curr); } System.out.println(res.divideAndRemainder(new BigDecimal(mod))[1]); } }
#include <iostream> #include <queue> #include <climits> using namespace std; const long long MOD = 1e9+7; int main() { int n,k; cin>>n>>k; long long x; priority_queue<long long> q; for(int i=0;i<n;i++){ cin>>x; q.push(x); } while(k--){ long long a1=q.top(); q.pop(); long long a2=q.top(); q.pop(); long long tmp=(a1*a2)%MOD; q.push(tmp); q.push(1); } //cout<<q.size()<<endl; long long sum=0; while(!q.empty()){ long long now=q.top(); q.pop(); sum=(sum+now)%MOD; } printf("%lld\n",sum); } // 64 位输出请用 printf(\"%lld\")