SRM 401(1-250pt, 1-500pt)
DIV1 250pt
题意:给一个整数f,则这样的正整数整数数列称为好数列:数列元素a0 >= a1 >= a2...,且a0<= f, a1 <= f-1, a2 <= f-2....。求给定f后,好数列有多少个。
解法:首先对于f = 3,{2, 1}为好数列,则补0称为{2, 1, 0}。然后,所有好数列长度都变为f。直接dp就好,d[i][j]表示长度为i,第一个元素(最大的)为j的数列有多少个。
d[i][j] = sum(d[i-1][k])其中k <= j。
Ps:官方题解写着还可以用Catalan数做,只不过我想不通为什么可以...
tag:DP
1 // BEGIN CUT HERE 2 /* 3 * Author: plum rain 4 * score : 5 */ 6 /* 7 8 */ 9 // END CUT HERE 10 #line 11 "FIELDDiagrams.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 CLR(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 zero(x) (((x)>0?(x):-(x))<eps) 42 #define out(x) cout<<#x<<":"<<(x)<<endl 43 #define tst(a) cout<<#a<<endl 44 #define CINBEQUICKER std::ios::sync_with_stdio(false) 45 46 typedef vector<int> VI; 47 typedef vector<string> VS; 48 typedef vector<double> VD; 49 typedef pair<int, int> pii; 50 typedef long long int64; 51 52 const double eps = 1e-8; 53 const double PI = atan(1.0)*4; 54 const int maxint = 2139062143; 55 56 int64 d[50][50]; 57 58 class FIELDDiagrams 59 { 60 public: 61 long long countDiagrams(int f){ 62 ++ f; 63 for (int i = 0; i < f; ++ i) 64 d[1][i] = 1; 65 for (int i = 2; i < f; ++ i){ 66 for (int j = 0; j <= f-i; ++ j){ 67 d[i][j] = 0; 68 for (int k = j; k <= f-i+1; ++ k) 69 d[i][j] += d[i-1][k]; 70 } 71 } 72 return d[f-1][0] + d[f-1][1] - 1; 73 } 74 75 // BEGIN CUT HERE 76 public: 77 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(); } 78 private: 79 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(); } 80 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 << "\tExpected: \"" << Expected << '\"' << endl; cerr << "\tReceived: \"" << Received << '\"' << endl; } } 81 void test_case_0() { int Arg0 = 2; long long Arg1 = 4LL; verify_case(0, Arg1, countDiagrams(Arg0)); } 82 void test_case_1() { int Arg0 = 3; long long Arg1 = 13LL; verify_case(1, Arg1, countDiagrams(Arg0)); } 83 void test_case_2() { int Arg0 = 5; long long Arg1 = 131LL; verify_case(2, Arg1, countDiagrams(Arg0)); } 84 85 // END CUT HERE 86 87 }; 88 89 // BEGIN CUT HERE 90 int main() 91 { 92 // freopen( "a.out" , "w" , stdout ); 93 FIELDDiagrams ___test; 94 ___test.run_test(-1); 95 return 0; 96 } 97 // END CUT HERE
DIV2 550pt
题意:有两个物体运动轨迹一个为x1 = cos(t1), y1 = sin(t1), z1 = t1,另一个物体运动轨迹为x2 = vx*t + x0, y2 = vy*t + y0, z2 = vz*t + z0。只要两个运动轨迹有交点即可,不需要t1 = t2。重合交点算一个。如果有多个交点输出{0, 0, 0},一个交点输出交点坐标,没有交点输出空集。
解法:理解题意理解了半天,还搞错了。。。上网看了下才知道。。。然后,代码本身不太好写,写之前想得太简单又写错了,于是这道题做了好久- -.........
首先,发现x1与y1的关系为x1*x1 + y1*y1 = 1,则x2*x2 + y2*y2 = 1,这样就变成了一元二次方程,当然,解出来的根还不一定满足题意,还要再判断。
Ps:官方题解说还有一种方法,就是有一个结论,要相交必然z为1/2的倍数。至于为什么,题解没有说,给了论坛的地址,看见里面的人在用英语讨论,也并不简单,我就放弃了。
tag:math
1 // BEGIN CUT HERE 2 /* 3 * Author: plum rain 4 * score : 5 */ 6 /* 7 8 */ 9 // END CUT HERE 10 #line 11 "ParticleCollision.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 CLR(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 zero(x) (((x)>0?(x):-(x))<eps) 42 #define out(x) cout<<#x<<":"<<(x)<<endl 43 #define tst(a) cout<<#a<<endl 44 #define CINBEQUICKER std::ios::sync_with_stdio(false) 45 46 typedef vector<int> VI; 47 typedef vector<string> VS; 48 typedef vector<double> VD; 49 typedef pair<int, int> pii; 50 typedef long long int64; 51 52 const double eps = 1e-11; 53 const double PI = atan(1.0)*4; 54 const int maxint = 2139062143; 55 56 bool ok(double t, int A, int B, int C, int a, int b, int c) 57 { 58 double x2 = A*t + a, y2 = B*t + b, z = C*t + c; 59 double x1 = cos(z*atan(1.0)*4), y1 = sin(z*atan(1.0)*4); 60 if (fabs(x1-x2) < eps && fabs(y1-y2) < eps) return 1; 61 return 0; 62 } 63 64 class ParticleCollision 65 { 66 public: 67 vector <double> collision(int A, int B, int C, int a, int b, int c){ 68 VD ans; ans.clear(); 69 VD tmp; 70 tmp.PB (0.0); tmp.PB (0.0); tmp.PB (0.0); 71 double ta = A*A+B*B, tb = 2*(a*A + b*B), tc = a*a + b*b - 1; 72 if (fabs(ta) < eps){ 73 if (tc) return ans; 74 else{ 75 if (C) return tmp; 76 if (!ok(0, A,B,C,a,b,c)) return ans; 77 ans.PB((double)a); 78 ans.PB((double)b); 79 ans.PB((double)c); 80 return ans; 81 } 82 } 83 84 double delta = tb*tb - 4 * ta * tc; 85 if (delta < -eps) return ans; 86 if (fabs(delta) < eps){ 87 double t1 = -tb / 2.0 / ta; 88 if (!ok(t1, A,B,C,a,b,c)) return ans; 89 ans.PB (A*t1+a); ans.PB (B*t1+b); ans.PB (C*t1+c); 90 return ans; 91 } 92 if (delta > eps){ 93 double t1, t[2] = {(-tb+sqrt(delta)) / 2.0/ta, (-tb-sqrt(delta)) / 2.0/ta}; 94 int t_num = 0; 95 for (int i = 0; i < 2; ++ i) 96 if (ok(t[i],A,B,C,a,b,c)){ 97 t1 = t[i]; ++ t_num; 98 } 99 if (t_num > 1) return tmp; 100 else if (!t_num) return ans; 101 ans.PB (A*t1+a); ans.PB (B*t1+b); ans.PB (C*t1+c); 102 return ans; 103 } 104 } 105 106 // BEGIN CUT HERE 107 public: 108 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(); } 109 private: 110 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(); } 111 void verify_case(int Case, const vector <double> &Expected, const vector <double> &Received) { cerr << "Test Case #" << Case << "..."; if (Expected == Received) cerr << "PASSED" << endl; else { cerr << "FAILED" << endl; cerr << "\tExpected: " << print_array(Expected) << endl; cerr << "\tReceived: " << print_array(Received) << endl; } } 112 void test_case_0() { int Arg0 = 0; int Arg1 = 1; int Arg2 = 0; int Arg3 = -1; int Arg4 = -5; int Arg5 = -4; double Arr6[] = {}; vector <double> Arg6(Arr6, Arr6 + (sizeof(Arr6) / sizeof(Arr6[0]))); verify_case(0, Arg6, collision(Arg0, Arg1, Arg2, Arg3, Arg4, Arg5)); } 113 void test_case_1() { int Arg0 = 2; int Arg1 = 4; int Arg2 = 1; int Arg3 = -1; int Arg4 = -1; int Arg5 = 0; double Arr6[] = {0.0, 1.0, 0.5 }; vector <double> Arg6(Arr6, Arr6 + (sizeof(Arr6) / sizeof(Arr6[0]))); verify_case(1, Arg6, collision(Arg0, Arg1, Arg2, Arg3, Arg4, Arg5)); } 114 void test_case_2() { int Arg0 = 4; int Arg1 = 4; int Arg2 = 2; int Arg3 = 5; int Arg4 = 4; int Arg5 = 0; double Arr6[] = {0.0, 0.0, 0.0 }; vector <double> Arg6(Arr6, Arr6 + (sizeof(Arr6) / sizeof(Arr6[0]))); verify_case(2, Arg6, collision(Arg0, Arg1, Arg2, Arg3, Arg4, Arg5)); } 115 void test_case_3() { int Arg0 = 0; int Arg1 = 0; int Arg2 = 1; int Arg3 = 1; int Arg4 = 0; int Arg5 = 0; double Arr6[] = {0.0, 0.0, 0.0 }; vector <double> Arg6(Arr6, Arr6 + (sizeof(Arr6) / sizeof(Arr6[0]))); verify_case(3, Arg6, collision(Arg0, Arg1, Arg2, Arg3, Arg4, Arg5)); } 116 117 // END CUT HERE 118 119 }; 120 121 // BEGIN CUT HERE 122 int main() 123 { 124 // freopen( "a.out" , "w" , stdout ); 125 ParticleCollision ___test; 126 ___test.run_test(-1); 127 return 0; 128 } 129 // END CUT HERE
------------------------------------------------------------------
现在的你,在干什么呢?
你是不是还记得,你说你想成为岩哥那样的人。
现在的你,在干什么呢?
你是不是还记得,你说你想成为岩哥那样的人。