fork(4) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define display(arr,s,e) for(i=s; i<=e; i++) cout<<arr[i]<<" ";
  5. #define mset(arr,x) memset(arr,x,sizeof(arr))
  6.  
  7. # define MOD 4294967296
  8. #define epsilon 0.000000000001
  9. #define I_MAX 9223372036854775807
  10. #define I_MIN -9223372036854775807
  11.  
  12. #define rep(i,s,e) for(i=s;i<=e;i++)
  13. #define rrep(i,s,e) for(i=s;i>=e;i--)
  14. #define endl "\n"
  15.  
  16. #define ll long long
  17. #define mid(a,b) ((a)+((b-a)/2))
  18. #define min(a,b) ((a)<(b)?(a):(b))
  19. #define max(a,b) ((a)>(b)?(a):(b))
  20.  
  21. // Useful STL commands:
  22. #define pb push_back
  23. #define mp make_pair
  24. #define f first
  25. #define s second
  26. #define si set<int>
  27. #define vi vector<int>
  28. #define ii pair<int,int>
  29. #define sii set<ii>
  30. #define vii vector<ii>
  31. #define all(c) c.begin(),c.end()
  32. #define tr(c,it) for(auto it=c.begin();it!=c.end();++it)
  33.  
  34. #define DEBUG
  35. // debugging
  36. #ifdef DEBUG
  37. #define trace1(x) cerr << #x << ": " << x << endl;
  38. #define trace2(x, y) cerr << #x << ": " << x << " | " << #y << ": " << y << endl;
  39. #define trace3(x, y, z) cerr << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": " << z << endl;
  40. #define trace4(a, b, c, d) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << endl;
  41. #define trace5(a, b, c, d, e) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << endl;
  42. #define trace6(a, b, c, d, e, f) cerr << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": " << c << " | " << #d << ": " << d << " | " << #e << ": " << e << " | " << #f << ": " << f << endl;
  43. #else
  44. #define trace1(x)
  45. #define trace2(x, y)
  46. #define trace3(x, y, z)
  47. #define trace4(a, b, c, d)
  48. #define trace5(a, b, c, d, e)
  49. #define trace6(a, b, c, d, e, f)
  50. #endif
  51.  
  52. double tick()
  53. {
  54. static clock_t oldtick; clock_t newtick = clock();
  55. double diff = 1.0*(newtick - oldtick) / CLOCKS_PER_SEC;
  56. oldtick = newtick;
  57. return diff;
  58. }
  59.  
  60. ll gcd(ll a, ll b)
  61. {
  62. if( (a == 0) || (b == 0) )
  63. return a + b;
  64. return gcd(b, a % b);
  65. }
  66.  
  67. ll pow_mod(ll a, ll b)
  68. {
  69. ll res = 1;
  70. while(b)
  71. {
  72. if(b & 1)
  73. res = (res * a)%MOD;
  74. a = (a * a)%MOD;
  75. b >>= 1;
  76. }
  77. return res;
  78. }
  79.  
  80. bool isPrime(ll a)
  81. {
  82. for(int i=3; (i*i)<=a; i+=2)
  83. {
  84. if( (a%i)==0 )
  85. {
  86. return false;
  87. }
  88. }
  89. if( ( a!=2 ) && ( (a%2)==0 ) )
  90. {
  91. return false;
  92. }
  93.  
  94. return true;
  95. }
  96.  
  97. string con_ll_to_str( ll a )
  98. {
  99. stringstream mystr;
  100. mystr << a;
  101.  
  102. return mystr.str();
  103. }
  104.  
  105. ll con_str_to_ll( string st )
  106. {
  107. ll numb = 0;
  108. int len = st.size(), i, j = 0;
  109. rrep(i, len-1, 0)
  110. {
  111. numb += ( pow_mod(10, j) * ( st[i] - '0' ) );
  112. j++;
  113. }
  114.  
  115. return numb;
  116. }
  117.  
  118. // Remember that set and map have the member functions find() and count(), which works in O(log N).
  119. // tick() can be here or there so don't deter and submit it. Check it always on ideone.
  120.  
  121. // bitset<99999000000000>& c = *(new bitset<99999000000000>()); // For O(1) lookup the max bitset space required.
  122. ll arr[100010];
  123. bool flag;
  124. vector< pair< pair< ll, int > , bool > > vc;
  125. bool comp( pair< pair< ll, int > , bool > pa1, pair< pair< ll, int > , bool > pa2 )
  126. {
  127. if( pa1.f.f==pa2.f.f )
  128. {
  129. return pa1.f.s < pa2.f.s;
  130. }
  131.  
  132. return pa1.f.f<pa2.f.f;
  133. }
  134.  
  135. int main()
  136. {
  137. ios_base::sync_with_stdio(false);
  138. cin.tie(0);
  139.  
  140. int i, j, Q, turn = 0;
  141. ll S1, A, B, Si, opop, ans = 0;
  142.  
  143. cin >> Q >> S1 >> A >> B;
  144.  
  145. Si = S1;
  146. while( Q-- )
  147. {
  148. opop = Si/2;
  149. if( Si&1 )
  150. {
  151. vc.pb( mp( mp(opop, turn), 1 ) );
  152. }
  153. else
  154. {
  155. vc.pb( mp( mp(opop, turn), 0 ) );
  156. }
  157.  
  158. Si = ( A*Si ) + B;
  159. if( Si>=MOD )
  160. Si %= MOD;
  161.  
  162. turn++;
  163. }
  164. sort( all( vc ) );
  165.  
  166. opop = I_MAX;
  167. if( vc[0].s==1 )
  168. {
  169. opop = vc[0].f.f;
  170. ans += opop;
  171. flag = 1;
  172. }
  173. rep(i, 1, turn-1)
  174. {
  175. if( vc[i].f.f==opop && flag && vc[i].s==0 )
  176. {
  177. flag = 0;
  178. ans -= vc[i].f.f;
  179. opop = I_MAX;
  180. }
  181. else if( vc[i].f.f!=opop && vc[i].s==1 )
  182. {
  183. opop = vc[i].f.f;
  184. ans += opop;
  185. flag = 1;
  186. }
  187. else
  188. {
  189. opop = I_MAX;
  190. }
  191. }
  192.  
  193. cout << ans;
  194.  
  195.  
  196. cerr<<tick();
  197. return 0;
  198. }
  199.  
Success #stdin #stdout #stderr 2.79s 4316KB
stdin
10000000 777777777 777777777 777777777
stdout
5362358669068782
stderr
2.7863