Advertisement
Guest User

Untitled

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