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