1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| #include <cstdio> #include<stdio.h> #include<math.h> const int MAXN = 1e7;
int n, a[MAXN + 8]; unsigned long long ans; namespace Generator {
unsigned long long k1, k2; int thres;
inline unsigned long long xorShift128Plus() { unsigned long long k3 = k1, k4 = k2; k1 = k4, k3 ^= (k3 << 23), k2 = k3 ^ k4 ^ (k3 >> 17) ^ (k4 >> 26); return k2 + k4; }
inline void generate() { for (int i = 1; i <= n; ++i) { a[i] = xorShift128Plus() % thres; } }
} int seach(int beg){ int remain=1e9,remain_id=0; for(int i=beg;i<=n;i++){ if(a[i]<remain){ remain=a[i]; remain_id=i; } } return remain_id; } int seach1(int min,int beg){ for(int i=n;i>=beg;i--){ if(a[i]<=min){ return i; } } } void swap(int &a1,int &b1){ int remain=a1; a1=b1; b1=remain; } int main() { scanf("%d", &n); scanf("%llu %llu %d", &Generator::k1, &Generator::k2, &Generator::thres); Generator::generate(); int beg=1,end=1; while(beg<=end&&end<=n){ while(a[end]<=a[end+1]&&end+1<=n){ end++; } if(end==n){ break; } int end1=0; end1=seach(end+1); int remain_min=a[end1]; while(a[beg]<=a[end1]){ beg++; } end1=seach1(remain_min,end+1); if(end1){ swap(a[beg],a[end1]); end1++; beg=end=end1;} }
for(int i=1;i<=n;i++){ unsigned long long remain4=a[i]; ans+=(remain4*i); } printf("%llu",ans); return 0; }
|