• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
dwtfukgv
博客园    首页    新随笔    联系   管理    订阅  订阅
HDU 2050 折线分割平面 (递推)

题意:略。

析:多写几个就找到规律了,第1条是2,2条时是7个,3条时是16,4条时是29,。。。。

那么规律就出来了2 * n * n + 1 - n;

也可以递推,第n条折线的两条边都与前n-1条折线的所有边都不平行,因为他们都是相交的;第n条折线的第一条边要与前n-1条折线的2*(n-1)条边都相交,

每与两个边相交就增加一个分割开的部分,所以有2*(n-1)-1个被分割的部分在这里被增加,另外一条第n条折线的边也增加2*(n-1)-1个部分,另外最后第n第折线的两边,

还要向外无限延伸,与它们相交的最后一个前n-1个折线中的边与其分别构成了一个多余的部分,而第n条折线的头部也是一个独立的部分,所 以2*(n-1)-1再+3,

就是比n-1条折线分割成的部分多出的部分数,所以有:a[n]=(2*(n-1)-1)*2+3+a[n-1];

代码如下:

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;
const int maxn = 10000 + 5;

int main(){
    int T, n, a, b;  cin >> T;

    while(T--){
        scanf("%d", &n);
        printf("%d\n", 2*n*n-n+1);
    }
    return 0;
}

 

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
using namespace std ;

typedef long long LL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const double inf = 0x3f3f3f3f3f3f3f;
const double eps = 1e-8;
const int maxn = 1e4 + 5;
const int mod = 1e9 + 7;
const int dr[] = {0, 0, -1, 1};
const int dc[] = {-1, 1, 0, 0};
int n, m;
inline bool is_in(int r, int c){
    return r >= 0 && r < n && c >= 0 && c < m;
}
LL ans[maxn];

void init(){
    ans[0] = 1;
    for(int i = 1; i <= 10000; ++i)
        ans[i] = ans[i-1] + 2LL * (2LL*(i-1)-1LL) + 3;
}

LL f(int n){
    if(!n)  return 1;
    return 2LL*(2LL*(n-1)-1LL) + 3 + f(n-1);
}

int main(){
    //init();
    int T;  cin >> T;
    while(T--) cin >> n, cout << f(n) << endl;
    return 0;
}

 

posted on 2016-08-05 20:08  dwtfukgv  阅读(768)  评论(1)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3