Advertisement
Guest User

Untitled

a guest
May 6th, 2019
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. #pragma warning(disable:4786)
  2. #pragma warning(disable:4996)
  3. #include<bits/stdc++.h>
  4. #include <ext/pb_ds/assoc_container.hpp>
  5. #include <ext/pb_ds/trie_policy.hpp>
  6. #define pii pair<int,int>
  7. #define pll pair<long long ,long long>
  8. #define pli pair<long long , int>
  9. #define pil pair<int ,long long>
  10. #define pi acos(-1)
  11. #define pb push_back
  12. #define mkp make_pair
  13. #define all(a) a.begin(), a.end()
  14. #define IOS ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
  15. #define ll long long
  16. #define MAX 300005
  17. #define INF 0x3f3f3f3f
  18. template <class T> inline T bigmod(T p,T e,T M){ll ret = 1LL;for(; e > 0LL; e >>= 1LL){if(e & 1LL) ret = (ret * p) % M;p = (p * p) % M;}return (T)ret;}
  19. template <class T> inline T modinverse(T a,T M){return bigmod(a,M-2,M);} // M is prime}
  20.  
  21. using namespace std;
  22. using namespace __gnu_pbds;
  23.  
  24. typedef trie<string,null_type,trie_string_access_traits<>,pat_trie_tag,trie_prefix_search_node_update>pref_trie;
  25.  
  26.  
  27. ll power(ll x, ll y, ll p)
  28. {
  29. ll res = 1LL; // Initialize result
  30. x = x % p; // Update x if it is more than or
  31. // equal to p
  32. while (y > 0)
  33. {
  34. // If y is odd, multiply x with result
  35. if (y & 1LL)
  36. res = (res*x) % p;
  37.  
  38. // y must be even now
  39. y = y>>1LL; // y = y/2
  40. x = (x*x) % p;
  41. }
  42. return res;
  43. }
  44.  
  45. // This function is called for all k trials. It returns
  46. // false if n is composite and returns false if n is
  47. // probably prime.
  48. // d is an odd number such that d*2<sup>r</sup> = n-1
  49. // for some r >= 1
  50. bool miillerTest(ll d, ll n)
  51. {
  52. // Pick a random number in [2..n-2]
  53. // Corner cases make sure that n > 4
  54. ll a = 2 + rand() % (n - 4);
  55.  
  56. // Compute a^d % n
  57. ll x = power(a, d, n);
  58.  
  59. if (x == 1LL || x == n-1LL)
  60. return true;
  61.  
  62. // Keep squaring x while one of the following doesn't
  63. // happen
  64. // (i) d does not reach n-1
  65. // (ii) (x^2) % n is not 1
  66. // (iii) (x^2) % n is not n-1
  67. while (d != n-1LL)
  68. {
  69. x = (x * x) % n;
  70. d *= 2LL;
  71.  
  72. if (x == 1LL) return false;
  73. if (x == n-1LL) return true;
  74. }
  75.  
  76. // Return composite
  77. return false;
  78. }
  79.  
  80. // It returns false if n is composite and returns true if n
  81. // is probably prime. k is an input parameter that determines
  82. // accuracy level. Higher value of k indicates more accuracy.
  83. bool isPrime(ll n, ll k)
  84. {
  85. // Corner cases
  86. if (n <= 1LL || n == 4LL) return false;
  87. if (n <= 3LL) return true;
  88.  
  89. // Find r such that n = 2^d * r + 1 for some r >= 1
  90. int d = n - 1LL;
  91. while (d % 2LL == 0)
  92. d /= 2LL;
  93.  
  94. // Iterate given nber of 'k' times
  95. for (ll i = 0LL; i < k; i++)
  96. if (!miillerTest(d, n))
  97. return false;
  98.  
  99. return true;
  100. }
  101. set<int>primes;
  102. vector<int>prime;
  103. bitset<100000>bs;
  104. void sieve (int n){
  105. bs.set ();
  106. //prime.pb(2);
  107. for (int i = 2; i <= n; i++){
  108. if (bs[i])prime.pb(i);
  109.  
  110. for (int j = 0; j<prime.size() && i*prime[j]<=n; j++){
  111. bs[i*prime[j]] = 0;
  112. if(i%prime[j]==0)break;
  113. }
  114. }
  115. }
  116.  
  117. int main(){
  118. IOS
  119. //freopen("output.txt","w",stdout);
  120. int t;
  121. cin>>t;
  122. sieve(1000);
  123. map<ll,int>mp;
  124. for(auto it=prime.begin();it!=prime.end();it++){
  125. mp[(*it)]++;
  126. }
  127. for(int tt=0;tt<t;tt++){
  128. ll n;
  129. cin>>n;
  130. if(n<=11){
  131. cout<<"Case "<<tt+1<<": IMPOSSIBLE\n";
  132. continue;
  133. }
  134. ll ans;
  135. if(n<1000){
  136. ans=2LL;
  137. }
  138. else{
  139. for(ll i=n-10LL;;i--){
  140. if(isPrime(i,4LL)){
  141. ans = i;
  142. break;
  143. }
  144. }
  145. }
  146.  
  147. vector<ll>vec;
  148. vec.pb(ans);
  149. ll now = n-ans;
  150. if(now&1LL){
  151. now-=7;
  152. vec.pb(2LL);
  153. vec.pb(2LL);
  154. vec.pb(3LL);
  155. }
  156. else{
  157. now-=6;
  158. vec.pb(2LL);
  159. vec.pb(2LL);
  160. vec.pb(2LL);
  161. }
  162. for(auto it =prime.begin();it!=prime.end();it++){
  163. if(mp[now-(*it)]>0){
  164. vec.pb((*it));
  165. vec.pb(now-(*it));
  166. sort(vec.begin(),vec.end());
  167. cout<<"Case "<<tt+1<<": ";
  168. for(auto it2 = vec.begin();it2!=vec.end();it2++){
  169. cout<<(*it2)<<" ";
  170. }
  171. break;
  172. }
  173. }
  174.  
  175. cout<<"\n";
  176. }
  177. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement