BNU OJ The More The Better || HDU 4379

5 篇文章 0 订阅

The More The Better

Given an sequence of numbers {X 1, X 2, ... , X n}, where X k = (A * k + B) % mod. Your task is to find the maximum sub sequence {Y 1, Y 2, ... , Y m} where every pair of (Y i, Y j) satisfies Y i + Y j <= 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*10 7, 1 ≤ L ≤ 2*10 9, 1 ≤ A, B, mod ≤ 10 9)

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
 
刚开始是用双重循环做的,老是TLE、Runtime Error,主要是没意识到数学规律。。。
// ( x + d ) % m = ( x % m + d % m ) % m
// 比较 x 和 L/2 的大小,若小于,则满足条件
// 两个数相加小于 L,则两个数不能都大于 L/2 ,即只能有 1个数大于 L/2 
#include<stdio.h>
#include<stdlib.h>
int main()
{
    int i,d;
    int n,x,l,a,b,m,mid,max,min,k;
    while(scanf("%d %d %d %d %d",&n,&l,&a,&b,&m)!=EOF){
         max=0;  k=0;  min=l;
         mid=l/2;     
         d=a%m;
         x=(a+b)%m;   // 即 x1 
         for(i=1;i<=n;i++){
             if(x<=mid){ 
               k++;
               if(x>max)   max=x;
             }
             else 
               if(x<min)   min=x;
             x=(x+d)%m;
         }
         if(min<=l-max)          //不同于min+max<=l ,判断 min+max 是不是小于 L 
            k++;
         printf("%d\n",k); 
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值