HDU2050 折线分割平面
生活随笔
收集整理的这篇文章主要介绍了
HDU2050 折线分割平面
小编觉得挺不错的,现在分享给大家,帮大家做个参考.
题目:acm.hdu.edu.cn/showproblem.php?pid=2050
递推:
从直线入手,第n条直线,最多和平面上的直线有n-1个交点,多出(n-1)+1个部分
| 序号 | 1 | 2 | 3 | ... | n |
| 交点 | 0 | 1 | 2 | ... | n-1 |
| 多出部分 | 1 | 2 | 3 | ... | (n-1)+1 |
总部分 2 4 7 ....
在转化为折线,当增加第n条折线时,与图形新的交点最多2*2*(n-1), 多出2*2*(n-1)+1 个部分
(可以把折线看作两条直线理解)
所以 f(n) = f(n-1) + 4*(n-1)+1;
AC代码:
#include <iostream> #include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #include <string> #include <bitset> #include <vector> #include <set> #include <map> #include <queue> #include <algorithm> #include <sstream> #include <stack> using namespace std; #define rep(i,a,n) for (int i=a;i<n;i++) #define per(i,a,n) for (int i=n-1;i>=a;i--) #define pb push_back #define mp make_pair #define all(x) (x).begin(),(x).end() #define fi first #define se second #define SZ(x) ((int)(x).size()) #define FO freopen("in.txt", "r", stdin); #define lowbit(x) (x&-x) typedef vector<int> VI; typedef long long ll; typedef pair<int,int> PII; const ll mod=1000000007; const int inf = 0x3f3f3f3f; ll powmod(ll a,ll b) {ll res=1;a%=mod;for(;b;b>>=1){if(b&1)res=res*a%mod;a=a*a%mod;}return res;} ll gcd(ll a,ll b) { return b?gcd(b,a%b):a;} template <class T> inline bool scan_d(T &ret) {char c; int sgn;if (c = getchar(), c == EOF) return 0; //EOFwhile (c != '-' && (c < '0' || c > '9')) c = getchar();sgn = (c == '-') ? -1 : 1;ret = (c == '-') ? 0 : (c - '0');while (c = getchar(), c >= '0' && c <= '9') ret = ret * 10 + (c - '0');ret *= sgn;return 1; } inline void out(ll x) {if (x > 9) out(x / 10);putchar(x % 10 + '0'); }const int maxn = 10010; ll f[maxn], ans; int main() {int _, n;f[1] = 2;rep(i, 2, maxn) {f[i] = f[i-1]+4*(i-1)+1;//递推公式 } for(scan_d(_);_;_--) {scan_d(n);out(f[n]);printf("\n");} }
转载于:https://www.cnblogs.com/ACMerszl/p/9572954.html
总结
以上是生活随笔为你收集整理的HDU2050 折线分割平面的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 梳理一下我理解的aop
- 下一篇: [国家集训队]最长双回文串 man