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

#include <bits/stdc++.h>
#define ll long long int
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define vi vector<int>
#define vvi vector<vector<int>>
#define mii map<int, int>
#define pb push_back
#define pii pair<int, int>
#define mkp make_pair
#define scan(a, n)                                                             \\
  for (int i = 0; i < n; i++)                                                  \\
  cin >> a[i]
#define print(a, n)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
  cout << a[i] << ' '
#define mem(a, v) memset(a, v, sizeof(a))
#define loop(i, a, n) for (int i = a; i < n; i++)
#define FastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define PRECISION                                                              \\
  std::cout.unsetf(std::ios::floatfield);                                      \\
  std::cout.precision(15)
double PI = acos(-1.0);
using namespace std;
int mod = 1000000007;
int inf = INT_MAX;
int minf = INT_MIN;

/*int main()
{
    int x;
    cin>>x;
    if(x>=30)
        cout<<"Yes";
    else
        cout<<"No";
}*/

/*int main()
{
    ll n,d;
    cin>>n>>d;
    int cnt=0;
    while(n--)
    {
        ll x,y;
        cin>>x>>y;
        ll temp=x*x+y*y;
        ll ans=ceil(sqrt(temp));
        //cout<<ans<<endl;
        if(ans<=d)
            cnt++;
    }
    cout<<cnt;
}*/

int main() {
  int k;
  cin >> k;
  if (k % 2 == 0 || k % 5 == 0) {
    cout << "-1";
    return 0;
  }
  int first = 7, cnt = 1;
  while (k > first) {
    first = first * 10 + 7;
    cnt++;
  }
  while (first > 0) {
    first = first % k;
    if (first > 0) {
      first = first * 10 + 7;
      cnt++;
    }
  }
  cout << cnt;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define ll long long int
#define max3(a, b, c) max(a, max(b, c))
#define min3(a, b, c) min(a, min(b, c))
#define vi vector<int>
#define vvi vector<vector<int>>
#define mii map<int, int>
#define pb push_back
#define pii pair<int, int>
#define mkp make_pair
#define scan(a, n)                                                             \\
  for (int i = 0; i < n; i++)                                                  \\
  cin >> a[i]
#define print(a, n)                                                            \\
  for (int i = 0; i < n; i++)                                                  \\
  cout << a[i] << ' '
#define mem(a, v) memset(a, v, sizeof(a))
#define loop(i, a, n) for (int i = a; i < n; i++)
#define FastIO                                                                 \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL)
#define PRECISION                                                              \\
  std::cout.unsetf(std::ios::floatfield);                                      \\
  std::cout.precision(15)
double PI = acos(-1.0);
using namespace std;
int mod = 1000000007;
int inf = INT_MAX;
int minf = INT_MIN;

/*int main()
{
    int x;
    cin>>x;
    if(x>=30)
        cout<<"Yes";
    else
        cout<<"No";
}*/

/*int main()
{
    ll n,d;
    cin>>n>>d;
    int cnt=0;
    while(n--)
    {
        ll x,y;
        cin>>x>>y;
        ll temp=x*x+y*y;
        ll ans=ceil(sqrt(temp));
        //cout<<ans<<endl;
        if(ans<=d)
            cnt++;
    }
    cout<<cnt;
}*/

int main() {
  int k;
  cin >> k;
  if (k % 2 == 0) {
    cout << "-1";
    return 0;
  }
  int first = 7, cnt = 1;
  while (k > first) {
    first = first * 10 + 7;
    cnt++;
  }
  while (first > 0) {
    first = first % k;
    if (first > 0) {
      first = first * 10 + 7;
      cnt++;
    }
  }
  cout << cnt;
}