UVA 10519 !! Really Strange !!
2013-01-20 23:48
330 查看
大意:n个圆,两两相交于两点,把平面分成多少个区域。
思路:中学递推题,设已有n-1个圆,第n个圆与前n-1个圆形成2(n-1)交点,即多出2*(n-1)条弧,一条弧把平面分成2*(n-1)个平面。
所以:f(n) = f(n-1) + 2*(n-1),然后根据f(2) - f(1) = 2; f(3) - f(2) = 4; f(n) - f(n-1) = 2*(n-1)可推出,f(n) = n*n - n + 2;
n=1时特判,WA3次。
思路:中学递推题,设已有n-1个圆,第n个圆与前n-1个圆形成2(n-1)交点,即多出2*(n-1)条弧,一条弧把平面分成2*(n-1)个平面。
所以:f(n) = f(n-1) + 2*(n-1),然后根据f(2) - f(1) = 2; f(3) - f(2) = 4; f(n) - f(n-1) = 2*(n-1)可推出,f(n) = n*n - n + 2;
n=1时特判,WA3次。
#include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <string> #include <cmath> #include <algorithm> using namespace std; const int MAXN = 410; struct bign { int s[MAXN], len; bign () { memset(s, 0, sizeof(s)); len = 1;} bign (int num) { *this = num;} bign (const char *num) { *this = num;} bign operator = (const char *num) { len = strlen(num); for(int i = 0; i < len; i++) s[i] = num[len-i-1] - '0'; return *this; } bign operator = (int num) { char s[MAXN]; sprintf(s, "%d", num); *this = s; return *this; } bign operator + (const bign &b) { bign c; c.len = 0; for(int i = 0, g = 0; g || i < max(len, b.len); i++) { int x = g; if(i < len) x += s[i]; if(i < b.len) x += b.s[i]; c.s[c.len++] = x % 10; g = x / 10; } return c; } void clean() { while(s[len-1] == 0 && len > 1) len--; } bign operator * (const bign &b) { bign c; c.len = len + b.len; for(int i = 0; i < len; i++) { for(int j = 0; j < b.len; j++) { c.s[i+j] += s[i] * b.s[j]; } } for(int i = 0; i < c.len; i++) { c.s[i+1] += c.s[i] / 10; c.s[i] %= 10; } c.clean(); return c; } bign operator - (const bign &b) { bign c; c.len = 0; for(int i = 0, g = 0; i < len; i++) { int x = s[i]-g; if(i < b.len) x -= b.s[i]; if(x >= 0) g = 0; else { g = 1; x += 10; } c.s[c.len++] = x; } c.clean(); return c; } bool operator < (const bign &b) { if(len != b.len) return len < b.len; for(int i = len-1; i >= 0; i--) { if(s[i] != b.s[i]) return s[i] < b.s[i]; } return 0; } bool operator > (const bign &b) { if(len != b.len) return len > b.len; for(int i = len-1; i >= 0; i--) { if(s[i] != b.s[i]) return s[i] > b.s[i]; } return 0; } bool operator == (const bign &b) { return !(*this > b) && !(*this < b); } string str() const { string res = ""; for(int i = 0; i < len; i++) res = char(s[i]+'0') + res; if(res == "") res = "0"; return res; } }; bign ans; istream& operator >> (istream &in, bign &x) { string s; in >> s; x = s.c_str(); return in; } ostream& operator << (ostream &out, bign &x) { out << x.str(); return out; } int main() { bign n; while(cin>>n) { if(n == 0) { printf("1\n"); continue;} ans = n*n - n + 2; cout<<ans<<endl; } return 0; }
相关文章推荐
- UVa 10519 - !! Really Strange !!
- UVa:10519 - !! Really Strange !!
- UVA10519 - !! Really Strange !!(数论+高精度)
- UVA 10519 !! Really Strange !!
- UVA 10519 !! Really Strange !!
- uva 10519 - !! Really Strange !!(规律)
- uva 10519 !! Really Strange !!
- UVa 10519 - !! Really Strange !!
- UVa 10519 !! Really Strange !! (递推)
- UVA 10519 - !! Really Strange !!(数论,规律)
- UVA 10519 !! Really Strange !!
- UVA 10519 !! Really Strange !!
- uva 11529 - Strange Tax Calculation(计数问题)
- UVA - 11529 Strange Tax Calculation
- uva11529 - Strange Tax Calculation 凹四边形个数
- UVa11592 Strange Tax Calculation
- UVA 11529 Strange Tax Calculation
- UVA 11529-Strange Tax Calculation-求三角形内点数 /二分/two pointers
- uva 11529 - Strange Tax Calculation(计数问题)
- UVa10519 - !! Really Strange !!