結果

問題 No.1339 循環小数
ユーザー rogi52rogi52
提出日時 2021-01-15 22:19:56
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
WA  
実行時間 -
コード長 1,432 bytes
コンパイル時間 1,518 ms
コンパイル使用メモリ 165,116 KB
実行使用メモリ 10,624 KB
最終ジャッジ日時 2024-05-05 00:18:39
合計ジャッジ時間 8,858 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
10,624 KB
testcase_01 AC 2 ms
5,376 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 2 ms
5,376 KB
testcase_09 AC 3 ms
5,376 KB
testcase_10 AC 2 ms
5,376 KB
testcase_11 AC 25 ms
5,376 KB
testcase_12 AC 28 ms
5,376 KB
testcase_13 AC 20 ms
5,376 KB
testcase_14 AC 22 ms
5,376 KB
testcase_15 AC 23 ms
5,376 KB
testcase_16 AC 21 ms
5,376 KB
testcase_17 AC 29 ms
5,376 KB
testcase_18 AC 18 ms
5,376 KB
testcase_19 AC 19 ms
5,376 KB
testcase_20 AC 17 ms
5,376 KB
testcase_21 WA -
testcase_22 WA -
testcase_23 WA -
testcase_24 WA -
testcase_25 WA -
testcase_26 WA -
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 WA -
testcase_33 WA -
testcase_34 TLE -
testcase_35 -- -
testcase_36 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
#define rep(i,n) for(int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

void junkan2(int n)
{
    int m = 1; // カメ。剰余を保持する変数(初期値1)
    int p = 1; // ウサギ。剰余を保持する変数(初期値1)
    int s = 0; // 循環節の開始位置
    int t = 0; // 循環節の終了位置
  
    if (n > 0)
    {
        // ウサギとカメが出合う地点を探す
        while (true)
        {
            m = (m * 10) % n;
            p = (p * 10) % n;
            p = (p * 10) % n;
            if (m == p)break;
        }
  
        // pが非ゼロ。割り切れない場合
        if (p != 0)
        {
            // ウサギをスタート地点に戻し、再びカメと出合うまでループを回す
            p = 1;
            s = 1;
            while (m != p)
            {
                s++;
                m = (m * 10) % n;
                p = (p * 10) % n;
            }
            // カメを止めて、ウサギだけ1ずつ進める
            p = (p * 10) % n;
            t = s;
            while (m != p)
            {
                t++;
                p = (p * 10) % n;
            }
        }
    }
    printf("%d\n", abs(s - t) + 1); // 結果を出力
}


int main(){
    cin.tie(0);
    ios::sync_with_stdio(0);

    int T; cin >> T;
    while(T--){
        int n; cin >> n;
        junkan2(n);
    }
}
0