首页 > 其他分享 >codeforces #864 div2 B

codeforces #864 div2 B

时间:2023-04-22 11:37:19浏览次数:52  
标签:gcd LL 864 codeforces b1 b2 b3 include div2

GCD Partition

这道题首先要解决一个问题,要把区间分成几块,可以证明分成两块是更优

首先我们假设把区间分成了m(>= 2)块 b1, b2, b3, ...,bm,则答案是gcd(b1, b2, b3,..., bm),则b1,b2是 gcd(b1, b2, b3,..., bm)的倍数,那么b1 + b2也是gcd(b1, b2, b3,..., bm)的倍数,所以gcd(b1, b2, b3,..., bm)<=gcd(b1 + b2, b3,..., bm),所以依此类推,区间数越少,越优,所以k = 2是最优的

然后我们可以处理一个前缀和,枚举一下分段点,求一下gcd,找出最大值即可

#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>

using namespace std;
typedef long long LL;
const int N = 2e5 + 10;
int a[N];
LL s[N];

LL gcd(LL a, LL b)
{
	if(b == 0)	return a;
	return gcd(b, a % b);
}

void solve()
{
	int n;
	cin >> n;
	for(int i = 1; i <= n; ++ i)	
	{
		scanf("%d", &a[i]);
		s[i] = s[i - 1] + a[i];
	}
	LL ans = 0;
	for(int i = 1; i <= n - 1; ++ i)
		ans = max(ans, gcd(s[i], s[n] - s[i]));
	cout << ans << endl;
}

int main()
{
	int t;
	cin >> t;
	while(t --)	solve();
}

标签:gcd,LL,864,codeforces,b1,b2,b3,include,div2
From: https://www.cnblogs.com/cxy8/p/17342666.html

相关文章

  • Educational Codeforces Round 147 (Rated for Div. 2)
    EducationalCodeforcesRound147(RatedforDiv.2)链接EducationalCodeforcesRound147(RatedforDiv.2)A题如果第一位数是0,直接打印0如果第一位数是'?',有9个数可以选择,如果其他位数是'?',有10中情况选择,相乘就可以了#include<iostream>#include<algo......
  • CodeForces 34B Sale
    B- SaleTimeLimit:2000MS     MemoryLimit:262144KB     64bitIOFormat:%I64d&%I64uSubmit Status Practice CodeForces34BDescriptionOnceBobgottoasaleofoldTVsets.Therewere n TVsetsatthatsale.TVsetwithi......
  • C. Table Decorations(Codeforces)(规律)
    C.TableDecorationstimelimitpertestmemorylimitpertestinputoutputr red, g greenand b blueballoons.Todecorateasingletableforthebanquetyo......
  • CodeForces 34A Reconnaissance 2
     Reconnaissance2TimeLimit:2000MS     MemoryLimit:262144KB     64bitIOFormat:%I64d&%I64uSubmit Status Practice CodeForces34ADescriptionn soldiersstandinacircle.Foreachsoldierhisheight ai isknown.Areco......
  • codeforces round The Monster and the Squirrel 529B (数学规律)
    TheMonsterandtheSquirrelTimeLimit: 1000MS MemoryLimit: 262144KB 64bitIOFormat: %I64d&%I64uSubmit StatusDescriptionArithemonsteralwayswakesupveryearlywiththefirstrayofthesunandthefirstthingshedoesisfeedinghersqu......
  • Codeforces Round 850 (Div. 2, based on VK Cup 2022 - Final Round) E. Monsters (h
    传送门详细题解传送门  抄的ygg代码,向在这里说一下刚开始没看懂的部分。  求答案的时候是把所有的当前为止的所有数值加起来减去一个从1开始并且公差为1的等差数列的前size项和。其中size是当前最多能用到哪个位置,满足前size项能构成1,2,3,....,sz这样的形式。  假设我们......
  • CodeForces - 610B Vika and Squares (模拟)
    CodeForces-610BVikaandSquaresTimeLimit: 2000MS MemoryLimit: 262144KB 64bitIOFormat: %I64d&%I64uSubmit StatusDescriptionVikahas n jarswithpaintsofdistinctcolors.Allthejarsarenumberedfrom 1 to n andthe......
  • CodeForces - 659C Tanya and Toys (map&模拟)
    CodeForces-659CTanyaandToysTimeLimit: 1000MS MemoryLimit: 262144KB 64bitIOFormat: %I64d&%I64uSubmit StatusDescriptionInBerlandrecentlyanewcollectionoftoyswentonsale.Thiscollectionconsistsof 109 typesof......
  • CodeForces - 367B Sereja ans Anagrams (map)
    CodeForces-367BSerejaansAnagramsTimeLimit: 1000MS MemoryLimit: 262144KB 64bitIOFormat: %I64d&%I64uSubmit StatusDescriptionSerejahastwosequences a and b andnumber p.Sequence a consistsof n integers a1, a......
  • CodeForces - 368C Sereja and Algorithm (找规律&模拟)
    CodeForces-368CSerejaandAlgorithmTimeLimit: 1000MS MemoryLimit: 262144KB 64bitIOFormat: %I64d&%I64uSubmit StatusDescriptionSerejalovesallsortsofalgorithms.Hehasrecentlycomeupwithanewalgorithm,whichreceiv......