a53

perioada

a53
Aug 29th, 2017
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.49 KB | None | 0 0
  1. // prof. Ionel Vasile Pit-Rada
  2. // sursa O(sqrt(n)+sqrt(fi(n))*log(fi(n))
  3. // Utilizeaza algoritmul de ridicare la putere in timp logaritmic - 100 puncte
  4.  
  5. #include<fstream>
  6. using namespace std;
  7. ifstream fin("perioada.in");
  8. ofstream fout("perioada.out");
  9.  
  10. long long N;
  11. int p10(int b, int MOD){
  12. long long p=1,a=10;
  13. while(b){
  14. if(b%2==1){
  15. p=(p*a)%MOD;
  16. }
  17. b=b/2;
  18. a=(a*a)%MOD;
  19. }
  20. return p;
  21. }
  22.  
  23. int main(){
  24. long long fi,x,d,p,perioada;
  25. int c;
  26. fin>>N;
  27. fi=N;
  28. x=N;
  29.  
  30. c=0;
  31. while(x%2==0){
  32. x=x/2;
  33. c++;
  34. }
  35. if(c){
  36. fi=fi/2;
  37. }
  38. d=3;
  39. while(d*d<=x){
  40. c=0;
  41. while(x%d==0){
  42. x=x/d;
  43. c++;
  44. }
  45. if(c){
  46. fi=fi/d*(d-1);
  47. }
  48. d=d+2;
  49. }
  50. if(x>1){
  51. fi=fi/x*(x-1);
  52. }
  53. perioada=0;
  54. for(d=1;d*d<fi;d++){
  55. if(fi%d==0){
  56. p=p10(d,N);
  57. if(p==1){
  58. perioada=d;
  59. break;
  60. }
  61. }
  62. }
  63. if(d*d==fi){
  64. p=p10(d,N);
  65. if(p==1){
  66. perioada=d;
  67. }
  68. }
  69. if(!perioada){
  70. for(d=d-1;d>=1;d--){
  71. if(fi%d==0){
  72. p=p10(fi/d,N);
  73. if(p==1){
  74. perioada=fi/d;
  75. break;
  76. }
  77. }
  78. }
  79. }
  80. fout<<perioada;
  81. fout.close();
  82. fin.close();
  83. return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment