結果
問題 |
No.1339 循環小数
|
ユーザー |
![]() |
提出日時 | 2021-01-15 22:45:10 |
言語 | C++14 (gcc 13.3.0 + boost 1.87.0) |
結果 |
WA
|
実行時間 | - |
コード長 | 1,852 bytes |
コンパイル時間 | 911 ms |
コンパイル使用メモリ | 100,624 KB |
実行使用メモリ | 6,824 KB |
最終ジャッジ日時 | 2024-11-26 16:46:35 |
合計ジャッジ時間 | 16,739 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 1 |
other | AC * 4 WA * 32 |
ソースコード
#include <iostream> #include <algorithm> #include <string> #include <vector> #include <cmath> #include <map> #include <queue> #include <iomanip> #include <set> #include <tuple> #define mkp make_pair #define mkt make_tuple #define rep(i,n) for(int i = 0; i < (n); ++i) #define all(v) v.begin(),v.end() using namespace std; typedef long long ll; const ll MOD=1e9+7; template<class T> void chmin(T &a,const T &b){if(a>b) a=b;} template<class T> void chmax(T &a,const T &b){if(a<b) a=b;} ll mod_pow(ll a,ll n,ll mod){ a%=mod; ll res=1; while(n>0){ if(n&1) res=res*a%mod; a=a*a%mod; n>>=1; } return res; } // gcd(a,mod) = 1 ll mod_inv(ll a,ll mod){ ll b=mod,u=1,v=0; while(b){ ll t=a/b; a-=t*b;swap(a,b); u-=t*v;swap(u,v); } u%=mod; if(u<0) u+=mod; return u; } // minimum x for a^x = b (mod) // gcd(a,mod) = 1 // x > 0 [x >= 0] ll mod_log(ll a,ll b,ll mod){ a%=mod;b%=mod; ll sqm=sqrt(mod)+1; map<ll,ll> apow; ll val=1; for(int r=0;r<sqm;r++){ if(apow.count(val)==false) apow[val]=r; val=val*a%mod; } ll A=mod_pow(mod_inv(a,mod),sqm,mod); ll target=b; for(int q=0;q<=sqm;q++){ if(apow.count(target)){ ll res=sqm*q+apow[target]; if(res>0) return res; //return res; } target=target*A%mod; } return -1; } void solve(){ ll N; cin>>N; if(10%N==100%N&&10%N!=1){ cout<<1<<"\n"; return; } ll base=10; if(N%2==0){ while(N%2==0) N/=2; //N/=2; base/=2; } if(N%5==0){ while(N%5==0) N/=5; //N/=5; base/=5; } //cout<<base<<" "<<N<<"\n"; ll ans=mod_log(base,1,N); cout<<ans<<"\n"; } int main(){ cin.tie(0); ios::sync_with_stdio(false); int T; cin>>T; rep(i,T) solve(); return 0; }