B. GCD Problem(互质),C. Paprika and Permutation(模数)

B. GCD Problem

题意:

给定正数 n ( 10 ≤ n ≤ 1 0 9 ) n(10≤n≤10^9) n(10n109),找到三个互不相同的数 a,b,c,满足:

  • a+b+c = n;
  • gcd(a, b) = c;
分析:

对于一个较小的数和很大的数,很容易互质。
所以构造 c=1,从小到大枚举a,b=n-a-1,找到第一个互质的数退出。

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

#define Ios ios::sync_with_stdio(false),cin.tie(0)
#define mem(a,b) memset(a,b,sizeof a)
#define int long long
#define PII pair<int,int>
#define pb push_back
#define fi first
#define se second
#define endl '\n'

map<int,int> mp;

const int N = 200010, mod = 1e9+7;
int T, n, m, k;
int a[N];
int prim[N], cnt, f[N];

void Prim()
{
	for(int i=2;i<=n;i++)
	{
		if(!f[i]) prim[++cnt]=i;
		for(int j=1;prim[j]<=n/i;j++)
		{
			f[prim[j]*i] = 1;
			if(i%prim[j]==0) break;
		}
	}
}

signed main(){
//	Ios;
	cin>>T;
	while(T--)
	{
		cin>>n;
		
		for(int i=2;i<=n;i++)
		{
			if(i!=n-i-1 && __gcd(i, n-i-1)==1){
				cout << i <<" "<<n-i-1 <<" "<<1<<endl;
				break;
			}
		}
	}
	
	return 0;
}

C. Paprika and Permutation

题意:

给定 n 个数,现在要将其变成一个全排列,最少用多少次下列操作?

  • 任意选一个位置 i 和 一个数 x,令 a[i] %= x
分析:

一定要想到一个性质:
对于两个数 x 和 y,如果 y ≤ 2*x,那么 y 无法取模得到 x。
所以越大的数越好,能够取模得到的数越多。

为了操作次数最少,首次在全排列中出现的数不需要改变。
将需要改变的数存下来,从小到大排序,和需要变化成的数一一对应。

  • 如果发现一个位置小于等于其变化数的2倍,那么这个变化数就无法得到,全排列无法构成,输出-1。
  • 否则全部能够变化,输出需要改变的数的个数。
Code:
#include<bits/stdc++.h>
using namespace std;

#define Ios ios::sync_with_stdio(false),cin.tie(0)
#define mem(a,b) memset(a,b,sizeof a)
#define int long long
#define PII pair<int,int>
#define pb push_back
#define fi first
#define se second
#define endl '\n'

map<int,int> mp;

const int N = 200010, mod = 1e9+7;
int T, n, m, k;
int a[N], f[N], b[N], c[N];

signed main(){
	Ios;
	cin>>T;
	while(T--)
	{
		cin>>n;
		for(int i=1;i<=n;i++) cin>>a[i], f[i]=0;
		
		int cnt1=0, cnt2=0;
		for(int i=1;i<=n;i++)
		{
			if(a[i]<=n && !f[a[i]]) f[a[i]]=1;
			else b[++cnt1]=a[i];
		}
		for(int i=1;i<=n;i++)
		{
			if(!f[i]) c[++cnt2]=i;
		}
		sort(b+1, b+cnt1+1);
		
		int flag=0;
		for(int i=1;i<=cnt1;i++)
		{
			if(b[i] <= c[i]*2) flag=1;
		}
		if(flag) cout<<-1<<endl;
		else cout<<cnt1<<endl;
	}
	
	return 0;
}

今天vp这两题都没写出来。。
第一题没想到这样构造,一直想着将n整除一个数。。
第二题没想到性质。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值