技巧(5)数对和小于定值

The More The Better

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3360    Accepted Submission(s): 863


Problem Description
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.
 

 

Input
Multiple test cases, process to the end of input. Every test case has a single line. A line of 5 integers: n, L, A, B and mod. (1 ≤ n ≤ 2*107, 1 ≤ L ≤ 2*109, 1 ≤ A, B, mod ≤ 109)
 

 

Output
For each case, output m in one line.
 

 

Sample Input
1 8 2 3 6
5 8 2 3 6
 

 

Sample Output
1
4
 

 

Source
 

 一对数(x,y)之和 比 L 小; 要么x < L/2 && y < L/2; 要么 x < L/2 && y > L/2; 不会出现 x > L/2 && y > L/2; 所以对于数组x[1~n]:x[] < L/2 , 一定可以。

并且只能有一个大于 L/2 的 数字x[] , 当然 这要保证 这个 x[] 与 其他所有 小于 L/2 的x[] 的和 都 < L , 才可成立。所以 判断 maxn(x[<=L/2]) + minn(x[>L/2]) 是否 <= L 。

#include<cstdio>
#include<algorithm>
#define ll __int64
using namespace std;
int main(){
    ll n,a,b,mod,l;
    while(~scanf("%I64d%I64d%I64d%I64d%I64d",&n,&l,&a,&b,&mod)){
        ll ans = 0;
        ll maxn = 0;
        ll minn = 2e9;
        for(ll i = 1; i <= n; i++){
            ll x = (a*i+b)%mod;
            if((x*2)<= l){
                ans++;
                maxn = max(x,maxn);
            }
            else {
                minn = min(x,minn);
            }
        }
        if(maxn+minn <= l)ans++;
        printf("%I64d\n",ans);
    }
    return 0;
}

 

posted @ 2015-09-01 19:04  Tobu  阅读(113)  评论(0)    收藏  举报