QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#457446#8723. 乘二ZipperTL 1ms5756kbC++142.8kb2024-06-29 12:24:002024-06-29 12:24:05

Judging History

你现在查看的是最新测评结果

  • [2024-06-29 12:24:05]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:5756kb
  • [2024-06-29 12:24:00]
  • 提交

answer

#include<iostream>
#include<math.h>
#include<algorithm>
using namespace std;
#define int long long

const int N = 2e5 + 5, p = 1e9 + 7;
pair<int, int> nums[N], heap[N];
int ans, arr[N], dLog[N];
long double Logarr[N];

bool smaller(pair<int, int> a, pair<int, int> b)
{
	double x = log2(nums[a.first].first) + nums[a.first].second;
	double y = log2(nums[b.first].first) + nums[b.first].second;
	if (x < y) return true; else return false;
}

void heapInsert(int idx)
{
	while (idx != 0 && smaller(heap[idx], heap[idx / 2]))
	{
		pair<int, int> tmp = heap[idx]; heap[idx] = heap[idx / 2];
		heap[idx / 2] = tmp; idx = idx / 2;
	}
}

void heapify(int idx, int size)
{
	int l = 2 * idx;
	while (l <= size)
	{
		int mini = l + 1 <= size && smaller
			(heap[l + 1], heap[l]) ? l + 1 : l;
		if (smaller(heap[idx], heap[mini])) break;
		pair<int, int> tmp = heap[mini]; heap[mini] = heap[idx];
		heap[idx] = tmp; idx = mini; l = 2 * idx;
	}
}

int power(int a, int b)
{
	int tmp = 2, ans = 1;
	while (b > 0)
	{
		if (b % 2) ans = (ans * tmp) % p;
		tmp = (tmp * tmp) % p; b /= 2;
	}
	return (a * ans) % p;
}

//小根堆维护最小值
//signed main()
//{
//	int n, k; cin >> n >> k;
//	for (int i = 1; i <= n; i++)
//	{
//		cin >> nums[i].first;
//		nums[i].second = 0;
//		heap[i] = { i, nums[i].first };
//		heapInsert(i);
//	}
//
//	for (int i = 0; i < k; i++)
//	{
//		//时间复杂度O(k*logn),k比较大时无法在时限内得到结果
//		heap[1].second = (heap[1].second * 2) % p;
//		nums[heap[1].first].second++; heapify(1, n);
//	}
//
//	for (int i = 1; i <= n; i++)
//	{
//		ans += heap[i].second; ans %= p;
//	}
//	cout << ans;
//
//	return 0;
//}

signed main()
{
	int n, k; cin >> n >> k;
	for (int i = 1; i <= n; i++)
	{
		cin >> arr[i];
	}
	sort(arr + 1, arr + n + 1);
	for (int i = 1; i <= n; i++)
	{
		Logarr[i] = log2(arr[i]);
	}

	//dLog[i]表示把前i个数的log2值都变成
	//log2(arr[i])所需要的操作次数
	for (int i = 1; i <= n; i++)
	{
		for (int j = 1; j < i; j++)
		{
			dLog[i] += floor(Logarr[i] - Logarr[j]);
		}
	}

	int stop = n, times = 0;
	for (int i = 1; i <= n; i++)
	{
		if (k < dLog[i])
		{
			stop = i - 1; break;
		}
	}
	times = k - dLog[stop];

	//先把前stop - 1个数都不断乘而直至它们的log2值都等于log2(arr[stop])
	//再把剩下的操作次数(times)***队前优先平均***分给前stop个数
	//由于可以使用快速幂,所以会比小根堆的实现更快
	for (int i = 1; i < stop; i++)
	{
		arr[i] = power(arr[i], floor(Logarr[stop] - Logarr[i]));
	}
	int t = times / stop, s = times - t * stop;
	for (int i = 1; i <= stop; i++)
	{
		if (i <= s) arr[i] = power(arr[i], t + 1);
		else arr[i] = power(arr[i], t);
	}

	for (int i = 1; i <= n; i++)
	{
		ans += arr[i]; ans %= p;
	}
	cout << ans;

	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 5756kb

input:

3 3
7 2 1

output:

15

result:

ok 1 number(s): "15"

Test #2:

score: -100
Time Limit Exceeded

input:

200000 1605067
366760624 67854 93901 693975 27016 1046 10808 6533158 54778 500941023 77236442 32173 10431454 2 9726 1553148 89282 411182309 494073 131299543 249904771 7906930 353 9909 3632698 29156 1917186 303 737 1189004 22 1983 263 711 4106258 2070 36704 12524642 5192 123 2061 22887 66 380 1 10153...

output:


result: