histcat

histcat

Little Green Onion Buys Candy

Problem#

Little Onion likes to eat candy, and he bought a lot of it. However, Little Onion bought too much candy, and he can't remember the exact number; he only remembers that the total amount of candy he has is the least common multiple of the $N$ numbers $a_1,a_2,\cdots,a_N$ recorded in his notebook. Please help Little Onion calculate how much candy he bought.

For $100%$ of the data, $1\leq N\leq 10^3,1\leq a_i\leq 10^9$.

Solution#

To summarize the problem—find the least common multiple of $n$ numbers.

Directly factor the numbers into primes, then determine the maximum power of each prime factor and multiply them together.

(This is the first time I encountered an A problem in an exam, but it's just a sign-in question QAQ)

Code#

#include<bits/stdc++.h>
#define int long long
using namespace std;

map <long long, long long>s;

const int N = 1e3 + 10;
int a[N], n;
const int mod = 1e9 + 7;
int read()
{
	int f = 1, x = 0;
	char ch = getchar();
	while(ch < '0' || ch > '9')
	{
		if(ch == '-') f = -1;
		ch = getchar();
	}

	while(ch >= '0' && ch <= '9')
	{
		x = 10 * x + ch - '0';
		ch = getchar();
	}
	return f * x;
}

int fast_power(int a, int p)
{
	int ans = 1;
	while(p)
	{
		if(p & 1) ans = ans * a % mod;
		a = a * a % mod;
		p >>= 1;
	}
	return ans % mod;
}

signed main()
{
	freopen("buy.in", "r", stdin);
	freopen("buy.out", "w", stdout);
	int ans = 1;
	n = read();
	for(int i = 1;i <= n;i++)
		a[i] = read();
	int temp;
	for(int i = 1;i <= n;i++)
	{
		temp = a[i];
		for(int j = 2;j <= temp / j;j++)
		{
			int c = 0;
			while(temp % j == 0)
			{
//				cout << "qwq" << a[i] << " " << j << endl;
				c++;
				temp /= j;
				if(c > s[j])
				{
					ans = (ans * j) % mod;
					s[j]++;
				}
			}
//			if(c > s[j])
//			{
////				cout << "ans *=" << c - s[j] << "*"<< j <<endl;
//				ans = (ans * (c - s[j]) * j) % mod;
//				s[j] = c;
//			}
		}
		if(temp > 1)
		{
//			cout << "qwq" << a[i] << " " << temp << endl;
			if(s[temp] == 0)
			{
				s[temp] = 1;
//				cout << "ans *= " << temp <<endl;
				ans = ans * temp % mod;
			}
		}
	}
	printf("%lld", ans % mod);
	return 0;
}
Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.