結果

問題 No.25 有限小数
ユーザー どらら
提出日時 2015-06-09 01:49:06
言語 C++11(廃止可能性あり)
(gcc 13.3.0)
結果
TLE  
実行時間 -
コード長 1,679 bytes
コンパイル時間 719 ms
コンパイル使用メモリ 96,412 KB
実行使用メモリ 10,016 KB
最終ジャッジ日時 2024-07-06 14:57:49
合計ジャッジ時間 12,600 ms
ジャッジサーバーID
(参考情報)
judge3 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 18 TLE * 1 -- * 12
権限があれば一括ダウンロードができます
コンパイルメッセージ
main.cpp: In function ‘int main()’:
main.cpp:62:20: warning: ‘mx’ may be used uninitialized [-Wmaybe-uninitialized]
   62 |     res1[it->first]-=mx;

ソースコード

diff #

#include <algorithm>
#include <cstdio>
#include <cstdlib>
#include <cctype>
#include <cmath>
#include <iostream>
#include <queue>
#include <list>
#include <map>
#include <unordered_map>
#include <numeric>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i,a,n) for(int i=(a); i<(int)(n); i++)
#define rep(i,n) REP(i,0,n)
#define FOR(it,c) for(__typeof((c).begin()) it=(c).begin(); it!=(c).end(); ++it)
#define ALLOF(c) (c).begin(), (c).end()
typedef long long ll;
typedef unsigned long long ull;

void factorize(unordered_map<ull,int>& ret, ull x){
  ull d, q;
  while(x>=4 && x%2==0){ret[2]++; x/=2;}
  d = 3; q = x/d;
  while(q>=d){
    if(x%d==0){ ret[d]++; x=q; }else{ d+=2; }
    q=x/d;
  }
  ret[x]++;
}

ll mod_pow(ll x, ll n, ll mod){
  ll res = 1;
  while(n>0){
    if(n&1) res = res*x%mod;
    x = x*x%mod;
    n >>= 1;
  }
  return res;
}

int main(){
  ull N, M;
  cin >> N;
  cin >> M;

  int mx, mn;
  unordered_map<ull,int> res1, res2;
  factorize(res1, N);
  factorize(res2, M);

  if(res2.count(2) == 0 && res2.count(5) == 0){
    cout << -1 << endl;
    return 0;
  }
  int cnt = 0;
  FOR(it,res2){
    mn = min(res1[it->first], res2[it->first]);
    res1[it->first]-=mx;
    res2[it->first]-=mx;
    cnt += res2[it->first];
  }

  if(cnt - res2[2] - res2[5] > 0){
    cout << -1 << endl;
    return 0;
  }
  
  mx = max(res2[2],res2[5]);
  res1[2]+=mx;
  res1[5]+=mx;
  res1[2]-=res2[2];
  res1[5]-=res2[5];
  mn = min(res1[2], res1[5]);
  res1[2]-=mn;
  res1[5]-=mn;

  ull ret = 1;
  FOR(it,res1){
    ret *= mod_pow(it->first, it->second, 10);
    ret %= 10;
  }
  cout << ret << endl;
  
  return 0;
}
0