Advertisement
Guest User

Untitled

a guest
Jul 1st, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.82 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define ll long long
  3. #define mod 1000000007
  4. #define mx 10000000
  5. #define SIZE 1000001
  6.  
  7. using namespace std;
  8.  
  9. int arr[SIZE];
  10. bool status[mx];
  11. /**************************************Seive************************************/
  12. void seive()
  13. {
  14. int N=mx;
  15. int sq=sqrt(N);
  16. for(int i=4; i<=N; i+=2) status[i]=1;
  17. for(int i=3; i<=sq; i+=2)
  18. if(!status[i])
  19. for(int j=i*i; j<=N; j+=2*i) status[j]=1;
  20.  
  21. status[1]=1;
  22. }
  23.  
  24.  
  25. /**********************************Bitwise Seive*********************************/
  26. //int N =100,prime[100];
  27. //int status[100/32];
  28. /*
  29. bool Check(int N,int pos)
  30. {
  31. return (bool)(N & (1<<pos));
  32. }
  33. int Set(int N,int pos)
  34. {
  35. return N=N | (1<<pos);
  36. }
  37. void sieve()
  38. {
  39. int i, j, sqrtN;
  40. sqrtN = int( sqrt( N ) );
  41. for( i = 3; i <= sqrtN; i += 2 )
  42. {
  43. if( Check(status[i/32],i%32)==0)
  44. {
  45. for( j = i*i; j <= N; j += 2*i )
  46. {
  47. status[j/32]=Set(status[j/32],j % 32) ;
  48. }
  49. }
  50. }
  51. puts("2");
  52. for(i=3; i<=N; i+=2)
  53. if( Check(status[i/32],i%32)==0)
  54. printf("%d\n",i);
  55.  
  56. }
  57. ?/
  58.  
  59. /*********************************modPow*******************************/
  60. ll modPow(ll b, ll e, ll m)
  61. {
  62. ll res = 1;
  63. for (; e; e >>= 1, b = b*b%m) if (e & 1) res = res*b%m;
  64. return res;
  65. }
  66.  
  67.  
  68. /*********************************Segmented Seive*******************************/
  69. int segmentedSieve ( int a, int b )///Returns number of primes between segment [a,b]
  70. {
  71. if ( a == 1 ) a++;
  72. int sqrtn = sqrt ( b );
  73. memset ( arr, 0, sizeof arr ); ///Make all index of arr 0.
  74.  
  75. vector<int>prime;
  76. seive();
  77. prime.push_back(2);
  78. for(int i=3; i<=1000000; i+=2)
  79. if(!status[i])prime.push_back(i);
  80.  
  81. for ( int i = 0; i < prime.size() && prime[i] <= sqrtn; i++ )
  82. {
  83. int p = prime[i];
  84. int j = p * p;
  85. if ( j < a ) ///If j is smaller than a, then shift it inside of segment [a,b]
  86. j = ((a+p-1)/p)*p;
  87. for ( ; j <= b; j += p )
  88. arr[j-a] = 1; ///mark them as not prime
  89. }
  90. int res = 0;
  91. for ( int i = a; i <= b; i++ )
  92. if ( arr[i-a] == 0 ) res++; ///If it is not marked, then it is a prime
  93. return res;
  94. }
  95.  
  96.  
  97. /**********************************Euler Function******************************/
  98. int Euler(int n)
  99. {
  100. int result = n;
  101. if(n%2==0) result -= result / 2;
  102. while (n % 2 == 0) n/=2;
  103. for(int i=3; i*i <= n; i+=2)
  104. {
  105. if (n % i == 0) result -= result / i;
  106. while (n % i == 0) n /= i;
  107. }
  108. if (n > 1) result -= result / n;
  109. return result;
  110. }
  111.  
  112.  
  113. /********************************No Of Divisor****************************/
  114. ll Divisor(ll n)
  115. {
  116. ll result = 1;
  117. ll p=1;
  118. while (n % 2 == 0)
  119. {
  120. n /= 2;
  121. p++;
  122. }
  123. result *= p;
  124. for(ll i=3; i*i <= n; i+=2)
  125. {
  126. p=1;
  127. while (n % i == 0)
  128. {
  129. n /= i;
  130. p++;
  131. }
  132. result *= p;
  133. }
  134. if (n > 1) result *= 2;
  135. return result;
  136. }
  137.  
  138.  
  139.  
  140. /*************************************Mulmod**********************************/
  141. long long mulmod(long long a,long long b,long long c)
  142. {
  143. long long x = 0,y=a%c;
  144. while(b > 0) /// this function calculates (a*b)%c taking into account
  145. {
  146. /// that a*b might overflow
  147. if(b&1 == 1)
  148. {
  149. x = (x+y)%c;
  150. }
  151. y = (y*2)%c;
  152. b >>= 1;///b /= 2
  153. }
  154. return x%c;
  155. }
  156.  
  157.  
  158.  
  159. /*************************************BigMod**********************************/
  160. ll bigmod(ll a,ll b,ll c) ///If a,b int then this functon is enough
  161. {
  162. ///else call mulmod function
  163. long long x=1,y=a; /// long long is taken to avoid overflow of intermediate results
  164. while(b > 0)
  165. {
  166. if(b&1 == 1)///b%2==b&1
  167. {
  168. x=mulmod(x,y,c)%c;
  169. ///x=(x*y)%c;
  170. }
  171. y=mulmod(y,y,c)%c;
  172. ///y = (y*y)%c;
  173. b >>= 1;/// b /= 2
  174. }
  175. return x%c;
  176. }
  177.  
  178.  
  179.  
  180. /************************Fermat’s Little Theorem******************************/
  181. bool Fermat(ll p, ll iterations)///p the number which will be checked
  182. {
  183. ///(a^(p-1))% p==1 ? prime : not prime;
  184. if(p == 1) /// 1 isn't prime
  185. return false;
  186. for(int i=0; i<iterations; i++)
  187. {
  188. /// choose a random integer between 1 and p-1 ( inclusive )
  189. ll a = rand()%(p-1)+1;
  190. /// modulo is the function we developed above for modular exponentiation.
  191. if(bigmod(a,p-1,p) != 1)
  192. return false; ///* p is definitely composite */
  193. }
  194. return true; ///* p is probably prime */
  195. }
  196.  
  197.  
  198.  
  199. /************************************Miller Test******************************/
  200. bool Miller(ll p,ll iteration)
  201. {
  202. if(p<2)
  203. {
  204. return false;
  205. }
  206. if(p!=2 && p%2==0)
  207. {
  208. return false;
  209. }
  210. ll s=p-1;
  211. while(s%2==0)
  212. {
  213. s/=2;
  214. }
  215. for(ll i=0; i<iteration; i++)
  216. {
  217. ll a=rand()%(p-1)+1,temp=s;
  218. ll mood=bigmod(a,temp,p);
  219. while(temp!=p-1 && mood!=1 && mood!=p-1)
  220. {
  221. mood=mulmod(mood,mood,p);
  222. temp *= 2;
  223. }
  224. if(mood!=p-1 && temp%2==0)
  225. {
  226. return false;
  227. }
  228. }
  229. return true;
  230. }
  231.  
  232. /*********************************Extended_Euclid****************************/
  233. ll Extended_Euclid(ll A, ll B, ll *X, ll *Y)///pointer take the address
  234. {
  235. ll x, y, u, v, m, n, a, b, q, r;
  236. x = 0;
  237. y = 1; ///* B = A(0) + B(1) */
  238. u = 1;
  239. v = 0; ///* A = A(1) + B(0) */
  240. for (a = A, b = B; 0 != a; b = a, a = r, x = u, y = v, u = m, v = n)
  241. {
  242. q = b / a; ///* b = aq + r and 0 <= r < a */
  243. r = b % a;
  244. m = x - (u * q); ///* r = Ax + By - aq = Ax + By - (Au + Bv)q = A(x - uq) + B(y - vq) */
  245. n = y - (v * q);
  246. }
  247. *X = x;
  248. *Y = y; ///* Ax + By = gcd(A, B) */
  249. return b;
  250. }
  251.  
  252.  
  253. /*********************************modInverse****************************/
  254. ll modInverse(ll a, ll m) ///a the number whose modular inverse will be counted
  255. {
  256. ///m is the mod value
  257. ll m0 = m, t, q;
  258. ll x0 = 0, x1 = 1;
  259. if (m == 1) return 0;
  260. while (a > 1)
  261. {
  262. q = a / m;
  263. t = m;
  264. m = a % m, a = t;
  265. t = x0;
  266. x0 = x1 - q * x0;
  267. x1 = t;
  268. }
  269. if (x1 < 0)
  270. x1 += m0;
  271. return x1;
  272. }
  273.  
  274.  
  275. int main()
  276. {
  277. ll i,j,t,k,l,m,n;
  278. cin>>t;
  279. while(t--)
  280. {
  281. cin>>m>>n;
  282. if(Fermat(m,n))
  283. cout<<"Yes\n";
  284. else
  285. cout<<"No\n";
  286. ll x,y;
  287. //k=Extended_Euclid(m,n,&x,&y);
  288. // cout<<x<<" "<<y<<" "<<Extended_Euclid(m,n,&x,&y)<<endl;
  289. }
  290. return 0;
  291. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement