比赛 20161115 评测结果 AAAAATTTTT
题目名称 军队 最终得分 50
用户昵称 Tabing010102 运行时间 5.044 s
代码语言 C++ 内存使用 0.69 MiB
提交时间 2016-11-15 11:42:28
显示代码纯文本
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef unsigned long long ULL;
const int maxn = 100000+100;
const ULL INF = 0x7fffffffffffffff;
int n, k, A[maxn];
ULL gcd(ULL a, ULL b) { return b==0?a:gcd(b, a%b); }
ULL lcm(ULL a, ULL b) { return a/gcd(a, b)*b; }
ULL max(ULL a, ULL b) { return a<b?b:a; }
int gcd(int a, int b) { return b==0?a:gcd(b,a%b); }
struct SEG {
	ULL sumv[maxn<<2];
//	ULL lcmv[maxn<<2];
//	ULL gcdmax[maxn<<2];
	SEG(int n) {
		this->build(1, 1, n);
	}
	void build(int o, int L, int R) {
		if(L == R) {
			sumv[o] = A[L];
//			lcmv[o] = A[L];
//			gcdmax[o] = 1;
		} else {
			int lc=o*2, rc=o*2+1;
			int M = L + (R-L)/2;
			build(lc, L, M);
			build(rc, M+1, R);
			sumv[o] = sumv[lc]+sumv[rc];
//			lcmv[o] = lcm(lcmv[lc], lcmv[rc]);
//			gcdmax[o] = gcd(lcmv[lc], lcmv[rc]);
		}
	}
	int oL, oR;
	/*
	ULL getgcd(int o, int L, int R) {
		if(L>=oL && R<=oR) {
			return lcmv[o];
		} else {
			int lc=o*2, rc=o*2+1;
			int M = L + (R-L)/2;
			ULL tans = 0;
			if(M >= oL) tans = getgcd(lc, L, M);
			if(M < oR) {
				if(tans == 0) tans = getgcd(rc, M+1, R);
				else tans = gcd(tans, getgcd(rc, M+1, R));
			}
			return tans;
		}
	}
	
	ULL _gcdmax;
	void qgcdmax(int o, int L, int R) {
		if(L>=oL && R<=oR) {
			_gcdmax = max(_gcdmax, gcdmax[o]);
		} else {
			int lc=o*2, rc=o*2+1;
			int M = L + (R-L)/2;
			if(M >= oL) qgcdmax(lc, L, M);
			if(M < oR) qgcdmax(rc, M+1, R);
		}
	}
	bool gcde1(int l, int r) {
		oL = l; oR = r;
		_gcdmax = 1;
		qgcdmax(1, 1, n);
		return _gcdmax==1;
	}
	*/
	ULL _sum;
	void qsum(int o, int L, int R) {
		if(L>=oL && R<=oR) {
			_sum += sumv[o];
		} else {
			int lc=o*2, rc=o*2+1;
			int M = L + (R-L)/2;
			if(M >= oL) qsum(lc, L, M);
			if(M < oR) qsum(rc, M+1, R);
		}
	}
	ULL qsum(int l, int r) {
		oL = l; oR = r; _sum = 0;
		qsum(1, 1, n);
		return _sum;
	}
};
bool gcde1(int l, int r) {
	for(int i = l; i < r; i++)
		for(int j = i+1; j <= r; j++)
			if(gcd(A[i], A[j]) != 1) return false;
	return true;
}
int ans;
bool judge(int v, SEG *D) {
	bool flag = false;
	for(int i = n; i >= v; i--) {
		int nl=i-v+1, nr=i;
		if(!gcde1(nl, nr)) continue;
		if(D->qsum(nl, nr) < k) continue;
//		printf("judge(v=%d), k=%d, nl=%d, nr=%d\n", v, k, nl, nr);
		flag = true; break;
	}
	return flag;
}
FILE *fin, *fout;
int main() {
	fin = fopen("tarmy.in", "r");
	fout = fopen("tarmy.out", "w");
	fscanf(fin, "%d%d", &n, &k);
//	printf("n=%d, k=%d\n", n, k); 
	if(n <= 0) { fprintf(fout, "0\n"); exit(0); }
	for(int i = 1; i <= n; i++) fscanf(fin, "%d", &A[i]);
	SEG *D = new SEG(n);
	int L=0, R=n;
	while(L <= R) {
		int M = L + (R-L)/2;
		if(judge(M, D)) { ans=M; L=M+1; }
		else R = M-1;
	}
	fprintf(fout, "%d\n", ans);
	return 0;
}