Problem p02596 (C++) — buggy submission, verdict: Time Limit Exceeded


// Problem : C - Repsept
// Contest : AtCoder - AtCoder Beginner Contest 174
// URL : https://atcoder.jp/contests/abc174/tasks/abc174_c
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

// Macros

#define ll long long
#define db long double
#define pb push_back
#define eb emplace_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define mod 1000000007
#define EPS 1e-9
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define inf 1e10
#define PI acos(-1.0)
#define int long long
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define N 100005

// Solution

void solve() {
  int k;
  cin >> k;
  if (k % 2 == 0) {
    cout << -1 << endl;
    return;
  }
  int cnt = 1, cur = 0;
  while (cnt <= k) {
    cur *= 10;
    cur += 7;
    cur %= k;
    if (cur == 0)
      break;
    cnt++;
  }
  if (cnt == k + 1)
    cout << -1;
  else
    cout << cnt;
}

signed main() {
  io;
  // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
  int tests;
  // cin>>tests;
  tests = 1;
  while (tests--) {
    solve();
  }
  return 0;
}

Error: TLE

Fixed submission:


// Problem : C - Repsept
// Contest : AtCoder - AtCoder Beginner Contest 174
// URL : https://atcoder.jp/contests/abc174/tasks/abc174_c
// Memory Limit : 1024 MB
// Time Limit : 2000 ms
// Powered by CP Editor (https://github.com/cpeditor/cpeditor)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

// Macros

#define ll long long
#define db long double
#define pb push_back
#define eb emplace_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define fi first
#define se second
#define all(a) (a).begin(), (a).end()
#define sz(x) (int)x.size()
#define mod 1000000007
#define EPS 1e-9
#define io                                                                     \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define inf 1e10
#define PI acos(-1.0)
#define int long long
template <typename T>
using ordered_set =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
#define N 100005

// Solution

void solve() {
  int k;
  cin >> k;
  if (k % 2 == 0) {
    cout << -1 << endl;
    return;
  }
  int cnt = 1, cur = 0;
  while (true) {
    cur *= 10;
    cur += 7;
    cur %= k;
    if (cur == 0)
      break;
    cnt++;
  }
  cout << cnt;
}

signed main() {
  io;
  // freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout);
  int tests;
  // cin>>tests;
  tests = 1;
  while (tests--) {
    solve();
  }
  return 0;
}