2023暑假集训 第七次vj比赛第D题——听我的回音

ttyy0915 / 2023-08-10 / 原文

题意:起点在(1,1),每次只可以向右或者向上移动一格。要求到达(i,j)且i*j=N.给出N。

思路:遍历sqrt(N),i和j都是N的因子,找出最短路径。

点击查看代码
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define inf 0x3f3f3f3f3f3f3f3f

ll n;
int main(){
	cin>>n;
	ll ans=inf;
	for(int i=1;i<=sqrt(n);i++){
		if(n%i==0){
			ans=min(n/i+i-2,ans);
		}
	}
	cout<<ans;
	
	return 0;
}