zoukankan      html  css  js  c++  java
  • SRM 390(1-250pt)

    DIV1 250pt

    题意:给定整数n和k,问最少需要多少个n连接在一起形成的新整数t,使得t是k的倍数。如果不能形成倍数,输出-1。k <= 10^5,n <= 10^9。

    解法:不断增加连接的n的数量,如果新形成的数除以k的余数已经出现过,说明出现循环,说明该输出-1。否则,最多执行k次就能得到答案。所以,总的来说最多执行k次,可以直接暴力。

    tag:brute-force

      1 // BEGIN CUT HERE
      2 /*
      3  * Author:  plum rain
      4  * score :
      5  */
      6 /*
      7 
      8  */
      9 // END CUT HERE
     10 #line 11 "ConcatenateNumber.cpp"
     11 #include <sstream>
     12 #include <stdexcept>
     13 #include <functional>
     14 #include <iomanip>
     15 #include <numeric>
     16 #include <fstream>
     17 #include <cctype>
     18 #include <iostream>
     19 #include <cstdio>
     20 #include <vector>
     21 #include <cstring>
     22 #include <cmath>
     23 #include <algorithm>
     24 #include <cstdlib>
     25 #include <set>
     26 #include <queue>
     27 #include <bitset>
     28 #include <list>
     29 #include <string>
     30 #include <utility>
     31 #include <map>
     32 #include <ctime>
     33 #include <stack>
     34 
     35 using namespace std;
     36 
     37 #define clr0(x) memset(x, 0, sizeof(x))
     38 #define clr1(x) memset(x, -1, sizeof(x))
     39 #define pb push_back
     40 #define sz(v) ((int)(v).size())
     41 #define all(t) t.begin(),t.end()
     42 #define zero(x) (((x)>0?(x):-(x))<eps)
     43 #define out(x) cout<<#x<<":"<<(x)<<endl
     44 #define tst(a) cout<<a<<" "
     45 #define tst1(a) cout<<#a<<endl
     46 #define CINBEQUICKER std::ios::sync_with_stdio(false)
     47 
     48 typedef vector<int> vi;
     49 typedef vector<string> vs;
     50 typedef vector<double> vd;
     51 typedef pair<int, int> pii;
     52 typedef long long int64;
     53 
     54 const double eps = 1e-8;
     55 const double PI = atan(1.0)*4;
     56 const int inf = 2139062143 / 2;
     57 
     58 class ConcatenateNumber
     59 {
     60     public:
     61         bool an[100005];
     62         int getSmallest(int n, int k){
     63             clr0 (an);
     64             int64 nn = n, tmp = n % k, ten = 1;
     65             while (n)
     66                 ten *= 10, n /= 10;
     67             int cnt = 1;
     68             while (1){
     69                 if (tmp == 0) return cnt;
     70                 if (an[tmp]) return -1;
     71                 else an[tmp] = 1;
     72 
     73                 tmp = (tmp * ten + nn)% k;
     74                 ++ cnt;
     75             }
     76         }
     77         
     78 // BEGIN CUT HERE
     79     public:
     80     void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0(); if ((Case == -1) || (Case == 1)) test_case_1(); if ((Case == -1) || (Case == 2)) test_case_2(); if ((Case == -1) || (Case == 3)) test_case_3(); if ((Case == -1) || (Case == 4)) test_case_4(); }
     81     private:
     82     template <typename T> string print_array(const vector<T> &V) { ostringstream os; os << "{ "; for (typename vector<T>::const_iterator iter = V.begin(); iter != V.end(); ++iter) os << '"' << *iter << "","; os << " }"; return os.str(); }
     83     void verify_case(int Case, const int &Expected, const int &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "	Expected: "" << Expected << '"' << endl; cerr << "	Received: "" << Received << '"' << endl; } }
     84     void test_case_0() { int Arg0 = 2; int Arg1 = 9; int Arg2 = 9; verify_case(0, Arg2, getSmallest(Arg0, Arg1)); }
     85     void test_case_1() { int Arg0 = 121; int Arg1 = 11; int Arg2 = 1; verify_case(1, Arg2, getSmallest(Arg0, Arg1)); }
     86     void test_case_2() { int Arg0 = 1; int Arg1 = 2; int Arg2 = -1; verify_case(2, Arg2, getSmallest(Arg0, Arg1)); }
     87     void test_case_3() { int Arg0 = 35; int Arg1 = 98765; int Arg2 = 9876; verify_case(3, Arg2, getSmallest(Arg0, Arg1)); }
     88     void test_case_4() { int Arg0 = 1000000000; int Arg1 = 3; int Arg2 = 3; verify_case(4, Arg2, getSmallest(Arg0, Arg1)); }
     89 
     90 // END CUT HERE
     91 
     92 };
     93 
     94 // BEGIN CUT HERE
     95 int main()
     96 {
     97 //    freopen( "a.out" , "w" , stdout );    
     98     ConcatenateNumber ___test;
     99     ___test.run_test(-1);
    100        return 0;
    101 }
    102 // END CUT HERE
    View Code
  • 相关阅读:
    go语言之行--简介与环境搭建
    Django Rest Framework源码剖析(八)-----视图与路由
    基于TLS证书手动部署kubernetes集群(下)
    多线程编程
    Java IO流
    java异常处理
    字符串处理(二)
    字符串处理(一)
    正则表达式(应用)
    集合相关知识
  • 原文地址:https://www.cnblogs.com/plumrain/p/srm_390.html
Copyright © 2011-2022 走看看