SRM 390(1-250pt)
2013-12-28 02:36
330 查看
DIV1 250pt
题意:给定整数n和k,问最少需要多少个n连接在一起形成的新整数t,使得t是k的倍数。如果不能形成倍数,输出-1。k <= 10^5,n <= 10^9。
解法:不断增加连接的n的数量,如果新形成的数除以k的余数已经出现过,说明出现循环,说明该输出-1。否则,最多执行k次就能得到答案。所以,总的来说最多执行k次,可以直接暴力。
tag:brute-force
View Code
题意:给定整数n和k,问最少需要多少个n连接在一起形成的新整数t,使得t是k的倍数。如果不能形成倍数,输出-1。k <= 10^5,n <= 10^9。
解法:不断增加连接的n的数量,如果新形成的数除以k的余数已经出现过,说明出现循环,说明该输出-1。否则,最多执行k次就能得到答案。所以,总的来说最多执行k次,可以直接暴力。
tag:brute-force
// BEGIN CUT HERE /* * Author: plum rain * score : */ /* */ // END CUT HERE #line 11 "ConcatenateNumber.cpp" #include <sstream> #include <stdexcept> #include <functional> #include <iomanip> #include <numeric> #include <fstream> #include <cctype> #include <iostream> #include <cstdio> #include <vector> #include <cstring> #include <cmath> #include <algorithm> #include <cstdlib> #include <set> #include <queue> #include <bitset> #include <list> #include <string> #include <utility> #include <map> #include <ctime> #include <stack> using namespace std; #define clr0(x) memset(x, 0, sizeof(x)) #define clr1(x) memset(x, -1, sizeof(x)) #define pb push_back #define sz(v) ((int)(v).size()) #define all(t) t.begin(),t.end() #define zero(x) (((x)>0?(x):-(x))<eps) #define out(x) cout<<#x<<":"<<(x)<<endl #define tst(a) cout<<a<<" " #define tst1(a) cout<<#a<<endl #define CINBEQUICKER std::ios::sync_with_stdio(false) typedef vector<int> vi; typedef vector<string> vs; typedef vector<double> vd; typedef pair<int, int> pii; typedef long long int64; const double eps = 1e-8; const double PI = atan(1.0)*4; const int inf = 2139062143 / 2; class ConcatenateNumber { public: bool an[100005]; int getSmallest(int n, int k){ clr0 (an); int64 nn = n, tmp = n % k, ten = 1; while (n) ten *= 10, n /= 10; int cnt = 1; while (1){ if (tmp == 0) return cnt; if (an[tmp]) return -1; else an[tmp] = 1; tmp = (tmp * ten + nn)% k; ++ cnt; } } // BEGIN CUT HERE public: 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(); } private: 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(); } 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 << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } void test_case_0() { int Arg0 = 2; int Arg1 = 9; int Arg2 = 9; verify_case(0, Arg2, getSmallest(Arg0, Arg1)); } void test_case_1() { int Arg0 = 121; int Arg1 = 11; int Arg2 = 1; verify_case(1, Arg2, getSmallest(Arg0, Arg1)); } void test_case_2() { int Arg0 = 1; int Arg1 = 2; int Arg2 = -1; verify_case(2, Arg2, getSmallest(Arg0, Arg1)); } void test_case_3() { int Arg0 = 35; int Arg1 = 98765; int Arg2 = 9876; verify_case(3, Arg2, getSmallest(Arg0, Arg1)); } void test_case_4() { int Arg0 = 1000000000; int Arg1 = 3; int Arg2 = 3; verify_case(4, Arg2, getSmallest(Arg0, Arg1)); } // END CUT HERE }; // BEGIN CUT HERE int main() { // freopen( "a.out" , "w" , stdout ); ConcatenateNumber ___test; ___test.run_test(-1); return 0; } // END CUT HERE
View Code
相关文章推荐
- SRM 401(1-250pt, 1-500pt)
- SRM 441(1-250pt, 1-500pt)
- SRM 499 250pt
- SRM 402(1-250pt, 1-500pt)
- SRM 394(1-250pt)
- 【解题报告】 SRM412_div1_250pt 和 SRM411_div2_900pt
- SRM 406(1-250pt, 1-500pt)
- SRM 410(1-250pt, 1-500pt)
- SRM 403(1-250pt, 1-500pt)
- SRM 393(1-250pt)
- SRM 440(1-250pt, 1-500pt)
- SRM 447(1-250pt, 1-500pt)
- SRM 399(1-250pt)
- SRM 386(1-250pt)
- SRM 398(1-250pt)
- SRM 392(1-250pt)
- SRM 442(1-250pt, 1-500pt)
- SRM 407(1-250pt, 1-500pt)
- SRM 404(1-250pt, 1-500pt)
- SRM 397(1-250pt)