histcat

histcat

小蔥買糖

題目#

小葱同學喜歡吃糖,小葱買了很多糖。但是小葱買的糖太多了,小葱記不清具體數字了,小葱只記得自己的總糖數是自己記錄在筆記本上的 $N$ 個數 $a_1,a_2,\cdots,a_N$ 的最小公倍數。請你幫幫小葱,算算小葱買了多少糖。

對於 $100%$ 的數據,$1\leq N\leq 10^3,1\leq a_i\leq 10^9$。

題解#

提煉一下題意 —— 求 $n$ 個數的最小公倍數

直接分解質因數,然後求出每個質因子的最大次幂是多少,乘起來即可

(第一次在考場上 A 題,不過是道签到題 QAQ)

代碼#

#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;
}
載入中......
此文章數據所有權由區塊鏈加密技術和智能合約保障僅歸創作者所有。