首页 > 代码库 > UVA 10623 - Thinking Backward(数论)
UVA 10623 - Thinking Backward(数论)
UVA 10623 - Thinking Backward
题目链接
题意:给定一个数量,求用圆,椭圆,三角形分割平面,分割出该数量,输出所有情况
思路:有公式2 + 2m(m-1) + n(n-1) + 4mn + 3p(p-1) + 6mp + 6np
由于m和p都是[0,100],所以可以枚举m和p,去求出n,然后判断合不合适
代码:
#include <stdio.h> #include <string.h> #include <math.h> #include <vector> #include <algorithm> using namespace std; //2 + 2m(m-1) + n(n-1) + 4mn + 3p(p-1) + 6mp + 6np long long N, n; struct Ans { long long m, n, p; Ans(long long m, long long n, long long p) { this->m = m; this->n = n; this->p = p; } }; vector<Ans> ans; bool cmp(Ans a, Ans b) { if (a.m != b.m) return a.m < b.m; if (a.n != b.n) return a.n < b.n; return a.p < b.p; } void judge(long long m, long long p) { long long a = 1; long long b = (4 * m + 6 * p - 1); long long c = 2 + 2 * m * (m - 1) + 3 * p * (p - 1) + 6 * m * p - N; long long delta = b * b - 4 * a * c; if (delta < 0) return; long long tmp = (long long)(sqrt(delta)); if (tmp * tmp != delta) return; long long n1 = -b + tmp, n2 = -b - tmp; if (n1 % (2 * a) == 0) { n1 /= (2 * a); if (n1 >= 0 && n1 < 20000) ans.push_back(Ans(m, n1, p)); } if (n2 % (2 * a) == 0) { n2 /= (2 * a); if (n2 >= 0 && n2 < 20000) ans.push_back(Ans(m, n2, p)); } } int main() { int cas = 0; while (~scanf("%lld", &N) && N != -1) { printf("Case %d:\n", ++cas); if (N == 1) {printf("0 0 0\n"); continue;} ans.clear(); for (long long m = 0; m < 100; m++) { for (long long p = 0; p < 100; p++) judge(m, p); } if (!ans.size()) printf("Impossible.\n"); else { sort(ans.begin(), ans.end(), cmp); for (int i = 0; i < ans.size(); i++) { if (ans[i].m == 0 && ans[i].n == 0 && ans[i].p == 0 && N != 1) continue; printf("%lld %lld %lld\n", ans[i].m, ans[i].n, ans[i].p); } } } return 0; }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。