2018 百度之星程序设计大赛 复赛(hdu 6409 6410 6411)

比赛题目链接

A题:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define INF 0x3f3f3f3f
const int  maxn =100000+5;
#define mod 1e9+7;
#define ll long long int
using namespace std;

int main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
        int a[maxn],b[maxn]; ll sum_max=0,sum_min=0;
        vector fa[maxn];
        int n; scanf("%d", &n);
        for (int i = 2; i <= n; i++) scanf("%d", &a[i]);
        for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
        for (int i = 0; i <= n; i++) fa[i].clear();
        for (int i = 1; i <= n; i++) fa[a[i]].push_back(b[i]);
        int MAX = 0, MIN = INF;
        for (int j = 0; j <= n; j++)
        {
            int fMAX = 0, fMIN = INF;
            for (int i = 0; i < fa[j].size(); i++)
            {
                if (fa[j][i] > fMAX)
                    MAX = max(MAX, fMAX),fMAX = fa[j][i];
                else
                    MAX = max(MAX, fa[j][i]);
                if (fa[j][i] < fMIN)
                    MIN = min(MIN, fMIN), fMIN = fa[j][i];
                else
                    MIN = min(MIN, fa[j][i]);
            }
            sum_max += max(0, fMAX);
            sum_min += min(0, fMIN);
        }
        cout<

B题:

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include

#define INF 0x3f3f3f3f
const int  maxn =100+10;
const int  mod =1e9+7;
#define ll long long int
using namespace std;

ll l[maxn], r[maxn], vv[maxn], p[maxn];
ll getsum(ll l, ll r)
{
    return (l + r)*(r - l + 1) / 2 % mod;
}
ll anss(ll k, ll x, ll t = 0)
{
    if (l[k] > x - t)
        return 0;
    return getsum(x - min(x - t, r[k]) + 1, x - l[k] + 1);
}
ll quick(ll a, ll n, ll m)
{
    ll dis = 1;
    while (n)
    {
        if (n & 1)
            dis = (dis * a) % m;
        a = (a * a) % m, n >>= 1;
    }
    return dis;
}
int  main()
{
    int t;scanf("%d",&t);
    while(t--)
    {
        ll n, bb = 0;cin>>n;
        for (ll i = 1; i <= n; i++)
            cin>>l[i]>>r[i], bb = max(bb, r[i]);
        ll ans = 0, t = 0;
        for (ll i = 1; i <= bb; i++)
        {
            p[n + 1] = 1;
            for (ll j = n; j >= 1; j--)
                p[j] = p[j + 1] * anss(j, i) % mod;
            t = 1;
            for (ll j = 1; j <= n; j++)
            {
                if (i <= r[j] && i >= l[j])
                    ans = (ans + t * p[j + 1] % mod) % mod;
                t = t * anss(j, i, 1) % mod;
                if (t==0)
                    break;
            }
        }
        t = 1;
        for (ll i = 1; i <= n; i++)
            t = t * (r[i] - l[i] + 1) % mod;
        ans = ans * quick(t, mod - 2, mod) % mod;
        ans = (ans % mod + mod) % mod;
        cout<

C题:

#include

#define INF 0x3f3f3f3f
const int  maxn =100000+5;
const int  mod =1e9+7;
#define ll long long int
using namespace std;

vector v[maxn];
int b[maxn];
int a[maxn];

int find(int x)
{
    return b[x] == x ? x : b[x] = find(b[x]);
}
void unio(int x, int y)
{
    b[find(y)] = find(x);
}
int main()
{
    int t;
    cin >> t;
    while(t--)
    {
        int n, m;ll ans = 0;
        scanf("%d%d", &n, &m);
        for (int i = 1; i <= n; i++)
            scanf("%d", &a[i]), b[i] = i;
        for (int i = 1; i <= m; i++)
        {
            int u, v;
            scanf("%d%d", &u, &v);
            unio(u, v);
        }
        for (int i = 1; i <= n; i++)
            v[i].clear();
        for (int i = 1; i <= n; i++)
            v[find(i)].push_back(a[i]);
        for (int i = 1; i <= n; i++)
        {
            sort(v[i].begin(), v[i].end());
            ll e[35] = { 0 };
            for (int j = 0; j < v[i].size(); j++)
            {
                int t = v[i][j];
                for (int k = 0; k < 32 && t; k++)
                {
                    if (t & 1)
                        e[k]++;
                    t >>= 1;
                }
            }
            for (int j = v[i].size() - 1; j > 0; j--)
            {
                int t = v[i][j];
                for (int k = 0; k < 32 && t; k++)
                {
                    if (t & 1)
                    {
                        e[k]--;
                        ans=(ans+(1ll<>= 1;
                }
            }
        }
        cout<

 

你可能感兴趣的:(大一,暑期练习)