Codeforces Round #653 (Div. 3) AB 题解

本文介绍了两种算法问题的解决方法:一种是求特定条件下最接近但不超过目标值的问题,通过简单的数学运算实现;另一种是通过质因数分解解决数的转换问题,确保数仅包含2和3的质因数,并计算最小操作次数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

A. Required Remainder

解法很简单,是个人应该都知道

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

int main()
{
	int n,x,y;
	int t;
	cin>>t;
	while(t--)
	{
		cin>>x>>y>>n;
		int div=(n-y)/x;
		if(div*x+y<=n) cout<<div*x+y<<endl;
		 else cout<<div*x-y<<endl;
	}
	return 0;
}

B. Multiply by 2, divide by 6

翻译一下, 就是一个数,你可以将它乘2或者除以6,反正最后要让它变成1,输出最小的操作次数,如果不行输出-1

解法:首先分解质因数,这个数必须只包括2和3,且2的次数必须不大于3的次数。原因很简单,你不能乘3只能乘2,如果2的次数较大的话,怎么样能正好配对除以6呢?

#include <bits/stdc++.h>
using namespace std;
// 18=2*3^2  cnt3-cnt2+cnt3
int main()
{
	int n,t;
	ios::sync_with_stdio(false);
	
	cin>>t;
	while(t--)
	{
		cin>>n;
		int cnt2=0,cnt3=0;
		while(n%2==0)
		{
			cnt2++;
			n/=2;
		}
		while(n%3==0)
		{
			cnt3++;
			n/=3;
		}
		if(n!=1||cnt2>cnt3)
		 cout<<"-1"<<endl;
		else cout<<2*cnt3-cnt2<<endl;
	}
	
	return 0;
}

 

写作不易,麻烦动个手点个赞,行不?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值