sgu 246 分类: sgu 2015-06...

本文探讨了一道算法题目,详细解析了如何通过计算最大公约数(gcd)确定珠子的联通块数量与大小,进而得出黑色珠子的最大数量。算法涉及数学原理与编程实现,提供了完整的代码示例。

一道好题。

把距离为 n+1的珠子连边,一共有 gcd(2n1,n+1) 个联通块,

每个联通块的大小为 2n1gcd(2n1,n+1),

所以每个联通块最多可以有(2n1)gcd(2n1,n+1)2 颗黑色珠子。

那么 ans=(2n1)gcd(2n1,n+1)2gcd(2n1,n+1)+1


#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<cstring>
#include<map>
#include<string>
#include<stack>
#include<queue>
#include<utility>
#include<iostream>
#include<algorithm>

int gcd(int a,int b)
{
    return b?gcd(b, a%b):a;
}

int main()
{
    int n, m, cnt, L;
#ifndef ONLINE_JUDGE
    freopen("sgu246.in","r",stdin);
    freopen("sgu246.out","w",stdout);
#endif

    std::cin >> m;

    n = ((unsigned int)m + 1)>>1;

    cnt = gcd(m, n+1), L = m/cnt;

    std::cout << (long long)cnt*(L>>1)+1;

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

转载于:https://www.cnblogs.com/dashgua/p/4722984.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值