Advertisement
Guest User

G(kite)

a guest
Oct 31st, 2014
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.46 KB | None | 0 0
  1. /* Mir Shahriar Sabuj
  2.    CSE 2K10, KUET  */
  3.  
  4. #include <set>
  5. #include <map>
  6. #include <list>
  7. #include <cmath>
  8. #include <ctime>
  9. #include <deque>
  10. #include <queue>
  11. #include <stack>
  12. #include <cctype>
  13. #include <cstdio>
  14. #include <string>
  15. #include <vector>
  16. #include <cassert>
  17. #include <cstdlib>
  18. #include <cstring>
  19. #include <sstream>
  20. #include <iostream>
  21. #include <algorithm>
  22. #include <bitset>
  23. #include <functional>
  24. //#include <conio.h>
  25. using namespace std;
  26.  
  27. #define LL long long int
  28. #define LI long int
  29. #define FOR(i,a,b) for(int i=a; i<b; i++)
  30. #define PB push_back
  31. #define NL cout << endl
  32. #define PRINT(v) FOR(i, 0, v.size()){ cout <<v[i]<<" "; }NL;
  33. #define FUNC(name,v) name(v.begin(), v.end())
  34. #define CLEAR(v) v.erase(v.begin(), v.end())
  35. #define gcd(a,b)    __gcd(a,b)
  36. #define lcm(a,b) ((a)*((b)/gcd(a,b)))
  37. #define mset(x,with) memset(x,with,sizeof(x))
  38. #define PI 3.141592653589793
  39. #define CASE_print printf("Case %d: ", ++test_case)
  40.  
  41. int check_bit(int N,int POS){return (N & (1<<POS));}
  42. int on_bit(int N,int POS){return N=N | (1<<POS);}
  43. int off_bit(int N,int POS){return N=N & ~(1<<POS);}
  44. int row[4]={-1,0,0,1};
  45. int col[4]={0,-1,1,0};
  46.  
  47. ////////////////////////////////////////////////////////
  48.  
  49.  
  50. const int BUFSIZE = 1 << 16; char BUFFER[BUFSIZE + 5]; int REM = 0, POS = 0;
  51.  
  52. char GETCHAR()
  53. {if (REM <= 0){POS = 0; REM = fread(BUFFER, 1, BUFSIZE, stdin);
  54.  if (REM <= 0) return EOF;}
  55.  --REM; return BUFFER[POS++];
  56. }
  57.  
  58. void GETINT(int &VAL)
  59. {
  60.     VAL = 0; char C = EOF; while (C != '-' && !isdigit(C)) C = GETCHAR();
  61.     bool NEG = C == '-'; if (NEG) C = GETCHAR();
  62.     while (isdigit(C)) { VAL = VAL * 10 + C - '0'; C = GETCHAR(); }
  63.     if (NEG) VAL = -VAL;
  64. }
  65.  
  66.  
  67. ////////////////////////////////////////////////////////////
  68.  
  69.  
  70. LL bigmod(LL b, LL e, LL m)
  71. {
  72.     LL r = 1;
  73.     for( ; e; e >>= 1)
  74.     {
  75.         if(e & 1)
  76.           r = (r * b) % m;
  77.         b = b*b % m;
  78.     }
  79.     return r;
  80. }
  81.  
  82.  
  83. int main()
  84. {
  85.     //freopen("in.txt", "r", stdin);
  86.     //freopen("out.txt", "w", stdout);
  87.  
  88.    int n, test_case=0;
  89.    scanf("%d", &n);
  90.  
  91.  
  92.    while(n--)
  93.    {
  94.  
  95.        CASE_print;
  96.  
  97.         LL D, V;
  98.  
  99.        scanf("%lld %lld", &D, &V);
  100.  
  101.        if(V==0)
  102.        {
  103.            cout << -1;
  104.            NL;
  105.            continue;
  106.        }
  107.  
  108.        LL A = bigmod(V,  D+1, 1000000007);
  109.  
  110.  
  111.        if(D==0)
  112.         cout << V;
  113.        else if(V==1)
  114.        {
  115.         cout << D+1;
  116.        }
  117.        else
  118.          cout << (1-A)/(1-V);
  119.  
  120.        NL;
  121.  
  122.  
  123.    }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement