Maximize?
题面翻译
给你一个整数 x x x 。您的任务是找出任意整数 y y y ( 1 ≤ y < x ) (1\le y<x) (1≤y<x) ,使得 gcd ( x , y ) + y \gcd(x,y)+y gcd(x,y)+y 最大。
如果有不止一个 y y y 满足该语句的要求,那么你可以找出任何一个。
题目描述
You are given an integer x x x . Your task is to find any integer y y y ( 1 ≤ y < x ) (1\le y<x) (1≤y<x) such that gcd ( x , y ) + y \gcd(x,y)+y gcd(x,y)+y is maximum possible.
Note that if there is more than one y y y which satisfies the statement, you are allowed to find any.
gcd ( a , b ) \gcd(a,b) gcd(a,b) is the Greatest Common Divisor of a a a and b b b . For example, gcd ( 6 , 4 ) = 2 \gcd(6,4)=2 gcd(6,4)=2 .
输入格式
The first line contains a single integer t t t ( 1 ≤ t ≤ 1000 1 \le t \le 1000 1≤t≤1000 ) — the number of test cases.
Each of the following t t t lines contains a single integer x x x ( 2 ≤ x ≤ 1000 2 \le x \le 1000 2≤x≤1000 ).
输出格式
For each test case, output any y y y ( 1 ≤ y < x 1 \le y < x 1≤y<x ), which satisfies the statement.
样例 #1
样例输入 #1
7
10
7
21
100
2
1000
6
样例输出 #1
5
6
18
98
1
750
3
代码内容
// #include <iostream>
// #include <algorithm>
// #include <cstring>
// #include <stack>//栈
// #include <deque>//队列
// #include <queue>//堆/优先队列
// #include <map>//映射
// #include <unordered_map>//哈希表
// #include <vector>//容器,存数组的数,表数组的长度
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b)
{
if(!b) return a;
else return gcd(b,a%b);
}
void solve()
{
ll x;
cin>>x;
ll op=1,mx=0;
for(ll i=1;i<x;i++)
{
ll ans=gcd(x,i)+i;
if(ans>mx)
{
op=i;
mx=ans;
}
}
cout<<op<<endl;
}
int main()
{
ll t;
cin>>t;
while(t--) solve();
return 0;
}