Advertisement
jeff69

Untitled

Aug 13th, 2016
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.65 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. /*
  3. WAY 2 C_M:: M_F
  4. */
  5. using namespace std;
  6. typedef long long ll;
  7. typedef long double ld;
  8. typedef short sh;
  9. const ld pi=acos(-1);
  10. const ld eps =1e-7;
  11. const int MX=1e5+3;
  12. ll gcdExtended(ll a, ll b, ll *x, ll *y);
  13.  
  14. // Function to find modulo inverse of a
  15. ll modInverse(ll a, ll m)
  16. {
  17. ll x, y;
  18. ll g = gcdExtended(a, m, &x, &y);
  19. if (g != 1);
  20. //cout << 0<<endl;
  21. else
  22. {
  23. // m is added to handle negative x
  24. ll res = (x%m + m) % m;
  25. return res;
  26. }
  27. }
  28.  
  29. // C function for extended Euclidean Algorithm
  30. ll gcdExtended(ll a, ll b, ll *x, ll *y)
  31. {
  32. // Base Case
  33. if (a == 0)
  34. {
  35. *x = 0, *y = 1;
  36. return b;
  37. }
  38.  
  39. ll x1, y1; // To store results of recursive call
  40. ll gcd = gcdExtended(b%a, a, &x1, &y1);
  41.  
  42. // Update x and y using results of recursive
  43. // call
  44. *x = y1 - (b/a) * x1;
  45. *y = x1;
  46.  
  47. return gcd;
  48. }
  49. const int MD=1e9+7;
  50. int p;
  51. ll M(int x)
  52. {
  53. if(x==0)return 1;
  54. ll ans;
  55. ans=M(x/2)%MD;
  56. ans*=ans;
  57. ans%=MD;
  58. if(x%2)ans*=p;
  59. ans%=MD;
  60.  
  61.  
  62. return ans ;
  63.  
  64. }
  65. int main()
  66. {
  67. int t;
  68. cin>>t;
  69. while(t--){
  70. int x,n,z;
  71. cin>>n>>z;
  72. if(n==0)
  73. {
  74. cout<<0<<endl;
  75. continue;
  76. }
  77.  
  78. x=1;
  79. int temp=z;
  80. while(temp)
  81. {
  82. temp/=10;
  83. x*=10;
  84. }
  85. //cout<<x<<endl;
  86. ll a = x-1, m = 1e9+7;
  87. ll b=modInverse(a, m);
  88.  
  89. //cout<<b<< ' ';
  90. p=x;
  91. ll s=M(n);
  92. s--;
  93. s+=MD;
  94. s%=MD;
  95. // cout<<s<<endl;
  96.  
  97. s*=b;
  98. s%=MD;
  99. s*=z;
  100. s%=MD;
  101. cout<<s<<endl;}
  102. return 0;
  103. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement