DIV1 250pt
题意:将一个数表示成质因子相乘的形式,若乘式所含数字的个数为质数,则称A为underprime。比如12 = 2*2*3,则含3个数字,是underprime。求A, B之间underprime的个数。A, B <= 10^5。
解法:暴力枚举A,B之间所有数,求出其乘式所含数字的个数, 判断是不是质数。
tag:brute-force
1 // BEGIN CUT HERE 2 /* 3 4 */ 5 // END CUT HERE 6 #line 7 "Underprimes.cpp" 7 #include <cstdlib> 8 #include <cctype> 9 #include <cstring> 10 #include <cstdio> 11 #include <cmath> 12 #include <algorithm> 13 #include <vector> 14 #include <iostream> 15 #include <sstream> 16 #include <set> 17 #include <queue> 18 #include <fstream> 19 #include <numeric> 20 #include <iomanip> 21 #include <bitset> 22 #include <list> 23 #include <stdexcept> 24 #include <functional> 25 #include <string> 26 #include <utility> 27 #include <map> 28 #include <ctime> 29 #include <stack> 30 31 using namespace std; 32 33 #define clr0(x) memset(x, 0, sizeof(x)) 34 #define clr1(x) memset(x, -1, sizeof(x)) 35 #define pb push_back 36 #define mp make_pair 37 #define sz(v) ((int)(v).size()) 38 #define out(x) cout<<#x<<":"<<(x)<<endl 39 #define tst(a) cout<<#a<<endl 40 #define CINBEQUICKER std::ios::sync_with_stdio(false) 41 42 typedef vector<int> VI; 43 typedef vector<string> VS; 44 typedef vector<double> VD; 45 typedef long long int64; 46 47 const double eps = 1e-8; 48 const double PI = atan(1.0)*4; 49 const int inf = 2139062143 / 2; 50 51 inline int MyMod( int a , int b ) { return (a%b+b)%b;} 52 int temp[] = {2,3,5,7,11,13,17,19}; 53 54 class Underprimes 55 { 56 public: 57 bool gao(int x) 58 { 59 int cnt = 0; 60 for (int i = 2; i*i <= x; ++ i) if (!(x % i)){ 61 while (!(x % i)) x /= i, ++ cnt; 62 } 63 if (x != 1) ++ cnt; 64 for (int i = 0; i < 8; ++ i) if (cnt == temp[i]) return 1; 65 return 0; 66 } 67 int howMany(int A, int B){ 68 int ret = 0; 69 for (int i = B; i >= A; -- i) 70 if (gao(i)) ++ ret; 71 return ret; 72 } 73 74 // BEGIN CUT HERE 75 public: 76 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(); } 77 private: 78 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(); } 79 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; } } 80 void test_case_0() { int Arg0 = 2; int Arg1 = 10; int Arg2 = 5; verify_case(0, Arg2, howMany(Arg0, Arg1)); } 81 void test_case_1() { int Arg0 = 100; int Arg1 = 105; int Arg2 = 2; verify_case(1, Arg2, howMany(Arg0, Arg1)); } 82 void test_case_2() { int Arg0 = 17; int Arg1 = 17; int Arg2 = 0; verify_case(2, Arg2, howMany(Arg0, Arg1)); } 83 void test_case_3() { int Arg0 = 123; int Arg1 = 456; int Arg2 = 217; verify_case(3, Arg2, howMany(Arg0, Arg1)); } 84 85 // END CUT HERE 86 87 }; 88 //by plum rain 89 // BEGIN CUT HERE 90 int main() 91 { 92 //freopen( "a.out" , "w" , stdout ); 93 Underprimes ___test; 94 ___test.run_test(-1); 95 return 0; 96 } 97 // END CUT HERE
DIV1 550pt
题意:用1*5的瓷砖将房间铺成如下图形状,现在给一个矩形的区域,重新铺里面的瓷砖。买一个1*5的瓷砖,可以将其切割成几小块使用,但不能将几小块合并成一个1*5的使用。问要重新铺所给区域瓷砖,最少买多少块1*5的瓷砖。
解法:分两步,第一步求出,给定区域内,含有1*5,1*4,1*3,1*2,1*1各多少块。这一步只能暴力求,不同的写法上可能有编码复杂度的差异,我觉得我的还行,就是效率不太高。。
第二步,求出各种块的数量之后,求最少买多少块瓷砖。贪心思想,方法是从大往小扫,如果有1*4的,每块都配一个1*1的只要有,如果有1*3的尽量配1*2,不行就配1*1。。。。就是从大往小扫,匹配能匹配到的最大的瓷砖。这样的匹配方法在SRM 598 DIV1的250pt里面有用到。
tag:brute-force, greedy
1 // BEGIN CUT HERE 2 /* 3 * Author: plum rain 4 * score : 5 */ 6 /* 7 8 */ 9 // END CUT HERE 10 #line 11 "BedroomFloor.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 BedroomFloor 59 { 60 public: 61 int64 num[10]; 62 63 int64 min(int64 a, int64 b) 64 { 65 return a > b ? b : a; 66 } 67 68 int64 gao(int64 sam, int64 s, int64 e, int64 len) 69 { 70 int64 tim = e/5 - s/5 - 1; 71 if (tim < 0){ 72 if (sam) num[len] += e - s + 1; 73 else num[e-s+1] += len; 74 return 0; 75 } 76 77 int64 ret = 0; 78 if (len == 5) ret = tim * 5; 79 else{ 80 num[len] += (tim + (sam^1)) / 2 * 5; 81 ret = (tim + sam) / 2 * len; 82 } 83 84 int64 up = (s/5 + 1) * 5; 85 if (!sam) num[up-s] += len; 86 else num[len] += up - s; 87 88 sam ^= (tim + 1) & 1; 89 int64 dn = e / 5 * 5 - 1; 90 if (!sam) num[e-dn] += len; 91 else num[len] += e - dn; 92 93 return ret; 94 } 95 96 long long numberOfSticks(int x1, int y1, int x2, int y2){ 97 clr0 (num); 98 int64 ret = 0; 99 for (int i = x1; i < x2;){ 100 ret += gao(((i/5) & 1) == ((y1/5) & 1), y1, y2-1, min(5 - (i%5), x2-i)); 101 i = i / 5 * 5 + 5; 102 } 103 104 //for (int i = 1; i < 6; ++ i) 105 //tst(i), out (num[i]); 106 // 107 ret += num[5]; 108 if (num[4] && num[1]){ 109 int64 tmp = min(num[4], num[1]); 110 ret += tmp; 111 num[4] -= tmp; num[1] -= tmp; 112 } 113 ret += num[4]; 114 115 if (num[3] && num[2]){ 116 int64 tmp = min(num[3], num[2]); 117 ret += tmp; 118 num[3] -= tmp; num[2] -= tmp; 119 } 120 if (num[3] && num[1]){ 121 int64 tmp = min(num[1]/2, num[3]); 122 ret += tmp; 123 num[1] -= 2*tmp; num[3] -= tmp; 124 } 125 ret += num[3]; 126 num[1] -= num[3] * 2; 127 128 if (num[2] && num[1] > 0){ 129 int64 tmp = min(num[2]/2, num[1]); 130 ret += tmp; 131 num[2] -= tmp*2; num[1] -= tmp; 132 } 133 if (num[2] > 1) ret += num[2]&1 ? num[2]/2+1 : num[2]/2; 134 else if (num[2] == 1) ret += 1, num[1] -= 3; 135 136 if (num[1] > 0) ret += num[1]%5 ? num[1]/5+1 : num[1]/5; 137 return ret; 138 } 139 140 // BEGIN CUT HERE 141 public: 142 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(); } 143 //void run_test(int Case) { if ((Case == -1) || (Case == 0)) test_case_1();} 144 private: 145 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(); } 146 void verify_case(int Case, const long long &Expected, const long long &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << " Expected: "" << Expected << '"' << endl; cerr << " Received: "" << Received << '"' << endl; } } 147 void test_case_0() { int Arg0 = 0; int Arg1 = 0; int Arg2 = 5; int Arg3 = 5; long long Arg4 = 5LL; verify_case(0, Arg4, numberOfSticks(Arg0, Arg1, Arg2, Arg3)); } 148 void test_case_1() { int Arg0 = 0; int Arg1 = 0; int Arg2 = 10; int Arg3 = 2; long long Arg4 = 5LL; verify_case(1, Arg4, numberOfSticks(Arg0, Arg1, Arg2, Arg3)); } 149 void test_case_2() { int Arg0 = 2; int Arg1 = 2; int Arg2 = 8; int Arg3 = 8; long long Arg4 = 12LL; verify_case(2, Arg4, numberOfSticks(Arg0, Arg1, Arg2, Arg3)); } 150 void test_case_3() { int Arg0 = 8; int Arg1 = 5; int Arg2 = 20; int Arg3 = 16; long long Arg4 = 27LL; verify_case(3, Arg4, numberOfSticks(Arg0, Arg1, Arg2, Arg3)); } 151 void test_case_4() { int Arg0 = 0; int Arg1 = 0; int Arg2 = 1000000; int Arg3 = 1000000; long long Arg4 = 200000000000LL; verify_case(4, Arg4, numberOfSticks(Arg0, Arg1, Arg2, Arg3)); } 152 153 // END CUT HERE 154 155 }; 156 157 // BEGIN CUT HERE 158 int main() 159 { 160 // freopen( "a.out" , "w" , stdout ); 161 BedroomFloor ___test; 162 ___test.run_test(-1); 163 return 0; 164 } 165 // END CUT HERE