数学 2016.10.19

1、HDU 5734 Acperience

参考:http://bestcoder.hdu.edu.cn/blog/2016-multi-university-training-contest-2-solutions-by-zimpha/

解题思路:

①展开得:

②令 C1 = ,显然 C1 为常数,,要求的最小值,则,令 C2 =,C2 也为常数

③问题进一步转化为求 α^2*n - 2*α*C2 这个二次方程的最小值,当 α = -b / 2a = C2 / n 时取得最小值

④所以原问题的解为:n * (C2^2) / n^2 - 2 * C2^2 / n + C1 =  (C2^2) / n - 2 * C2^2 / n + C1 = C1 - (C2^2) / n

⑤最后将分子分母同时乘以 n,分子分母再同时除以它们的最大公约数,即得最简式


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <cmath>
#include <cctype>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;

const ull mod = 1e9 + 7;
const int INF = 0x7fffffff;
ll n;

int main()
{
#ifdef __AiR_H
    freopen("in.txt", "r", stdin);
#endif // __AiR_H
    int T;
    scanf("%d", &T);
    while (T--) {
        ll sum_w = 0, sum_pow_w_2 = 0;
        scanf("%I64d", &n);
        ll w_t;
        for (ll i = 0; i < n; ++i) {
            scanf("%I64d", &w_t);
            if (w_t < 0) {
                w_t = -w_t;
            }
            sum_w += w_t;
            sum_pow_w_2 += w_t * w_t;
        }
        ll ans = sum_pow_w_2*n - sum_w*sum_w;
        ll g = __gcd(ans, n);
        printf("%I64d/%I64d\n", ans/g, n/g);
    }
    return 0;
}


2、UVa 10112 Myacm Triangles

题意:

找出面积最大且不包含其它顶点的三角形

题中给出了三角形面积计算公式 0.5 * [(y3 - y1) * (x2 - x1) - (y2 - y1) * (x3 - x1)]


解题思路:

若三角形ABC内有一点 D ,则 S_ABC = S_ABD + S_ACD + S_BCD


#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <cmath>
#include <cctype>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;

const double eps = 1e-8;
const ull mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 15 + 5;

struct Node {
    int x, y;
};
Node node[maxn];

double Get_Area(double x1, double y1, double x2, double y2, double x3, double y3);

int main()
{
#ifdef __AiR_H
    freopen("in.txt", "r", stdin);
#endif // __AiR_H
    int n;
    char s_t[2];
    while (scanf("%d", &n) != EOF && n != 0) {
        int ans_1, ans_2, ans_3;
        for (int i = 0; i < n; ++i) {
            scanf("%s%d%d", s_t, &node[i].x, &node[i].y);
        }
        double Max = -1.0;
        for (int i = 0; i < n-2; ++i) {
            for (int j = i+1; j < n-1; ++j) {
                for (int k = j+1; k < n; ++k) {
                    double t = Get_Area(node[i].x, node[i].y, node[j].x, node[j].y, node[k].x, node[k].y);
                    bool flag = true;
                    for (int x = 0; x < n; ++x) {
                        if (!(x == i || x == j || x == k)) {
                            double t1 = Get_Area(node[i].x, node[i].y, node[j].x, node[j].y, node[x].x, node[x].y);
                            t1 += Get_Area(node[i].x, node[i].y, node[k].x, node[k].y, node[x].x, node[x].y);
                            t1 += Get_Area(node[k].x, node[k].y, node[j].x, node[j].y, node[x].x, node[x].y);
                            if (fabs(t-t1) < eps) {
                                flag = false;
                                break;
                            }
                        }
                    }
                    if (flag && t > Max) {
                        Max = t;
                        ans_1 = i; ans_2 = j; ans_3 = k;
                    }
                }
            }
        }
        printf("%c%c%c\n", ans_1+'A', ans_2+'A', ans_3+'A');
    }
    return 0;
}

double Get_Area(double x1, double y1, double x2, double y2, double x3, double y3)
{
    double ans = 0.5 * ((y3 - y1) * (x2 - x1) - (y2 - y1) * (x3 - x1));
    return fabs(ans);
}

3、HDU 5741 Helter Skelter

参考:http://bestcoder.hdu.edu.cn/blog/2016-multi-university-training-contest-2-solutions-by-zimpha/

%%%,先膜为敬

先确定上下边界,然后二分,至于为什么,怎么做,那就要看标程和题解自己想,自己调试了,%%%

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <cmath>
#include <cctype>

using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int, int> Pair;

const ull mod = 1e9 + 7;
const int INF = 0x7fffffff;
const int maxn = 1e3 + 10;
int n, m;
int x[maxn];
Pair low[maxn*maxn], upper[maxn*maxn];

int main()
{
#ifdef __AiR_H
    freopen("in.txt", "r", stdin);
#endif // __AiR_H
    int T;
    scanf("%d", &T);
    while (T--) {
        scanf("%d%d", &n, &m);
        for (int i = 0; i < n; ++i) {
            scanf("%d", &x[i]);
        }
        int low_Count = 0, upper_Count = 0;
        for (int i = 0; i < n; ++i) {
            int x_t = 0, y_t = 0;
            for (int j = i; j < n; ++j) {
                if (j%2 == 0) {
                    x_t += x[j];
                } else {
                    y_t += x[j];
                }
                if (i%2 == 0 && j%2 == 0) {
                    low[low_Count++] = Pair(x_t, y_t);
                } else if (i%2 == 1 && j%2 == 1) {
                    upper[upper_Count++] = Pair(x_t, y_t);
                }
            }
        }
        sort(low, low+low_Count);
        sort(upper, upper+upper_Count);
        int Count = 0;
        for (int i = 0, j; i < low_Count; i = j) {
            j = i;
            while (j < low_Count && low[i].first == low[j].first) {
                ++j;
            }
            while (Count > 0 && low[Count-1].second >= low[i].second) {
                --Count;
            }
            low[Count++] = low[i];
        }
        low_Count = Count;
        Count = 0;
        for (int i = 0, j; i < upper_Count; i = j) {
            j = i;
            while (j < upper_Count && upper[i].first == upper[j].first) {
                ++j;
            }
            if (Count == 0) {
                upper[Count++] = upper[j-1];
            } else if (upper[Count-1].second < upper[j-1].second) {
                upper[Count++] = upper[j-1];
            }
        }
        upper_Count = Count;
        int a, b;
        for (int i = 0; i < m; ++i) {
            scanf("%d%d", &a, &b);
            int x = upper_bound(low, low+low_Count, Pair(a, -INF)) - low;
            int y = upper_bound(upper, upper+upper_Count, Pair(a, INF)) - upper;
            if (x < low_Count && low[x].second <= b && b <= upper[y-1].second) {
                printf("1");
            } else {
                printf("0");
            }
        }
        printf("\n");
    }
    return 0;
}

4、HDU 5512 Pagodas

参考:http://blog.csdn.net/queuelovestack/article/details/49533989

题意:

有 n 座塔,除编号为 a,b 的塔保留下来以外,其他塔需要重建

对于已建成的塔 j,k(包括 a,b 两塔),每次只能重建编号为 j+k 或 j-k 的塔

Yuwgna 和 Iaka 两个僧人轮流建一座塔,Yuwgna 先手,最后不能建的人输,问最终获胜的人是哪个

解题思路:

能新建的塔的编号显然是 ged(a, b) 的倍数,所以我们只要判断 n/gcd(a, b) 的奇偶性就可以了

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <bitset>
#include <ctime>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define lson low, mid, _id<<1
#define rson mid+1, high, _id<<1|1

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int, int> Pair;

const ull mod = 1e9 + 7;
const int INF = 0x7fffffff;

int t, n, a, b, Case = 0;

int main()
{
#ifdef __AiR_H
    freopen("in.txt", "r", stdin);
#endif // __AiR_H
    scanf("%d", &t);
    while (t--) {
        scanf("%d %d %d", &n, &a, &b);
        if ((n/__gcd(a, b))%2 == 0) {
            printf("Case #%d: Iaka\n", ++Case);
        } else {
            printf("Case #%d: Yuwgna\n", ++Case);
        }
    }
    return 0;
}

5、HDU 5914 Triangle

解题思路:

斐波那契

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <vector>
#include <stack>
#include <map>
#include <set>
#include <cmath>
#include <cctype>
#include <bitset>
#include <ctime>

using namespace std;

#define REP(i, n) for (int i = 0; i < (n); ++i)
#define lson low, mid, _id<<1
#define rson mid+1, high, _id<<1|1

typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
typedef pair<int, int> Pair;

const ull mod = 1e9 + 7;
const int INF = 0x7fffffff;

int T, n, Case = 0;
int key[20];

int main()
{
#ifdef __AiR_H
    freopen("in.txt", "r", stdin);
#endif // __AiR_H
    key[0] = 1, key[1] = 1;
    for (int i = 2; i < 15; ++i) {
        key[i] = key[i-1] + key[i-2];
    }
    scanf("%d", &T);
    while (T--) {
        scanf("%d", &n);
        int t = lower_bound(key+1, key+10, n) - key;
        if (key[t] > n) {
            --t;
        }
        int ans = n - t;
        printf("Case #%d: %d\n", ++Case, ans);
    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值