CF Round #528 Div. 2

本文介绍了一种解决特定数学问题的算法,该问题要求找到一个最小的正整数x,使得x除以k的余数乘以x减去余数再除以k等于给定的整数n。通过寻找最接近sqrt(n*k)的n的因子,可以有效地解决这个问题。

A

B - Div Times Mod
题意:给出n,k 求 一个 使等式成立的最小正整数x
思路 :  令(x%k)   = a 则 原式变为 (x-a)/k * a = n   这里 a∈ (1,k)并且为n的因子
解出x来得到 x= n*k / a + a   求最小值即可。即 在a的定义域内找一个最靠近   sqrt (n*k)就是最小值
 

#include<bits/stdc++.h>
using namespace std;
int n,k,x,a,b,c,ans,ttt=0x3f3f3f3f,ad;
int main()
{
    scanf("%d%d",&n,&k);
    c=sqrt(k*n);
    for(int i=1; i<k; i++)
    {
        if(n%i==0)
        {
            if(abs(i-c)<ttt)
            {
                ttt=abs(i-c);
                ad=i;
            }
        }
    }
    ans=n*k/ad+ad;
    printf("%d\n",ans);
    return 0;
}

C 模拟

#include<bits/stdc++.h>
using namespace std;
struct unt {
	int x,y;
} coo[5];
bool cmp(unt a,unt b) {
	if(a.x == b.x) return a.y < b.y;
	return a.x < b.x;
}
int main() {
	int n = 3;
	int x,y,minx=1000,miny=1000,maxx=0,maxy=0;
	for(int i=0; i<3; i++) {
		cin>>x>>y;
		maxx = max(maxx,x);
		minx = min(minx,x);
		maxy = max(maxy,y);
		miny = min(miny,y);
		coo[i].x = x;
		coo[i].y = y;
	}
	sort(coo,coo+n,cmp);
	int ans = maxx - minx + (maxy - miny) + 1;
	cout<<ans<<endl;

	int tem = min(coo[0].y,min(coo[1].y,coo[2].y));
	while(tem<=maxy) {
		cout<<coo[1].x<<' '<<tem<<endl;
		tem++;
	}


	tem = coo[0].x;
	while(tem<coo[1].x) {
		cout<<tem<<' '<<coo[0].y<<endl;
		tem++;
	}

	tem = coo[1].x+1;
	while(tem<=coo[2].x) {
		cout<<tem<<' '<<coo[2].y<<endl;
		tem++;
	}

	return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值