Gcd HDU - 6545 (basic number theory)

Keywords: PHP iOS

wls has an integer n, he wants to divide the N numbers of 1_n into two groups, each group has at least one number, and make the sum of the two groups of numbers the largest common divisor, please output the largest common divisor.
Input
Enter an integer n in a row.
2 ≤ n ≤ 1, 000, 000, 000
Output
Output a line and an integer to represent the answer.
Sample Input
6
Sample Output
7

Train of thought:

Let's find sum of sum from 1 to n.

The sum of the two groups was sum1 and sum2, respectively.

So according to the meaning of the question, we know that sum1+sum2=sum

The answer is the largest one in gcd(sum1,sum2). Let's set it as ans.

According to the properties of gcd, we know that gcd(sum1,sum2)=gcd(sum1,sum)=gcd(sum2,sum)

Because gcd(x,y)=gcd(x,x+y)

Then we can get the equation:

sum1/ans+sum2/ans = sum/ans

Obviously, ans is a factor of sum. What we want to get is the largest ans, that is, the largest factor of sum.

How to find the largest factor? We just need to enumerate violently to find the smallest factor x, sum/x=ans for sum.

See the code for details:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <vector>
#include <iomanip>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define all(a) a.begin(), a.end()
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define gg(x) getInt(&x)
#define chu(x) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
using namespace std;
typedef long long ll;
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
ll powmod(ll a,ll b,ll MOD){ll ans=1;while(b){if(b%2)ans=ans*a%MOD;a=a*a%MOD;b/=2;}return ans;}
inline void getInt(int* p);
const int maxn=1000010;
const int inf=0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/

int main()
{
    //freopen("D:\\common_text\\code_stream\\in.txt","r",stdin);
    //freopen("D:\\common_text\\code_stream\\out.txt","w",stdout);
    
    
    ll n;
    cin>>n;
    if(n==2)
    {
        return puts("1");
    }else
    {
        ll sum=(n+1ll)*n/2ll;
        for(ll i=2ll;;++i)
        {
            if(sum%i==0)
            {
                cout<<sum/i<<endl;
                break;
            }
        }
    }
    
    return 0;
}

inline void getInt(int* p) {
    char ch;
    do {
        ch = getchar();
    } while (ch == ' ' || ch == '\n');
    if (ch == '-') {
        *p = -(getchar() - '0');
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 - ch + '0';
        }
    }
    else {
        *p = ch - '0';
        while ((ch = getchar()) >= '0' && ch <= '9') {
            *p = *p * 10 + ch - '0';
        }
    }
}



Posted by smoothrider on Fri, 11 Oct 2019 09:31:40 -0700