Codeforces Round 900 (Div. 3)F. Vasilije Loves Number Theory(数学)

原题链接:F. Vasilije Loves Number Theory
题目大意:令 d(x) 表示 x 的正因子数量,给定 n,q。现有两种操作:
操作1:给定 x,令 n替换为n⋅x。同时询问是否存在一个正整数 a 满足 gcd(a,n)=1 且
d(n⋅a)=n。若存在a,输出"YES",否则输出”NO“
操作2:将 n 还原为最初的值。
数据范围:1≤t≤100,1≤n≤106, 1≤q≤1000,1≤k≤2 ,1≤x≤106,对于给定的输入,保证d(n)在任何时候都不超过109。 保证所有测试案例的q总和不超过103

解题思路
前置知识:一个数的正因子数量等于该数的每个质因子出现次数+1的乘积
例如:对72进行质因数的分解,得到2,2,2,3,3。 2出现了3次,3出现了2次,则36的正因子数量为(3+1)*(2+1)=12个
分析题目,由于gcd(a,n)=1,推出a,n不可能有相同的质因子,因此d(n⋅a)=d(n)⋅d(a),而a只需要取整数个对n质因数分解得不到的质因子即可达到,所以只需要考虑n是否是d(n)的整数倍即可
首先开两个map,分别为mp和now,mp用来维护原始n的质因子个数,now用来存当前n⋅x的质因子个数,这样就不用每次都算一遍n⋅x的质因子,直接对x进行质因数分解即可
然后对n进行质因数分解

	int p = n;
	for (int i = 2; i <= sqrt(p); i++)
	{
		while (p % i == 0)p /= i, mp[i]++;
	}
	if (p > 1)mp[p]++;//特判一下分解后得到质数的情况

进行操作1后在次基础上对x进行同样分解操作即可
分解后,用上述方法计算正因子个数

for (auto [x, y] : now)p *= (y + 1);

然后用快速幂可还原当前的n⋅x

for (auto [x,y] : now) sum *= ksm(x, y, p),sum%=p;

然后判断当前sum是否是正因子的整数倍即可

AC代码

#include<iostream>
#include<map>
#include<algorithm>
#include<vector>
#include<queue>
#include<set>
#include<math.h>
using namespace std;
#define int long long
map<int, int> now, mp;
int ksm(int a, int b, int p)
{
	int ans = 1;
	while (b)
	{
		if (b & 1)ans *= a, ans %= p;
		a *= a;
		a %= p;
		b >>= 1;
	}
	return ans % p;
}
void solve()
{
	int n, q;
	cin >> n >> q;
	now.clear();
	mp.clear();
	int p = n;
	//分解质因数
	for (int i = 2; i <= sqrt(p); i++)
	{
		while (p % i == 0)p /= i, mp[i]++;
	}
	if (p > 1)mp[p]++;
	p = n; now = mp;
	while (q--)
	{
		int op;
		cin >> op;
		if (op == 1)
		{
			int x;
			cin >> x;
			for (int i = 2; i <= sqrt(x); i++)
			{
				while (x % i == 0)x /= i, now[i]++;
			}
			if (x > 1)now[x]++;
			int p = 1, sum = 1;
			for (auto [x, y] : now)p *= (y + 1);//计算正因子个数
			for (auto [x,y] : now) sum *= ksm(x, y, p),sum%=p;//计算当前n⋅x
			if (sum%p)cout << "NO" << endl;
			else cout << "YES" << endl;
		}
		else
		{
			now = mp;//操作二直接把原来分解n得到的mp赋给now
		}
	}
	cout << endl;
}
signed main() {
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);
	int t = 1;
	cin >> t;
	while (t--)solve();
	return 0; 
}
  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值