結果

問題 No.25 有限小数
ユーザー A8pf
提出日時 2018-10-30 01:21:24
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
AC  
実行時間 2 ms / 5,000 ms
コード長 1,040 bytes
コンパイル時間 607 ms
コンパイル使用メモリ 58,992 KB
実行使用メモリ 5,248 KB
最終ジャッジ日時 2024-11-19 10:07:54
合計ジャッジ時間 1,589 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 31
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <iostream>
#include <vector>

using namespace std;
using ll=long long;

ll gcd(ll a, ll b)
{
    if(b==0)
        return a;
    else
        return gcd(b,a%b);
}

int main()
{
    ll n,m;
    ll ans=-1;
    cin>>n>>m;
    ll g=gcd(n,m);
    n/=g;m/=g;

    if(m==1)
    {
        while(n%10==0)
            n/=10;
        ans=n%10;
        if(ans<0)
            ans+=10;
    }else{
        ll cnt2=0;
        ll cnt5=0;
        while(m%2==0)
        {
            m/=2;
            cnt2++;
        }
        while(m%5==0)
        {
            m/=5;
            cnt5++;
        }
        if(m==1)
        {
            n%=(ll)1e12;
            for(ll i=0;i<cnt2;i++)
            {
                n*=5;
                n%=(ll)1e12;
            }
            for(ll i=0;i<cnt5;i++)
            {
                n*=2;
                n%=(ll)1e12;
            }
            while(n%10==0)
                n/=10;
            ans=n%10;
            if(ans<0)
                ans+=10;
        }
    }
    cout<<ans<<endl;
    return 0;
}
0