fork download
  1. #include<bits/stdc++.h>
  2. #define ll long long int
  3. #define ld long double
  4. #define pb push_back
  5. #define fi first
  6. #define se second
  7. #define ios ios_base::sync_with_stdio(false)
  8. using namespace std;
  9. ll f(vector< vector<ll> > v, int idx)
  10. {
  11. int i;
  12. ll area=0;
  13. ll r=-1;
  14. for(i=0;i<v.size();i++)
  15. {
  16. if(i==idx) continue;
  17. if(v[i][0]>r)
  18. {
  19. area+=v[i][1]-v[i][0]+1;
  20. r=v[i][1];
  21. }
  22. else if(v[i][0]==r)
  23. {
  24. area+=v[i][1]-v[i][0];
  25. r=v[i][1];
  26. }
  27. else if(v[i][1]>r)
  28. {
  29. area+=v[i][1]-r;
  30. r=v[i][1];
  31. }
  32. }
  33. return area;
  34. }
  35. int main()
  36. {
  37. ios;
  38. int t;
  39. cin>>t;
  40. int tc=1;
  41. while(tc<=t)
  42. {
  43. ll n,l1,r1,a,b,c1,c2,m;
  44. cin>>n>>l1>>r1>>a>>b>>c1>>c2>>m;
  45. vector< vector<ll> > v,xy;
  46. vector<ll> temp={l1,r1};
  47. v.pb(temp);
  48. xy.pb(temp);
  49. int i;
  50. ll x,y;
  51. for(i=1;i<n;i++)
  52. {
  53. x=((((a%m)*(xy[i-1][0]%m))%m+((b%m)*(xy[i-1][1]%m))%m)%m+c1%m)%m;
  54. y=((((a%m)*(xy[i-1][1]%m))%m+((b%m)*(xy[i-1][0]%m))%m)%m+c2%m)%m;
  55. vector<ll> temp1={x,y};
  56. xy.pb(temp1);
  57. vector<ll> temp2={min(x,y),max(x,y)};
  58. v.pb(temp2);
  59. }
  60. sort(v.begin(),v.end());
  61. /*cout << "Generated Sets - \n";
  62. for(i=0;i<v.size();i++) cout << v[i][0] << " " << v[i][1] << "\n";
  63. cout << "---------------------\n";*/
  64. ll ans=1000000000000000000L;
  65. for(i=0;i<n;i++)
  66. {
  67. ans=min(ans,f(v,i));
  68. }
  69. cout << "Case #" << tc << ": " << ans << "\n";
  70. tc++;
  71. }
  72. return 0;
  73. }
  74.  
  75. // aC2+bC2+cC2+ab+bc
Time limit exceeded #stdin #stdout 5s 1029120KB
stdin
Standard input is empty
stdout
Standard output is empty