HDU 4379 - The More The Better

 

题目地址:  http://acm.hdu.edu.cn/showproblem.php?pid=4379

 

2012年多校 ,第8场,  1010题。

 

水题一枚。。。 O(n) 就可以过。。。

 

比赛时怪叔叔找循环节去了,找了之后,然后排序,暴力去跑,结果变成了 O(n lg n)  ,TLE了。。。

 

思路很简单: 直接从1到n 寻找所有 Xi ,结果 < =  L/2  的全部都要,于是这里统计了 sum 个。

 

然后从找到的这些数中,取一个最大值, 再从 > L/2 的 结果中找一个最小值,如果 这两个值的和 <= L ,那么此时 sum++。

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<vector>

using namespace std;

int main(){
	__int64 mn,mx,n,L,LL,A,B,mod,tmp;
	int i,sum;
	while(~scanf("%I64d%I64d%I64d%I64d%I64d",&n,&L,&A,&B,&mod)){
		sum=0;
		mx=0;
		LL=L/2;
		mn=L;
		B%=mod;  
		A%=mod;// 下面用减法,这里需要先预处理A 和 B 
		for(i=1,tmp=(B+A)%mod;i<=n;i++,tmp+=A){
			if(tmp>=mod) tmp-=mod; // 这里用减法,直接从1600MS降到600MS 
			if(tmp<=LL){
				sum++;
				if(mx<tmp) mx=tmp;
			}
			else{
				if(mn>tmp) mn=tmp;
			}
		}
		if(mn+mx<=L) sum++;
		printf("%d\n",sum);
	}
	return 0;
}


 

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值