H 描述
This is the logo of PKUACM 2016. More specifically, the logo is generated as follows:
- Put four points A0(0,0), B0(0,1), C0(1,1), D0(1,0) on a cartesian coordinate system.
- Link A0B0, B0C0, C0D0, D0A0separately, forming square A0B0C0D0.
- Assume we have already generated square AiBiCiDi, then square Ai+1Bi+1Ci+1Di+1is generated by linking the midpoints of AiBi, BiCi, CiDiand DiAisuccessively.
- Repeat step three 1000 times.
Now the designer decides to add a vertical line x=k to this logo( 0<= k < 0.5, and for k, there will be at most 8 digits after the decimal point). He wants to know the number of common points between the new line and the original logo.
输入
In the first line there’s an integer T( T < 10,000), indicating the number of test cases.
Then T lines follow, each describing a test case. Each line contains an float number k, meaning that you should calculate the number of common points between line x = k and the logo.
输出
For each test case, print a line containing one integer indicating the answer. If there are infinity common points, print -1.
样例输入
3
0.375
0.001
0.478
样例输出
-1
4
20
题目类型:二分查找
算法分析:由题目可知内部的每个小正方形都可以看作是相对较大的正方形二分边得来的,此时可以对于每个输入的坐标在[0,0. 5)二分坐标值
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
#pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define LL long long #define ULL unsigned long long #define DB(ccc) cout << #ccc << " = " << ccc << endl const int INF = 0x7FFFFFFF; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 3e5 + 66; int PP (double val) { double a = 0, b = 0.5, m; int res = 0; while (b - a > EPS) { m = (a + b) / 2; if (fabs (m - val) < EPS) return -1; else if (val + EPS < m) { res += 4; break; } else { res += 4; a = m; } } return res; } int main() { // CPPFF; int t; cin >> t; while (t--) { double val; cin >> val; if (fabs (val - 0) < EPS) cout << "-1" << endl; else cout << PP (val) << endl; } return 0; } |
- poj1014:下一篇 »