0w1

CFR 248 B. Chilly Willy ( Periodic, Observation )

Problem - 248B - Codeforces

題意:
給長度 n,求長度為 n 的最小的數字,可以整除 2, 3, 5, 7。若無解輸出 -1。

資料規模:
1 ≤ n ≤ 1e5

解法:
打表,發現規律,週期為 6。

時間 / 空間複雜度:
O( 1 )

/*#include <bits/stdc++.h>
using namespace std;

#define int long long

int get_len( int x ){
  int res = 0;
  while( x ){
    ++res;
    x /= 10;
  }
  return res;
}

signed main(){
  ios::sync_with_stdio( 0 );
  int len; cin >> len;
  if( len < 3 ) cout << -1 << endl;
  else{
    int z = 3;
    for( int i = 1; ; ++i ){
      if( get_len( i * 210 ) == z ){
        cout << z << " " << i * 210 << endl;
        ++z;
      }
      if( z == 15 ) break;
    }
  }
  cout << endl;
  return 0;
}
周期性を示すと推測:
3 210
4 1050
5 10080
6 100170
7 1000020
8 10000200
9 100000110
10 1000000050
11 10000000080
12 100000000170
*/

#include <bits/stdc++.h>
using namespace std;

signed main(){
  ios::sync_with_stdio( 0 );
  int len; cin >> len;
  if( len < 3 ) cout << -1 << endl;
  else if( len == 3 ) cout << 210 << endl;
  else{
    int f = ( len - 4 ) % 6;
    if( f <= 3 and f != 2 ){
      cout << 1;
      for( int i = 1; i < len - 2; ++i ){
        cout << 0;
      }
      if( f == 0 ) cout << 50 << endl;
      else if( f == 1 ) cout << 80 << endl;
      else if( f == 3 ) cout << 20 << endl;
    } else{
      cout << 1;
      for( int i = 1; i < len - 3; ++i ){
        cout << 0;
      }
      if( f == 2 ) cout << 170 << endl;
      else if( f == 4 ) cout << 200 << endl;
      else cout << 110 << endl;
    }
  }
  return 0;