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;
}
読み込み中...
文章は、創作者によって署名され、ブロックチェーンに安全に保存されています。