NC223615. GOutofSorts
描述
输入描述
Input consists of a line containing five integers n, m, a, c, and x0 (1 ≤ n ≤ 106 , 1 ≤ m, a, c ≤ 2 31 − 1, 0 ≤ x0 ≤ 2 31 − 1), where n is the length of the sequence x1, . . . , xn to be generated and m, a, c, and x0 are the constants used for generating the sequence. All values in the generated sequence are guaranteed to be unique.
输出描述
Output the number of sequence values that could be found using Ann’s version of binary search, assuming she forgot to sort the sequence.
示例1
输入:
5 8 1 3 3
输出:
2
示例2
输入:
6 10 1234567891 1 1234567890
输出:
6
C++ 解法, 执行用时: 64ms, 内存消耗: 4260K, 提交时间: 2021-07-22 12:13:35
#include<bits/stdc++.h> using namespace std; int n,m,a,c,l,r,cnt,mid; int A[1000005]; int main(){ cin>>n>>m>>a>>c>>A[0]; for(int i=1;i<=n;i++) A[i]=(1ll*a*A[i-1]%m+c)%m; for(int i=1;i<=n;i++){ l=1,r=n; while(l<=r){ mid=(l+r)/2; if(A[mid]==A[i]){ cnt++;break; } if(A[mid]>A[i]) r=mid-1; else l=mid+1; } } printf("%d\n",cnt); return 0; }