Problem p02596 (C++)

Buggy submission (status: Time Limit Exceeded):
/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <cstdint>   // int64_t, int*_t
#include <iomanip>
#include <iostream> // cout, endl, cin
#include <limits>   //setprecision
#include <string>   // string, to_string, stoi
#include <tuple>    // tuple, make_tuple
#include <utility>  // pair, make_pair
#include <vector>   // vector
// #include <cstdio> // printf
#include <map>   // map
#include <queue> // queue, priority_queue
#include <set>   // set
// #include <unordered_set> //unordered_set
#include <bitset> // bitset
#include <cmath>  //abs,,,
#include <deque>  // deque
#include <math.h> //pow,,,
#include <stack>  // stack
#define endl "\n";
using namespace std;

const long long INF = 100100100100100;
const int MOD = 1000000007;
const int inf = 1001001001;
typedef pair<int, int> P;

// Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M) {
  if (M == 0)
    return 1;
  long long res = mod_pow((N * N) % MOD, M / 2);
  // 最下位ビット(*N)が1の時は単独でNをかける
  if (M & 1)
    res = (res * N) % MOD;

  return res %= MOD;
}

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

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  long long K;
  cin >> K;
  if (K % 2 == 0) {
    cout << -1 << endl;
    return 0;
  }
  long long ans = 0;
  long long amari = 0;
  while (true) {
    ans++;
    amari = amari * 10 + 7;
    amari %= K;
    if (amari % K == 0)
      break;
  }
  cout << ans << endl;
}

Fixed submission:
/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <cstdint>   // int64_t, int*_t
#include <iomanip>
#include <iostream> // cout, endl, cin
#include <limits>   //setprecision
#include <string>   // string, to_string, stoi
#include <tuple>    // tuple, make_tuple
#include <utility>  // pair, make_pair
#include <vector>   // vector
// #include <cstdio> // printf
#include <map>   // map
#include <queue> // queue, priority_queue
#include <set>   // set
// #include <unordered_set> //unordered_set
#include <bitset> // bitset
#include <cmath>  //abs,,,
#include <deque>  // deque
#include <math.h> //pow,,,
#include <stack>  // stack
#define endl "\n";
using namespace std;

const long long INF = 100100100100100;
const int MOD = 1000000007;
const int inf = 1001001001;
typedef pair<int, int> P;

// Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M) {
  if (M == 0)
    return 1;
  long long res = mod_pow((N * N) % MOD, M / 2);
  // 最下位ビット(*N)が1の時は単独でNをかける
  if (M & 1)
    res = (res * N) % MOD;

  return res %= MOD;
}

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

int main() {
  ios::sync_with_stdio(false);
  std::cin.tie(nullptr);

  long long K;
  cin >> K;
  if (K % 2 == 0 || K % 5 == 0) {
    cout << -1 << endl;
    return 0;
  }
  long long ans = 0;
  long long amari = 0;
  while (true) {
    ans++;
    amari = amari * 10 + 7;
    amari %= K;
    if (amari % K == 0)
      break;
  }
  cout << ans << endl;
}