题意:定义一个数为k-smooth,如果它最大的质因子不超过k。给定n和k,求不超过n的,k-smooth的数有多少个。(k <= 100, n <= 10^5)
解法:对于一个数t,判断它是不是k-smooth的,只需要2~k试除一遍即可。所以,暴力即可,O(k*n)。
tag:水题
1 // BEGIN CUT HERE 2 /* 3 * Author: plum rain 4 * score : 5 */ 6 /* 7 8 */ 9 // END CUT HERE 10 #line 11 "SmoothNumbers.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 SmoothNumbers 59 { 60 public: 61 int gao(int n, int k) 62 { 63 for (int i = k; i > 1; -- i) 64 while (!(n % i)) n /= i; 65 if (n <= k) return 1; 66 return 0; 67 } 68 69 int countSmoothNumbers(int n, int k){ 70 int cnt = 0; 71 for (int i = 1; i <= n; ++ i) 72 if (gao(i, k)) ++ cnt; 73 return cnt; 74 } 75 76 // BEGIN CUT HERE 77 public: 78 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(); } 79 //void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_0();} 80 private: 81 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(); } 82 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; } } 83 void test_case_0() { int Arg0 = 10; int Arg1 = 3; int Arg2 = 7; verify_case(0, Arg2, countSmoothNumbers(Arg0, Arg1)); } 84 void test_case_1() { int Arg0 = 10; int Arg1 = 4; int Arg2 = 7; verify_case(1, Arg2, countSmoothNumbers(Arg0, Arg1)); } 85 void test_case_2() { int Arg0 = 15; int Arg1 = 3; int Arg2 = 8; verify_case(2, Arg2, countSmoothNumbers(Arg0, Arg1)); } 86 void test_case_3() { int Arg0 = 5; int Arg1 = 20; int Arg2 = 5; verify_case(3, Arg2, countSmoothNumbers(Arg0, Arg1)); } 87 void test_case_4() { int Arg0 = 100000; int Arg1 = 100; int Arg2 = 17442; verify_case(4, Arg2, countSmoothNumbers(Arg0, Arg1)); } 88 89 // END CUT HERE 90 91 }; 92 93 // BEGIN CUT HERE 94 int main() 95 { 96 // freopen( "a.out" , "w" , stdout ); 97 SmoothNumbers ___test; 98 ___test.run_test(-1); 99 return 0; 100 } 101 // END CUT HERE