51 Nod 1035 最長的迴圈節 (此題還不是很懂,日後再看)
轉自:
51nod 1035 最長的迴圈節(無限小數的迴圈節)
2016年09月07日 09:53:13 單木 閱讀數:867更多
版權宣告:本文為博主原創文章,歡迎轉載。 https://blog.csdn.net/define_danmu_primer/article/details/52456763
定理
- 如果1<=b<a1<=b<a,a沒有2或5的質因子,並且a與b互質,那麼b/ab/a 的迴圈節位數恰好等於min(10e≡1(moda))min(10e≡1(moda)),e是正整數。
- 如果1<=b<a1<=b<a,a沒有2或5的質因子,並且a與b互質,那麼b/ab/a 的迴圈節位數必整除ϕ(a)ϕ(a)。
程式碼
/* ******************************** Author : danmu Created Time : 2016年09月07日 星期三 09時01分10秒 File Name : a.cpp ******************************** */
#include <algorithm> #include <iostream> #include <cstdlib> #include <cstring> #include <iomanip> #include <string> #include <vector> #include <cstdio> #include <stack> #include <queue> #include <cmath> #include <list> #include <map> #include <set>
#define rep(i,x,y) for(int i=x;i<=y;++i) #define _rep(i,x,y) for(int i=x;i>=y;--i) #define CL(S,x) memset(S,x,sizeof(S)) #define CP (S1,S2) memcpy(S1,S2,sizeof(S2)) #define ALL(x,S) for(x=S.begin();x!=S.end();++x) #define ULL unsigned long long #define PI 3.1415926535 #define INF 0x3f3f3f3f #define LL long long
const int maxn = 1e3+5; const int mod = 1e9 + 7; const double eps = 1e-8;
using namespace std; int main(){ //freopen("in.txt", "r", stdin); //freopen("out.txt", "w", stdout); int n,ans=1,maxx=0; scanf("%d",&n); for(int i=2;i<=n;++i){ int tmp=i,tmp1=1,tmp2=0; while(tmp%2==0) tmp/=2; while(tmp%5==0) tmp/=5; if(tmp==1) tmp2=0; else{ do{ tmp1=tmp1*10%tmp; ++tmp2; }while(tmp1!=1); } //printf("%d\n",tmp2); if(tmp2>maxx){ maxx=tmp2; ans=i; } } printf("%d\n",ans); return 0; }
另一種模擬除法的做法如下:
#include<iostream> #include<string.h> using namespace std; int d[1000000];
int fun(int x){ int k=1,l=1; memset(d,0,sizeof(d)); while(1){ k*=10; k%=x; d[k]++; l++; if(d[k]==2||k==0) break; } return l; } int main(){ int n; cin>>n; int maxx=0,max=0; for(int i=1;i<=n;i++){ if(fun(i)>maxx){ maxx=fun(i); max=i; } } cout<<max<<endl; return 0; }