hdu 4379 The More The Better

题目描述:

Given an sequence of numbers {X1, X2, ... , Xn}, where Xk = (A * k + B) % mod. Your task is to find the maximum sub sequence {Y1, Y2, ... , Ym} where every pair of (Yi, Yj) satisfies Yi + Yj <= L (1 ≤ i < j ≤ m), and every Yi <= L (1 ≤ i ≤ m ).
Now given n, L, A, B and mod, your task is to figure out the maximum m described above. (1 ≤ n ≤ 2*107, 1 ≤ L ≤ 2*109, 1 ≤ A, B, mod ≤ 109)

题解:

题目可以用反证法证明,加入集合中的数大于l/2的数至多有1个,所以这个题做法如下:

对于所有小于等于L/2 的,统统可以放进来,并设放入的那些的最大值为maxn。然后对于没有放入的数,看看是否有一个数x,满足x+maxn<=l,如果有的话,答案加1.

这个题需要注意的是,竟然卡常数!!long long过不了,int又会溢出。所以我们不妨回过来看看这个式子Xk = (A * k + B) % mod,可以推出xk=(x(k-1)+a)%mod,这样就用加法来求并取模就不会超过unsigned int。

AC代码1903ms才过

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
unsigned int a,b,k,n,l,last;
unsigned int mod;
bool v[21000000];
int main()
{
    while(cin>>n>>l>>a>>b>>mod)
    {
        for(unsigned int i=1;i<=n;i++)
        v[i]=false;
        unsigned int ans=0;unsigned int max=0,t;
        last=b;
        for(unsigned int i=1;i<=n;i++)
        {
            t=(last+a)%mod;
            if(t*2<=l)
            {
                ans++;v[i]=true;
                if(t>max)max=t;
            }
            last=t;
        }
        last=b;
        for(unsigned int i=1;i<=n;i++)
        {
            t=(last+a)%mod;
            if((!v[i])&&t+max<=l){ans++;break;}
            last=t;
        }
        cout<<ans<<endl;
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值