Advertisement
hkshakib

Untitled

Mar 17th, 2020
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3. const int mx = 10000025;
  4. const long long mod = 1000000007;
  5. int prime[mx], totient[mx], lp[mx], f[mx], facto[mx];
  6. long long power(long long x, long long y)
  7. {
  8. long long res = 1;
  9. x = x % mod;
  10. while (y > 0)
  11. {
  12. if (y & 1)
  13. res = (res * x) % mod;
  14. y = y >> 1;
  15. x = (x * x) % mod;
  16. }
  17. return res%mod;
  18. }
  19. void isPrime()
  20. {
  21. for(int i = 2; i < mx; i ++)
  22. {
  23. if(!prime[i])
  24. {
  25. lp[i] = i;
  26. for(int j = i + i; j < mx; j += i)
  27. {
  28. prime[j] = 1;
  29. lp[j] = i;
  30. }
  31. }
  32. for(int i = 2; i < mx; i ++)
  33. {
  34. prime[i] = prime[i - 1] + 1 - prime[i];
  35. }
  36. }
  37.  
  38. }
  39. void phi()
  40. {
  41. totient[1] = 1;
  42. for(int i = 2; i < mx; i ++)
  43. {
  44. int j = i / lp[i];
  45. if(j % lp[i] == 0)
  46. {
  47. totient[i] = totient[j] * lp[i];
  48. }
  49. else
  50. {
  51. totient[i] = totient[j] * (lp[i] - 1);
  52. }
  53. }
  54. for(int i = 1; i < mx; i ++){
  55. f[i] = prime[i] - totient[i];
  56. if(f[i] < 0){
  57. f[i] = 0;
  58. }
  59. }
  60. }
  61.  
  62. void modfactot()
  63. {
  64. facto[0] = 1;
  65. for(int i = 1; i < mx; i ++){
  66. facto[i] = 1LL * facto[i - 1] * i % (mod - 1);
  67. }
  68. }
  69. int main()
  70. {
  71. isPrime();
  72. phi();
  73. modfactot();
  74. int t;
  75. cin>>t;
  76. while(t--)
  77. {
  78. int n;
  79. cin>>n;
  80. cout<<power(totient[n],facto[f[n]])<<endl;
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement