首页 > 代码库 > HDU 5045 5047 5050 5053(上海网络赛E,F,I,L)

HDU 5045 5047 5050 5053(上海网络赛E,F,I,L)

HDU 5045 5047 5050 5053

太菜了,名额差点没保住,吓尿。。赶紧开刷树链抛分

5045:状压DP,压缩10个人,由于两个人不能差2以上,所以可以用01表示

5047:推推公式即可,每次交线多4条

5050:求GCD,用java大叔即可

5053:签到题

代码:

5045:

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;

const int N = 1025;

int t, n, m, vis[N][N];
double dp[N][N], g[15][N];

double dfs(int u, int num) {
    if (u == (1<<n) - 1) u = 0;
    if (vis[u][num]) return dp[u][num];
    vis[u][num] = 1;
    if (num == m) return dp[u][num] = 0;
    dp[u][num] = 0;
    for (int i = 0; i < n; i++) {
        if (u&(1<<i)) continue;
        dp[u][num] = max(dp[u][num], dfs(u|(1<<i), num + 1) + g[i][num]);
    }
    return dp[u][num];
}

int main() {
    int cas = 0;
    scanf("%d", &t);
    while (t--) {
        scanf("%d%d", &n, &m);
        for (int i = 0; i < n; i++) {
            for (int j = 0; j < m; j++) {
                scanf("%lf", &g[i][j]);
            }
        }
        memset(vis, 0, sizeof(vis));
        printf("Case #%d: %.5lf\n", ++cas, dfs(0, 0));
    }
    return 0;
}

5047:

#include <cstdio>
#include <cstring>

typedef long long ll;

const ll MOD = 100000000;
int t;
ll n;

struct Num {
    ll num[5];
    int len;
    Num() {
        len = 0; 
        memset(num, 0, sizeof(num));
    }
    void init(ll x) {
        len = 0;
        if (x == 0) {
            len = 1;
            num[0] = 0;
            return;
        }
        while (x) {
            num[len++] = x % MOD;
            x /= MOD;
        }
    }

    Num operator * (Num c) {
        Num ans;
        for (int i = 0; i < len; i++) {
            for (int j = 0; j < c.len; j++) {
                ans.num[i + j] += num[i] * c.num[j];
            }
        }
        ans.len = len + c.len;
        for (int i = 0; i < ans.len; i++) {
            ans.num[i + 1] += ans.num[i] / MOD;
            ans.num[i] %= MOD;
        }
        return ans;
    }

    void add() {
        while (len && num[len - 1] == 0) len--;
        num[0] += 2;
        for (int i = 0; i < len; i++) {
            num[i + 1] += num[i] / MOD;
            num[i] %= MOD;
        }
        if (num[len]) len++;
        while (len && num[len - 1] == 0) len--;
    }

    void print() {
        while (len && num[len - 1] == 0) len--;
        for (int i = len - 1; i >= 0; i--) {
            if (i == len - 1) printf("%I64d", num[i]);
            else printf("%08I64d", num[i]);
        }
        printf("\n");
    }
} A, B;

int main() {
    int cas = 0;
    scanf("%d", &t);
    while (t--) {
        scanf("%I64d", &n);
        A.init(n - 1);
        B.init(8 * n + 1);
        Num c = (A * B);
        c.add();
        printf("Case #%d: ", ++cas);
        c.print();
    }
    return 0;
}

5050:

import java.util.*;
import java.math.*;
import java.io.*;

public class Main {
    public static BigInteger gcd(BigInteger a, BigInteger b) {
        if (b.equals(BigInteger.valueOf(0)) == true) return a;
        return gcd(b, a.mod(b));
    }
    public static void main(String args[]) {
        Scanner cin = new Scanner(System.in);
        int t;
        t = cin.nextInt();
        String a, b;
        BigInteger aa, bb;
        for (int cas = 1; cas <= t; cas++) {
            a = cin.next();
            b = cin.next();
            aa = BigInteger.valueOf(0);
            bb = BigInteger.valueOf(0);
            for (int i = 0; i < a.length(); i++) {
                aa = aa.multiply(BigInteger.valueOf(2));
                if (a.charAt(i) == '1') aa = aa.add(BigInteger.valueOf(1));
            }
            for (int i = 0; i < b.length(); i++) {
                bb = bb.multiply(BigInteger.valueOf(2));
                if (b.charAt(i) == '1') bb = bb.add(BigInteger.valueOf(1));
            }
            BigInteger tmp = gcd(aa, bb);
            String ans = "";
            while (tmp.equals(BigInteger.valueOf(0)) == false) {
                if (tmp.mod(BigInteger.valueOf(2)).equals(BigInteger.valueOf(1))) ans += '1';
                else ans += '0';
                tmp = tmp.divide(BigInteger.valueOf(2));
            }
            System.out.print("Case #");
            System.out.print(cas);
            System.out.print(": ");
            for (int i = ans.length() - 1; i >= 0; i--)
                System.out.print(ans.charAt(i));
            System.out.println();
        }
    }
}

5053:

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <set>
using namespace std;

typedef long long ll;

int t;
ll a, b;

int main() {
    int cas = 0;
    scanf("%d", &t);
    while (t--) {
        scanf("%I64d%I64d", &a, &b);
        ll sum = 0;
        for (ll i = a; i <= b; i++)
            sum += i * i * i;
        printf("Case #%d: %I64d\n", ++cas, sum);
    }
    return 0;
}


HDU 5045 5047 5050 5053(上海网络赛E,F,I,L)