a53

rogvaiv

a53
Mar 30th, 2017
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. #include <fstream>
  2. #include <cstring>
  3. #define MOD 1000000007
  4. #define Nmax 1000001
  5. using namespace std;
  6. ifstream f("rogvaiv.in");
  7. ofstream g("rogvaiv.out");
  8.  
  9. typedef int NrMare[Nmax];
  10. NrMare n;
  11.  
  12. long long Divide(NrMare x,long long n)
  13. ///x = x /n, returneaza x%n
  14. {
  15. long long i,r=0;
  16. for(i=x[0];i>0;i--)
  17. {
  18. r=10*r+x[i];
  19. x[i]=r/n;
  20. r%=n;
  21. }
  22. for(;x[x[0]]==0 && x[0]>1;)
  23. x[0]--;
  24. return r;
  25. }
  26.  
  27. /// Ridicam la putere in timp logaritmic, folosind:
  28. /// 7^n = 7^(n/2) * 7^(n/2), n%2==0 OR 2^(n-1)*2, n%2==1
  29.  
  30. long long PutereLog(int baza,long long n)
  31. {
  32. ///Calculam puterea cu modulo
  33. if(n==0)
  34. return 1;
  35.  
  36. if(n%2==0)
  37. {
  38. long long rezultat=PutereLog(baza,n/2);
  39. return (rezultat*rezultat)%MOD;
  40. }
  41. else
  42. {
  43. return (PutereLog(baza,n-1)*baza)%MOD;
  44. }
  45. }
  46.  
  47. int main()
  48. {
  49. char s[Nmax];
  50. f.getline(s,Nmax); /// Il citim pe n ca sir de caractere
  51. int nc=strlen(s); /// Numarul de cifre ale lui n
  52. for(int i=1;i<=nc;++i) /// Punem cifrele din s in n in ordine inersa (lucram cu numere mari)
  53. n[i]=s[nc-i]-'0';
  54. n[0]=nc;
  55. long long rest=Divide(n,1000000006);
  56. g<<PutereLog(7,rest);
  57. return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment