Problem:
循環小数
2 つの正の整数
p
,
q
を入力し、
p
/
q
を小数として正確に表現することを考えます。(ただし、0 <
p
<
q
< 10
6
とします。)
このとき、結果は
有限の桁で正確に表現できる。
ある桁の範囲を繰り返す循環小数となる。
のいずれかとなります。筆算と同じ手順で1桁ずつ小数部を求めていくと、
割り切れた(余りが 0 になった)なら、そこまでの桁で正確に表現できた。
1度出てきた余りが、再び現れたなら、循環した。
と区別できます。
2 つの整数
p
,
q
を入力すると、
p
/
q
を小数で表した時の、小数部を出力するプログラムを作成してください。 ただし、
結果が有限の桁で正確に表現できる時は、数値だけを 1 行に出力してください。
結果が循環小数になる時は次のように 2 行に出力してください。
最初の行に、循環する部分までの数字を出力してください。
次の行には、循環しない部分の下には空白を出力し、循環する部分の下には「^」を出力してください。
いずれの場合も数字列は 80 文字を超えることはないものとします。
Input
入力は複数のデータセットからなります。各データセットとして、
p
,
q
が空白区切りで１行に与えられます。データセットの数は 250 を超えません。
Output
データセットごとに、循環しない小数の場合は数値の小数部分を(この場合 1 行)、循環小数の場合は循環するまでの数字と循環する部分を示す記号「^」(この場合 2 行) を出力してください。
Sample Input
1 12
10000 32768
1 11100
1 459550
Output for the Sample Input
083
  ^
30517578125
00009
  ^^^
00000217604178
  ^^^^^^^^^^^^

Submitted solutions:

code_snippet: #include<bits/stdc++.h>
using namespace std;
int jun[10000010];
int main(){
    int p, q, cnt;
    while(cin >> p >> q){
        p = p%q*10;
        cnt = 0;
        memset(jun, -1, sizeof(jun));
        while(p != 0 && jun[p] == -1){
            cout << p/q;
            jun[p] = cnt;
            cnt++;
            p = p%q*10;
        }
        cout << endl;
        if(p != 0){
            for(int i=0;i<cnt;i++){
                if(i < jun[p]) cout << ' ';
                else cout << '^';
            }
            cout << endl;
        }
    }
    return(0);
}
accuracy: 1
time_ms: 630
memory_kb: 42040
final_rank: 20

code_snippet: #line 1 "library/my_template.hpp"
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using pi = pair<ll, ll>;
using vi = vector<ll>;
using u32 = unsigned int;
using u64 = unsigned long long;
using i128 = __int128;

template <class T>
using vc = vector<T>;
template <class T>
using vvc = vector<vc<T>>;
template <class T>
using vvvc = vector<vvc<T>>;
template <class T>
using vvvvc = vector<vvvc<T>>;
template <class T>
using vvvvvc = vector<vvvvc<T>>;
template <class T>
using pq = priority_queue<T>;
template <class T>
using pqg = priority_queue<T, vector<T>, greater<T>>;

#define vec(type, name, ...) vector<type> name(__VA_ARGS__)
#define vv(type, name, h, ...) \
  vector<vector<type>> name(h, vector<type>(__VA_ARGS__))
#define vvv(type, name, h, w, ...)   \
  vector<vector<vector<type>>> name( \
      h, vector<vector<type>>(w, vector<type>(__VA_ARGS__)))
#define vvvv(type, name, a, b, c, ...)       \
  vector<vector<vector<vector<type>>>> name( \
      a, vector<vector<vector<type>>>(       \
             b, vector<vector<type>>(c, vector<type>(__VA_ARGS__))))

// https://trap.jp/post/1224/
#define FOR1(a) for (ll _ = 0; _ < ll(a); ++_)
#define FOR2(i, a) for (ll i = 0; i < ll(a); ++i)
#define FOR3(i, a, b) for (ll i = a; i < ll(b); ++i)
#define FOR4(i, a, b, c) for (ll i = a; i < ll(b); i += (c))
#define FOR1_R(a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR2_R(i, a) for (ll i = (a)-1; i >= ll(0); --i)
#define FOR3_R(i, a, b) for (ll i = (b)-1; i >= ll(a); --i)
#define FOR4_R(i, a, b, c) for (ll i = (b)-1; i >= ll(a); i -= (c))
#define overload4(a, b, c, d, e, ...) e
#define FOR(...) overload4(__VA_ARGS__, FOR4, FOR3, FOR2, FOR1)(__VA_ARGS__)
#define FOR_R(...) \
  overload4(__VA_ARGS__, FOR4_R, FOR3_R, FOR2_R, FOR1_R)(__VA_ARGS__)

#define FOR_subset(t, s) for (ll t = s; t >= 0; t = (t == 0 ? -1 : (t - 1) & s))
#define all(x) x.begin(), x.end()
#define len(x) ll(x.size())
#define elif else if

#define eb emplace_back
#define mp make_pair
#define mt make_tuple
#define fi first
#define se second

#define stoi stoll

template <typename T, typename U>
T SUM(const vector<U> &A) {
  T sum = 0;
  for (auto &&a: A) sum += a;
  return sum;
}

#define MIN(v) *min_element(all(v))
#define MAX(v) *max_element(all(v))
#define LB(c, x) distance((c).begin(), lower_bound(all(c), (x)))
#define UB(c, x) distance((c).begin(), upper_bound(all(c), (x)))
#define UNIQUE(x) sort(all(x)), x.erase(unique(all(x)), x.end())

int popcnt(int x) { return __builtin_popcount(x); }
int popcnt(u32 x) { return __builtin_popcount(x); }
int popcnt(ll x) { return __builtin_popcountll(x); }
int popcnt(u64 x) { return __builtin_popcountll(x); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 1, 2)
int topbit(int x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(u32 x) { return (x == 0 ? -1 : 31 - __builtin_clz(x)); }
int topbit(ll x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
int topbit(u64 x) { return (x == 0 ? -1 : 63 - __builtin_clzll(x)); }
// (0, 1, 2, 3, 4) -> (-1, 0, 1, 0, 2)
int lowbit(int x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(u32 x) { return (x == 0 ? -1 : __builtin_ctz(x)); }
int lowbit(ll x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }
int lowbit(u64 x) { return (x == 0 ? -1 : __builtin_ctzll(x)); }

template <typename T>
T pick(deque<T> &que) {
  T a = que.front();
  que.pop_front();
  return a;
}

template <typename T>
T pick(pq<T> &que) {
  T a = que.top();
  que.pop();
  return a;
}

template <typename T>
T pick(pqg<T> &que) {
  assert(que.size());
  T a = que.top();
  que.pop();
  return a;
}

template <typename T>
T pick(vc<T> &que) {
  assert(que.size());
  T a = que.back();
  que.pop_back();
  return a;
}

template <typename T, typename U>
T ceil(T x, U y) {
  return (x > 0 ? (x + y - 1) / y : x / y);
}

template <typename T, typename U>
T floor(T x, U y) {
  return (x > 0 ? x / y : (x - y + 1) / y);
}

template <typename T, typename U>
pair<T, T> divmod(T x, U y) {
  T q = floor(x, y);
  return {q, x - q * y};
}

template <typename F>
ll binary_search(F check, ll ok, ll ng) {
  assert(check(ok));
  while (abs(ok - ng) > 1) {
    auto x = (ng + ok) / 2;
    tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x));
  }
  return ok;
}

template <typename F>
double binary_search_real(F check, double ok, double ng, int iter = 100) {
  FOR(iter) {
    double x = (ok + ng) / 2;
    tie(ok, ng) = (check(x) ? mp(x, ng) : mp(ok, x));
  }
  return (ok + ng) / 2;
}

template <class T, class S>
inline bool chmax(T &a, const S &b) {
  return (a < b ? a = b, 1 : 0);
}
template <class T, class S>
inline bool chmin(T &a, const S &b) {
  return (a > b ? a = b, 1 : 0);
}

vc<int> s_to_vi(const string &S, char first_char) {
  vc<int> A(S.size());
  FOR(i, S.size()) { A[i] = S[i] - first_char; }
  return A;
}

template <typename T, typename U>
vector<T> cumsum(vector<U> &A, int off = 1) {
  int N = A.size();
  vector<T> B(N + 1);
  FOR(i, N) { B[i + 1] = B[i] + A[i]; }
  if (off == 0) B.erase(B.begin());
  return B;
}

template <typename CNT, typename T>
vc<CNT> bincount(const vc<T> &A, int size) {
  vc<CNT> C(size);
  for (auto &&x: A) { ++C[x]; }
  return C;
}

// stable
template <typename T>
vector<int> argsort(const vector<T> &A) {
  vector<int> ids(A.size());
  iota(all(ids), 0);
  sort(all(ids),
       [&](int i, int j) { return A[i] < A[j] || (A[i] == A[j] && i < j); });
  return ids;
}

// A[I[0]], A[I[1]], ...
template <typename T>
vc<T> rearrange(const vc<T> &A, const vc<int> &I) {
  int n = len(I);
  vc<T> B(n);
  FOR(i, n) B[i] = A[I[i]];
  return B;
}
#line 1 "library/other/io2.hpp"
#define INT(...) \
  int __VA_ARGS__; \
  IN(__VA_ARGS__)
#define LL(...) \
  ll __VA_ARGS__; \
  IN(__VA_ARGS__)
#define STR(...) \
  string __VA_ARGS__; \
  IN(__VA_ARGS__)
#define CHR(...) \
  char __VA_ARGS__; \
  IN(__VA_ARGS__)
#define DBL(...) \
  long double __VA_ARGS__; \
  IN(__VA_ARGS__)

#define VEC(type, name, size) \
  vector<type> name(size);    \
  read(name)
#define VV(type, name, h, w)                     \
  vector<vector<type>> name(h, vector<type>(w)); \
  read(name)

void read(int &a) { cin >> a; }
void read(long long &a) { cin >> a; }
void read(char &a) { cin >> a; }
void read(double &a) { cin >> a; }
void read(long double &a) { cin >> a; }
void read(string &a) { cin >> a; }
template <class T, class S> void read(pair<T, S> &p) { read(p.first), read(p.second); }
template <class T> void read(vector<T> &a) {for(auto &i : a) read(i);}
template <class T> void read(T &a) { cin >> a; }
void IN() {}
template <class Head, class... Tail> void IN(Head &head, Tail &...tail) {
  read(head);
  IN(tail...);
}

template <typename T, typename U>
ostream& operator<<(ostream& os, const pair<T, U>& A) {
  os << A.fi << " " << A.se;
  return os;
}

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& A) {
  for (size_t i = 0; i < A.size(); i++) {
    if(i) os << " ";
    os << A[i];
  }
  return os;
}

void print() {
  cout << "\n";
  cout.flush();
}

template <class Head, class... Tail>
void print(Head&& head, Tail&&... tail) {
  cout << head;
  if (sizeof...(Tail)) cout << " ";
  print(forward<Tail>(tail)...);
}

void YES(bool t = 1) { print(t ? "YES" : "NO"); }
void NO(bool t = 1) { YES(!t); }
void Yes(bool t = 1) { print(t ? "Yes" : "No"); }
void No(bool t = 1) { Yes(!t); }
void yes(bool t = 1) { print(t ? "yes" : "no"); }
void no(bool t = 1) { yes(!t); }
#line 3 "main.cpp"

void solve() {
  ll p, q;
  while (cin >> p >> q) {
    vi pos(q, -1);
    string ANS;
    while (p) {
      if (pos[p] != -1) break;
      pos[p] = len(ANS);
      p *= 10;
      ANS += '0' + (p / q);
      p %= q;
    }
    int a = pos[p], b = len(ANS);
    print(ANS);
    if (p == 0) continue;
    for (auto&& x: ANS) x = ' ';
    FOR(i, a, b) ANS[i] = '^';
    print(ANS);
  }
}

signed main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  cout << setprecision(15);

  solve();

  return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 11292
final_rank: 14

code_snippet: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;

int main(){
    int p,q;
    while(cin >> p >> q){
        vector<int> rm(q, -1);
        string sr = "";
        int i = 0;
        while(rm[p]==-1 and p>0){
            rm[p] = i++;
            p *= 10;
            sr += '0'+p/q;
            p %= q;
        }
        cout << sr << endl;
        if(rm[p] != -1){
            cout << string(rm[p], ' ') +string((int)sr.length()-rm[p], '^') << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 7112
final_rank: 9

code_snippet: #include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<string>
#define lol(i,n) for(int i=0;i<n;i++)
#define mod 1000000007
typedef long long ll;
using namespace std;
int vist[1000010];
int main(){
  int a,b;
  while(cin>>a>>b){
    a%=b;
    for(int i=0;i<1000010;i++)vist[i]=-1;
    int len=0;
    while(a){
      if(vist[a]==-1)vist[a]=len;
      else break;
      //cout<<"#"<<a<<" ";
      a*=10;
      int tmp=a/b;
      a%=b;
      //cout<<a<<" "<<vist[a]<<" "<<tmp<<endl;
      len++;
      cout<<tmp;
    }
    cout<<endl;
    if(vist[a]==-1)continue;
    for(int i=0;i<len;i++){
      if(i<vist[a])cout<<" ";
      else cout<<"^";
    }
    cout<<endl;
  }
  return 0;
}
accuracy: 1
time_ms: 70
memory_kb: 6984
final_rank: 13

code_snippet: #include <stdio.h>
#include<math.h>
#include <algorithm>
using namespace std;

int main(){

	int p,q,start,end;
	int* table = new int[1000000];
	bool FLG;

	while(scanf("%d %d",&p,&q)!=EOF){
		for(int i=0; i < q;i++)table[i] = -1;

		FLG = false;

		table[p] = 0;

		for(int i=1;i<=80;i++){
			p = 10*p;
			printf("%d",p/q);
			p = p%q;
			if(p == 0)break;
			if(table[p] == -1){
				table[p] = i;
			}else{
				FLG = true;
				start = table[p];
				end = i-1;
				break;
			}
		}

		printf("\n");
		if(FLG){
			for(int i=0; i < start; i++)printf(" ");
			for(int i=start; i <= end; i++)printf("^");
			printf("\n");
		}

	}

    return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 6468
final_rank: 6

code_snippet: #include <bits/stdc++.h>

using namespace std;

int main()
{
  int p, q;
  while(cin >> p >> q){
    static int used[1000010];
    memset(used, -1, sizeof(used));
    string ans = "";
    string hat = "";
    int prevr = -1;
    for(int i = 0; ; i++){
      int r = p / q;
      int l = p % q;
      if(l == 0){
	ans += to_string(r);
	break;
      }
      /*
      else if(prevr == r && used[l] != -1) {
	for(int j = 0; j < i; j++){
	  if(j < used[l]) hat += " ";
	  else hat += "^";
	}
	break;
      }
      */
      else if(used[l] != -1){
	ans += to_string(r);
	for(int j = 0; j <= i; j++){
	  if(j <= used[l]) hat += " ";
	  else hat += "^";
	}
	break;
      } else {
	ans += to_string(r);
	p = l;
	prevr = r;
	used[l] = i;
      }
      p *= 10;
    }
    cout << ans.substr(1) << endl;
    if(hat.size()) cout << hat.substr(1) << endl;
  }
  return 0;
}
accuracy: 1
time_ms: 20
memory_kb: 7156
final_rank: 10

code_snippet: #include<bits/stdc++.h>
using namespace std;
int main(){
  int p,q,used[1000005];
  while(cin>>p>>q){
    for(int i=0;i<q;i++)used[i]=-1;
    vector<int>ans;
    int i=0,idx=-1;
    while(p){
      if(used[p]!=-1){idx=used[p];break;}
      used[p]=i;
      p*=10;
      ans.push_back(p/q);
      p%=q;
      i++;
    }
    for(int j=0;j<ans.size();j++)
      cout<<ans[j];
    cout<<endl;
    if(idx!=-1){
      for(int j=0;j<ans.size();j++){
	if(j<idx)cout<<" ";
	else cout<<"^";
      }
      cout<<endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 5124
final_rank: 5

code_snippet: #include "bits/stdc++.h"
#include<unordered_map>
#include<unordered_set>
#pragma warning(disable:4996)
using namespace std;
using ld = long double;
template<class T>
using Table = vector<vector<T>>;
const ld eps=1e-9;

//// < "D:\D_Download\Visual Studio 2015\Projects\programing_contest_c++\Debug\a.txt"
int main() {
	string st;
	while (getline(cin,st)) {
		long long int p = stoi(st.substr(0, st.find(" ")));
		long long int q = stoi(st.substr(st.find(" ") + 1));
		vector<int>vs;
		for (int i = 0; i < 10000; ++i) {
			p *= 10;
			vs.push_back(p / q);
			p %= q;
			if (!p)break;
		}
		if (vs.size() != 10000) {
			for (auto v : vs) {
				cout << v;
			}
			cout << endl;
		}
		else {
			int roop = 1;
			for (; roop <=80; ++roop) {
				bool flag = true;
				for (int i = 2000; i < 8000; ++i) {
					if (vs[i] == vs[i + roop]) {

					}
					else {
						flag = false;
					}
				}
				if (flag)break;
			}
			int start = 100;
			for (; start >= 0; --start) {
				if (vs[start] == vs[start + roop]) {

				}
				else {
					start;
					break;
				}
			}
			for (int i = 0; i <= start+roop; ++i) {
				cout << vs[i];
			}
			cout << endl;
			for (int i = 0; i <= start + roop; ++i) {
				if (i <=start)cout << " ";
				else cout << "^";
			}
			cout << endl;
		}
	}
	return 0;
}
accuracy: 1
time_ms: 30
memory_kb: 3300
final_rank: 4

code_snippet: #include <iostream>
using namespace std;
int p, q, visit[1000000];
int main() {
	while (cin >> p >> q) {
		for (int i = 0; i < q; i++) visit[i] = -1; visit[p] = 0;
		for (int i = 0; ; i++) {
			p *= 10, cout << p / q, p %= q;
			if (p == 0) {
				cout << endl; break;
			}
			if (visit[p] != -1) {
				cout << endl;
				for (int j = 0; j < visit[p]; j++) cout << ' ';
				for (int j = visit[p]; j <= i; j++) cout << '^';
				cout << endl; break;
			}
			visit[p] = i + 1;
		}
	}
}
accuracy: 1
time_ms: 10
memory_kb: 6900
final_rank: 7

code_snippet: #include <string>
#include <iostream>
using namespace std;
int p, q, visit[1000000];
int main() {
	while (cin >> p >> q) {
		string s;
		for (int i = 0; i < q; i++) visit[i] = -1; visit[p] = 0;
		for (int i = 0; ; i++) {
			p *= 10, s += p / q + 48, p %= q;
			if (p == 0) {
				cout << s << endl; s = "-1"; break;
			}
			if (visit[p] != -1) {
				cout << s << endl;
				for (int j = 0; j < visit[p]; j++) cout << ' ';
				for (int j = visit[p]; j <= i; j++) cout << '^';
				cout << endl; break;
			}
			visit[p] = i + 1;
		}
	}
}
accuracy: 1
time_ms: 10
memory_kb: 7012
final_rank: 8

code_snippet: #include<iostream>
#include<cstring>
using namespace std;

long long p, a, cnt;
long long x[100000];

int main() {
	while (cin >> p >> a) {
		cnt = 0;
		memset(x, 0, sizeof(x));
		while (true) {
			if (cnt >= 1) { cout << p / a; }
			if (p%a == 0) { break; }
			p *= 10;
			p %= (a * 10);
			x[cnt] = p;
			cnt++;
			for (int i = 0; i < cnt - 1; i++) {
				if (p == x[i]) {
					cout << endl;
					for (int j = 1; j <= i; j++) {
						cout << ' ';
					}
					for (int j = i + 1; j < cnt; j++) {
						cout << '^';
					}
					goto E;
				}
			}
		}
	E:;
		cout << endl;
	}
	return 0;
}
accuracy: 1
time_ms: 10
memory_kb: 1944
final_rank: 2

code_snippet: #include <iostream>
using namespace std;

int main() {
    int p, q;
    while (cin >> p >> q) {
        int prev[1000010];
        int hist[1000010];
        int ind = 0;

        fill(prev, prev + 1000010, -1);
        fill(hist, hist + 1000010, -1);

        while (true) {
            prev[p] = ind;
            hist[ind++] = 10 * p / q;
            p = 10 * p % q;
            if (p == 0 || prev[p] >= 0) {
                break;
            }
        }

        for (int j = 0; j < ind; ++j) {
            cout << hist[j];
        }
        cout << endl;
        if (p != 0) {
            for (int j = 0; j < prev[p]; ++j) {
                cout << ' ';
            }
            for (int j = prev[p]; j < ind; ++j) {
                cout << '^';
            }
            cout << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 150
memory_kb: 8968
final_rank: 16

code_snippet: #include "bits/stdc++.h"

using namespace std;

typedef pair<int,int> pii;
typedef vector<int> vi;

#define dump(x)  cerr << #x << " = " << (x) << endl
#define rep(i,n) for(int i=0;i<(n);i++)
#define all(a)  (a).begin(),(a).end()
#define pb push_back
#define INF 999999999


int fin(vector<pii> vec,int x){
    rep(i,vec.size()){
        if(vec[i].first==x)return vec[i].second;
    }
    return -1;
}

int main(){
    int a,b;
    while(cin>>a>>b){
        static vector<pii> used[1000001];
        rep(i,1000001)used[i].clear();
        string ans="";
        int c=0;
        a*=10;
        while(1){
            int x=a/b;
            int y=a%b;
//            cout<<x<<" "<<y<<endl;
            if(y==0){ans+='0'+x;cout<<ans<<endl;break;}
            int res=-1;
            if( used[y].size() && (res = fin(used[y],x))!=-1 ){
                cout<<ans<<endl;
                rep(i,res)cout<<" ";
                rep(i,ans.size()-res)cout<<'^';
                cout<<endl;
                break;
            }
            ans+='0'+x;
            used[y].pb(pii(x,c));
            a-=b*x;
            c++;
            a*=10;
        }
    }
    
}
accuracy: 1
time_ms: 650
memory_kb: 24656
final_rank: 18

code_snippet: #include<cstdio>
#include<cstring>

int p,q;
int data[85];
int encount[10000000];
int prev[90];
int main(void){
	while(~scanf("%d%d",&p,&q)){
		memset(encount,0,sizeof(encount));
		memset(data,0,sizeof(data));
		for(int i=1;i<=80;i++){
			p*=10;
			if(encount[p]==0)encount[p]=i,prev[i]=p;
			else if(encount[p]!=0){
				prev[i]=p;
				for(int j=1;j<i;j++)printf("%d",data[j]);
				printf("\n");
				for(int j=1;j<i;j++)printf("%c",(encount[p]<=j && j<i)?'^':' ');
				printf("\n");
				break;
			}
			data[i]=p/q;
			p=p%q;
			if(p==0){
				for(int j=1;j<=i;j++)printf("%d",data[j]);
				printf("\n");
				break;
			}
		}
	}
	return 0;
}
accuracy: 1
time_ms: 480
memory_kb: 40096
final_rank: 19

code_snippet: #include <iostream>
#include <sstream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <ctime>
#include <cstring>
#include <string>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <map>
#include <set>
#include <bitset>
#include <numeric>
#include <utility>
#include <iomanip>
#include <algorithm>
#include <functional>
using namespace std;

typedef long long ll;
typedef vector<int> vint;
typedef vector<long long> vll;
typedef pair<int,int> pint;
typedef pair<long long, long long> pll;

#define MP make_pair
#define PB push_back
#define ALL(s) (s).begin(),(s).end()
#define EACH(i, s) for (__typeof__((s).begin()) i = (s).begin(); i != (s).end(); ++i)
#define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }
template<class T1, class T2> ostream& operator << (ostream &s, pair<T1,T2> P) 
{ return s << '<' << P.first << ", " << P.second << '>'; }
template<class T> ostream& operator << (ostream &s, vector<T> P) 
{ for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; }
template<class T> ostream& operator << (ostream &s, vector<vector<T> > P) 
{ for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; }
template<class T1, class T2> ostream& operator << (ostream &s, map<T1,T2> P) 
{ EACH(it, P) { s << "<" << it->first << "->" << it->second << "> "; } return s; }



const int DEFAULT_SIZE = 125;
struct Bigint : vector<long long> {
    static const long long BASE = 100000000;
    static const int BASE_DIGIT = 8;
    int sign;
    
    Bigint() : vector<long long>(1, 0) { sign = 1; }
    Bigint(long long num) : vector<long long>(DEFAULT_SIZE, 0) {
        sign = 1; if (num < 0) sign = -1;
        (*this)[0] = abs(num); 
        (*this).normalize();
    }
    Bigint(int SIZE, long long num) : vector<long long>(SIZE, 0) {
        sign = 1; if (num < 0) sign = -1;
        (*this)[0] = abs(num); 
        (*this).normalize();
    }
    
    Bigint& normalize() {
        long long c = 0;
        for (int i = 0;; ++i) {
            if (i >= (*this).size()) (*this).push_back(0);
            if ((*this)[i] < 0 && i+1 >= (*this).size()) (*this).push_back(0);
            while ((*this)[i] < 0) { (*this)[i+1] -= 1; (*this)[i] += BASE; }
            long long a = (*this)[i] + c;
            (*this)[i] = a % BASE;
            c = a / BASE;
            if (c == 0 && i == (*this).size()-1) break;
        }
        return (*this);
    }
    
    inline Bigint operator - () { 
        Bigint res = (*this); 
        bool allzero = true; for (int i = 0; i < res.size(); ++i) if (res[i] != 0) { allzero = false; break; }
        if (!allzero) res.sign *= -1; 
        return res; 
    }
    inline const Bigint& operator += (const Bigint &x);
    inline const Bigint& operator -= (const Bigint &x);
    inline const Bigint& operator *= (long long x);
    inline const Bigint& operator *= (const Bigint &x);
    inline const Bigint& operator /= (long long x);
    inline const Bigint& operator /= (const Bigint &x);
    inline const Bigint& operator %= (long long x);
    inline const Bigint& operator %= (const Bigint &x);
};

inline Bigint abs(const Bigint &x) {
    Bigint z = x;
    if (z.sign == -1) z.sign = 1;
    return z;
}
inline Bigint conv(string s) {
    Bigint res = 0;
    for (int i = 0; i < s.size(); ++i) {
        res += (long long)(s[i] - '0');
        if (i != s.size()-1) res *= 10;
    }
    return res;
}
ostream &operator << (ostream &os, Bigint x) {
    if (x.sign == -1) os << '-';
    int d = x.size()-1; 
    for (d = x.size()-1; d >= 0; --d) if (x[d] > 0) break;
    if (d == -1) os << 0;
    else os << x[d];
    for (int i = d-1; i >= 0; --i) { os.width(Bigint::BASE_DIGIT); os.fill('0'); os << x[i]; }
    return os;
}
istream &operator >> (istream &is, Bigint &x) {
    string s; is >> s;
    x = conv(s);
    return is;
}
inline bool operator > (Bigint x, Bigint y) {
    x.normalize(); y.normalize();
    if (x.sign > y.sign) return true;
    else if (x.sign < y.sign) return false;
    else if (x.sign == 1) {
        for (int i = x.size()-1; i >= 0; --i) if (x[i] != y[i]) return x[i] > y[i];
        return false;
    }
    else {
        for (int i = x.size()-1; i >= 0; --i) if (x[i] != y[i]) return x[i] < y[i];
        return false;
    }
}
inline bool operator < (Bigint x, Bigint y) { return y > x; }
inline bool operator <= (Bigint x, Bigint y) { return !(y < x); }
inline bool operator >= (Bigint x, Bigint y) { return !(x < y); }
inline bool operator != (Bigint x, Bigint y) { return x < y || y < x; }
inline bool operator == (Bigint x, Bigint y) { return !(x < y) && !(y < x); }

inline Bigint operator + (Bigint x, Bigint y) {
    while (x.size() < y.size()) x.push_back(0);
    while (x.size() > y.size()) y.push_back(0);
    Bigint z((int)x.size(), 0);
    if (x.sign == y.sign) {
        z.sign = x.sign;
        for (int i = 0; i < x.size(); ++i) z[i] = x[i] + y[i];
    }
    else {
        if (x.sign == -1) swap(x, y);
        if (x >= -y) { z.sign = 1; for (int i = 0; i < x.size(); ++i) z[i] = x[i] - y[i]; }
        else { z.sign = -1; for (int i = 0; i < x.size(); ++i) z[i] = y[i] - x[i]; }
    }
    return z.normalize();
} 
inline Bigint operator - (Bigint x, Bigint y) {
    y = -y;
    return x + y;
}
inline Bigint operator * (Bigint x, long long a) {
    Bigint z((int)x.size(), 0);
    if ( (x.sign == 1 && a >= 0) || (x.sign == -1 && a < 0) ) z.sign = 1;
    else z.sign = -1;
    a = abs(a);
    for (int i = 0; i < x.size(); ++i) z[i] = x[i] * a;
    return z.normalize();
}
inline Bigint operator * (Bigint x, Bigint y) {
    int tx = x.size()-1, ty = y.size()-1; 
    for (tx = x.size()-1; tx >= 0; --tx) if (x[tx] > 0) break;
    for (ty = y.size()-1; ty >= 0; --ty) if (y[ty] > 0) break;
    Bigint z(tx+ty+5, 0);
    if (x.sign == y.sign) z.sign = 1;
    else z.sign = -1;
    for (int i = 0; i <= tx; ++i) {
        for (int j = 0; j <= ty && i+j < z.size()-1; ++j) {
            long long val = x[i] * y[j] + z[i+j];
            z[i+j+1] += val / Bigint::BASE;
            z[i+j] = val % Bigint::BASE;
        }
    }
    return z.normalize();
}
pair<Bigint, long long> divmod(Bigint x, long long a) {
    long long c = 0, t = 0;
    for (int i = x.size()-1; i >= 0; --i) {
        t = Bigint::BASE * c + x[i];
        x[i] = t / a;
        c = t % a;
    }
    x.normalize();
    return pair<Bigint, long long>(x, c);
}
Bigint operator / (Bigint x, long long a) {
    return divmod(x, a).first;
}
long long operator % (Bigint x, long long a) {
    return divmod(x, a).second;
}
pair<Bigint, Bigint> divmod(Bigint x, Bigint y) {
    Bigint zero = 0;
    if (abs(x) < abs(y)) return pair<Bigint, Bigint>(zero, x);
    Bigint ay = abs(y), q((int)x.size(), 0), r((int)y.size(), 0);
    int tx = x.size()-1; 
    for (tx = x.size()-1; tx >= 0; --tx) if (x[tx] > 0) break;
    for (int i = tx; i >= 0; --i) {
        r = r * Bigint::BASE + x[i];
        long long lo = 0, hi = Bigint::BASE;
        if (r >= ay) {
            while (hi - lo > 1) {
                int mid = (hi + lo) / 2;
                if (ay * mid > r) hi = mid;
                else lo = mid;
            }
            r = r - ay * lo;
        }
        q[i] = lo;
    }
    if (x.sign == -1 || y.sign == -1) q.sign = -1, r.sign = -1;
    return make_pair(q.normalize(), r.normalize());
}
Bigint operator / (Bigint x, Bigint y) {
    return divmod(x, y).first;
}
Bigint operator % (Bigint x, Bigint y) {
    return divmod(x, y).second;
}
inline Bigint pow(Bigint a, long long n) {
    Bigint res = 1;
    while (n > 0) { if (n & 1) { res = res * a; } a = a * a; n >>= 1; }
    return res;
}
inline const Bigint& Bigint::operator += (const Bigint &x) {*this = *this + x; return *this;}
inline const Bigint& Bigint::operator -= (const Bigint &x) {*this = *this - x; return *this;}
inline const Bigint& Bigint::operator *= (long long x) {*this = *this * x; return *this;}
inline const Bigint& Bigint::operator *= (const Bigint &x) {*this = *this * x; return *this;}
inline const Bigint& Bigint::operator /= (long long x) {*this = *this / x; return *this;}
inline const Bigint& Bigint::operator /= (const Bigint &x) {*this = *this / x; return *this;}
inline const Bigint& Bigint::operator %= (long long x) {*this = *this % x; return *this;}
inline const Bigint& Bigint::operator %= (const Bigint &x) {*this = *this % x; return *this;}

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

long long ta, b;

int main() {
    while (cin >> ta >> b) {
        long long d = gcd(ta, b);
        ta /= d;
        b /= d;
        
        Bigint a = ta;
        string res = "";
        a *= 10;
        while (a < b) {
            res += "0";
            a *= 10;
        }
        int zero2 = 0, zero5 = 0;
        int b2 = b;
        while (b2 % 2 == 0) { b2 /= 2; ++zero2; }
        while (b2 % 5 == 0) { b2 /= 5; ++zero5; }
        int zero = max(zero2, zero5);
        
        if (b2 == 1) {
            for (int i = 0; i < 1000; ++i) a *= 10;
            Bigint q = a / b;
            stringstream sout; sout << q;
            res += sout.str();
            while (res[res.size()-1] == '0') res = res.substr(0, res.size()-1);
            cout << res << endl;
        }
        else {
            int con = 0;
            long long tot = 1;
            for (int i = 0; i < 1000; ++i) {
                tot *= 10;
                tot %= b2;
                ++con;
                if (tot == 1) break;
            }
        
            for (int i = 0; i < 1000; ++i) a *= 10;
            Bigint q = a / b;
        
            stringstream sout; sout << q;
            res += sout.str();
        
            res = res.substr(0, zero + con);
            string res2 = "";
            for (int i = 0; i < zero; ++i) res2 += ' ';
            for (int i = 0; i < con; ++i) res2 += '^';
            
            cout << res << endl;
            cout << res2 << endl;
        }
    }
    return 0;
}
accuracy: 1
time_ms: 260
memory_kb: 1280
final_rank: 15

code_snippet: #include<iostream>
#include<algorithm>
using namespace std;
int n,m,a,b,c,d;
int ans[10000000];
int flg[10000000];
int main(){
  for(int i=0;i<=10000000;i++)flg[i]=-1;
  while(cin>>n>>m){
    for(int i=0;i<=d;i++)flg[i]=-1;
    
    n=n%m;
    d=c=-1;
    for(int i=0;i<=10000000;i++){
      n*=10;
      if(flg[n]!=-1){
	c=i;break;
      }
      flg[n]=i;d=max(d,n);
      ans[i]=n/m;
      cout<<ans[i];
      n=n%m;
      if(n==0)break;
    }
    cout<<endl;
    if(c!=-1){
      for(int i=1;i<=c;i++){
	if(i<=flg[n]){
	  cout<<' ';
	}else{
	  cout<<'^';
	}
      }
    cout<<endl;
    }
  }
  return 0;
}
accuracy: 1
time_ms: 80
memory_kb: 42116
final_rank: 17

code_snippet: #include <iostream>
using namespace std;

int main() {
  int p, q;
  while(cin >> p >> q) {
    bool num[1000000] = {}; num[p] = 1;
    int pos[1000000] = {}; pos[p] = -1;
    bool loop = 0;
    int cnt = 0;
    string t;
    while(1) {
      p *= 10;
      t += (char)(p/q + '0');
      p %= q;
      if(num[p] == 1) {
	loop = 1;
	break;
      }
      num[p] = 1;
      if(p == 0) break;
      pos[p] = cnt++;
    }
    
    cout << t << endl;
    
    if(loop) {
      int I;
      for(I=0; I<=pos[p]; I++) {
	cout << ' ';
      }
      for(I; I<t.size(); I++) {
	cout << '^';
      }
      cout << endl;
    }
  }
  
  return 0;
}
accuracy: 1
time_ms: 60
memory_kb: 6088
final_rank: 12

code_snippet: #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <iomanip>
#include <map>
#include <fstream>
#include <sstream>
#include <math.h>
#include <queue>
#include <stack>

using namespace std;


int main(){
    int p,q;
    while(cin>>p>>q){
        
        
        bool amari[1000000]={0};
        
        string s;

        bool flag = false;
        
        queue<int> qu;
        while(1){
            p=p%q*10;
            if(p==0)break;
            //cout<<p<<endl;
            if(amari[p/10]){
                flag=true;
                break;
            }
            amari[p/10]=true;
            qu.push(p/10);
            
            s+=p/q+'0';
        }
        cout<<s<<endl;
        if(flag){
            flag=true;
            while(!qu.empty()){
                if(qu.front()==p/10)flag=false;
                if(flag)cout<<" ";
                else cout<<"^";
                qu.pop();
            }
            cout<<endl;
        }
    }
       
}
accuracy: 1
time_ms: 10
memory_kb: 2184
final_rank: 3

code_snippet: #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <string>
using namespace std;
int md[1111111];
int main(void){
  int p,q;
  while(cin >> p >> q){
    memset(md,-1,sizeof(md));
    int n,m=-1;
    for(n = 0; p%q; n++){
      if(md[p%q]!=-1){
	m = md[p%q];
	break;
      }
      else md[p%q] = n;
      p = (p%q) * 10;
      cout << p/q;
    }
    cout << endl;
    if(m != -1){
      for(int i = 0; i < n; i++){
	cout << (i<m?' ':'^');
      }
      cout << endl;
    }
  }
}
accuracy: 1
time_ms: 60
memory_kb: 5508
final_rank: 11

code_snippet: #include<stdio.h>
int last[1000000];
int main(){
	int a,b;
	while(~scanf("%d%d",&a,&b)){
		for(int i=0;i<b;i++)last[i]=-1;
		int now=a;
		last[now]=0;
		int at=0;
		while(1){
			now*=10;
			printf("%d",now/b);
			if(~last[now%b]){
				printf("\n");
				for(int i=0;i<last[now%b];i++)printf(" ");
				for(int i=last[now%b];i<=at;i++)printf("^");
				printf("\n");
				break;
			}
			now%=b;
			last[now]=++at;
			if(now==0){
				printf("\n");
				break;
			}
		}
	}
}
accuracy: 1
time_ms: 20
memory_kb: 0
final_rank: 1