您的位置:首页 > 其它

UVa 10623 - Thinking Backward (平面图的欧拉公式)

2015-07-21 21:10 513 查看

题意

椭圆三角形圆形混合,问有多少个面。

思路

这题要用到平面图的欧拉公式。

V-E+F=2。

V是图的点数,E是边数,F是面。

我们可以通过计算出最终图里的V和E然后算出F。

F=E-V+2

下面简单说下怎么推出公式。

按题目要求,有m个椭圆,n个圆,p个三角形。

我们可以分别计算出椭圆和椭圆贡献了多少个点和面,椭圆和圆产生了多少个点和面,椭圆和三角形产生多少个点和面。。。。等等。最后的和就是答案。

如何计算椭圆和椭圆产生了多少个面?

我们可以算出它们的交点数和边数,然后用上面那个公式算。

大家随便搞一下就能发现,假设现在有x个椭圆,它们产生的交点数是4(x−1)+4(x−2)+...+4=2x(x−1)

产生的边数是4x(x−1)。减一下,就变成2x(x−1)

再随便说一下椭圆和三角形相交的计算。一开始算不出来,就去超市买果冻吃,在路上就想出来了。

每个三角形放一个椭圆,每条边就会多出两个点,总共出现6个点,9条边,加上原来的三个顶点就是9个点。

那么p个三角形,总共出现(6m+3)p个点,而每放一个椭圆,三角形边的数目增加6条,椭圆的边数增加6条。那么,总共的边数是(1+2m)∗3p+6mp

减一下,6mp的贡献。

其他的就以此类推了。

最终的公式是F=2+2m(m−1)+n(n−1)+4mn+3p(p−1)+6mp+6np

然后解一下二次方程就行。注意特判1

代码

#include <stack>
#include <cstdio>
#include <list>
#include <cassert>
#include <set>
#include <fstream>
#include <iostream>
#include <string>
#include <sstream>
#include <vector>
#include <queue>
#include <functional>
#include <cstring>
#include <algorithm>
#include <cctype>
//#pragma comment(linker, "/STACK:102400000,102400000")
#include <string>
#include <map>
#include <cmath>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/hash_policy.hpp>
using namespace std;
//using namespace __gnu_pbds;
#define LL long long
#define ULL unsigned long long
#define SZ(x) (int)x.size()
#define Lowbit(x) ((x) & (-x))
#define MP(a, b) make_pair(a, b)
#define MS(p, num) memset(p, num, sizeof(p))
#define PB push_back
#define X first
#define Y second
#define ROP freopen("input.txt", "r", stdin);
#define MID(a, b) (a + ((b - a) >> 1))
#define LC rt << 1, l, mid
#define RC rt << 1|1, mid + 1, r
#define LRT rt << 1
#define RRT rt << 1|1
#define FOR(i, a, b) for (int i=(a); (i) < (b); (i)++)
#define FOOR(i, a, b) for (int i = (a); (i)<=(b); (i)++)
#define TRAVERSAL(u, i) for (int i = head[u]; i != -1; i = edge[i].nxt)
const double PI = acos(-1.0);
const int INF = 0x3f3f3f3f;
const double eps = 1e-8;
const int MAXN = 1e5+10;
const int MOD = 1e9+7;
const int dir[][2] = { {-1, 0}, {1, 0}, {0, -1}, {0, 1} };
const int seed = 131;
int cases = 0;
typedef pair<int, int> pii;

struct POINT
{
int m, n, p;
bool operator < (const POINT &a) const
{
if (m != a.m) return m < a.m;
if (n != a.n) return n < a.n;
return p < a.p;
}
};

vector<POINT> ans;

void Solve(int m, int p, LL num)
{
LL b = 4*m+6*p-1;
LL delta = b*b + 4*num;
if (delta < 0) return;
double res = (-b + sqrt(delta)) / 2;
LL int_res = (LL)round(res);
if (int_res < 0 || int_res >= 20000) return;
if (int_res*int_res + b*int_res == num)
ans.PB((POINT){m, (int)int_res, p});
}

int main()
{
//ROP;
LL n;
while (scanf("%lld", &n), n != -1)
{
ans.clear();
printf("Case %d:\n", ++cases);
if (n == 1) { puts("0 0 0"); continue; }
for (int m = 0; m < 100; m++)
for (int p = 0; p < 100; p++)
{
LL num = 2+2*m*(m-1)+3*p*(p-1)+6*m*p;
num = n-num;    //f(n) = num
Solve(m, p, num);
}
sort(ans.begin(), ans.end());
if (ans.empty()) printf("Impossible.\n");
else for (int i = 0; i < SZ(ans); i++) printf("%d %d %d\n", ans[i].m, ans[i].n, ans[i].p);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  acm-icpc