hdu 4379 The More The Better

来源:互联网 发布:成考和网络教育区别 编辑:程序博客网 时间:2024/06/05 01:50

题目描述:

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;}


原创粉丝点击