Codeforces Round #428 (Div. 2)

最近一直懒,怎么没有补上题解(放上代码.并不需要写臭长的题解)

A. Arya and Bran
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Bran and his older sister Arya are from the same house. Bran like candies so much, so Arya is going to give him some Candies.

At first, Arya and Bran have 0 Candies. There are n days, at the i-th day, Arya finds ai candies in a box, that is given by the Many-Faced God. Every day she can give Bran at most 8 of her candies. If she don't give him the candies at the same day, they are saved for her and she can give them to him later.

Your task is to find the minimum number of days Arya needs to give Bran k candies before the end of the n-th day. Formally, you need to output the minimum day index to the end of which k candies will be given out (the days are indexed from 1 to n).

Print -1 if she can't give him k candies during n given days.

Input

The first line contains two integers n and k (1 ≤ n ≤ 100, 1 ≤ k ≤ 10000).

The second line contains n integers a1, a2, a3, ..., an (1 ≤ ai ≤ 100).

Output

If it is impossible for Arya to give Bran k candies within n days, print -1.

Otherwise print a single integer — the minimum number of days Arya needs to give Bran k candies before the end of the n-th day.

Examples
input
2 3
1 2
output
2
input
3 17
10 10 10
output
3
input
1 9
10
output
-1
Note

In the first sample, Arya can give Bran 3 candies in 2 days.

In the second sample, Arya can give Bran 17 candies in 3 days, because she can give him at most 8 candies per day.

In the third sample, Arya can't give Bran 9 candies, because she can give him at most 8 candies per day and she must give him the candies within 1 day.

就是个简单模拟

#include <bits/stdc++.h>
using namespace std;
int main()
{
    ios::sync_with_stdio(false);
    int n,k;
    cin>>n>>k;
    int f=n;
    int x=0;
    for(int i=0;i<n;i++){
        int b;
        cin>>b;
        x+=b;
        int mi=min(x,8);
        k-=mi;
        if(x>8)x-=8;else x=0;
        if(k<=0){f=i;
        for(int j=i+1;j<n;j++)
            cin>>x;
        break;
        }
    }

    if(f==n) f=-2;
    cout<<f+1<<endl;
    return 0;
}
C. Journey
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

There are n cities and n - 1 roads in the Seven Kingdoms, each road connects two cities and we can reach any city from any other by the roads.

Theon and Yara Greyjoy are on a horse in the first city, they are starting traveling through the roads. But the weather is foggy, so they can’t see where the horse brings them. When the horse reaches a city (including the first one), it goes to one of the cities connected to the current city. But it is a strange horse, it only goes to cities in which they weren't before. In each such city, the horse goes with equal probabilities and it stops when there are no such cities.

Let the length of each road be 1. The journey starts in the city 1. What is the expected length (expected value of length) of their journey? You can read about expected (average) value by the link https://en.wikipedia.org/wiki/Expected_value.

Input

The first line contains a single integer n (1 ≤ n ≤ 100000) — number of cities.

Then n - 1 lines follow. The i-th line of these lines contains two integers ui and vi (1 ≤ ui, vi ≤ nui ≠ vi) — the cities connected by the i-th road.

It is guaranteed that one can reach any city from any other by the roads.

Output

Print a number — the expected length of their journey. The journey starts in the city 1.

Your answer will be considered correct if its absolute or relative error does not exceed 10 - 6.

Namely: let's assume that your answer is a, and the answer of the jury is b. The checker program will consider your answer correct, if .

Examples
input
4
1 2
1 3
2 4
output
1.500000000000000
input
5
1 2
1 3
3 4
2 5
output
2.000000000000000
Note

In the first sample, their journey may end in cities 3 or 4 with equal probability. The distance to city 3 is 1 and to city 4 is 2, so the expected length is 1.5.

In the second sample, their journey may end in city 4 or 5. The distance to the both cities is 2, so the expected length is 2.

 期望是加权平均,而不是简单平均啊,当时一定是傻了,还一直以为自己想的没有问题

#include <bits/stdc++.h>
using namespace std;
const int N=100005;
vector<int>V[N];
int vis[N];
double ff=0;
void dfs(int x,int l,double c)
{
    vis[x]=1;
    int s=0;
    for(int i=0; i<(int)V[x].size(); i++)
    if(vis[V[x][i]]==0)
    s++;
    for(int i=0; i<(int)V[x].size(); i++)
    {
        if(vis[V[x][i]]==0)
        {
            dfs(V[x][i],l+1,c/s);
        }
    }
    if(s==0)ff+=l*c;

}
int main()
{
    ios::sync_with_stdio(false);
    int n;
    cin>>n;
    memset(vis,0,sizeof(vis));
    for(int i=1; i<n; i++)
    {
        int x,y;
        cin>>x>>y;
        V[x].push_back(y);
        V[y].push_back(x);
    }
    dfs(1,0,1.0);
    printf("%.6f",ff);
    return 0;
}

我想的复杂了,在此看下PP的

#include <bits/stdc++.h>

using namespace std;

const int N = 100005;

vector<int> g[N];

double dfs(int u, int fa) {
  double ret = 0, d = (u == 1 ? 0 : -1) + (int)g[u].size();
  for (auto v : g[u]) {
    if (v != fa) {
      ret += 1.0 / d * (dfs(v, u) + 1);
    }
  }
  return ret;
}

int main(int argc, char *argv[]) {
  cin.sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 1; i < n; i++) {
    int u, v;
    cin >> u >> v;
    g[u].push_back(v);
    g[v].push_back(u);
  }
  printf("%.10f
", dfs(1, 0));
  return 0;
}

写得多好啊

D. Winter is here
time limit per test
3 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Winter is here at the North and the White Walkers are close. John Snow has an army consisting of n soldiers. While the rest of the world is fighting for the Iron Throne, he is going to get ready for the attack of the White Walkers.

He has created a method to know how strong his army is. Let the i-th soldier’s strength be ai. For some k he calls i1, i2, ..., ik a clan if i1 < i2 < i3 < ... < ik and gcd(ai1, ai2, ..., aik) > 1 . He calls the strength of that clan k·gcd(ai1, ai2, ..., aik). Then he defines the strength of his army by the sum of strengths of all possible clans.

Your task is to find the strength of his army. As the number may be very large, you have to print it modulo 1000000007 (109 + 7).

Greatest common divisor (gcd) of a sequence of integers is the maximum possible integer so that each element of the sequence is divisible by it.

Input

The first line contains integer n (1 ≤ n ≤ 200000) — the size of the army.

The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 1000000) — denoting the strengths of his soldiers.

Output

Print one integer — the strength of John Snow's army modulo 1000000007 (109 + 7).

Examples
input
3
3 3 1
output
12
input
4
2 3 4 6
output
39
Note

In the first sample the clans are {1}, {2}, {1, 2} so the answer will be 1·3 + 1·3 + 2·3 = 12

 这个题就是个容斥

#include "bits/stdc++.h"
using namespace std;
const int N = 1e6 + 6;
const int mod = 1e9 + 7;
int n;
int cnt[N];
int pw2[N];
int dp[N];
int inv[N];
int ans;
int main(){
    pw2[0] = 1;
    for(int i = 1 ; i < N ; ++i){
        pw2[i] = (2LL * pw2[i - 1]) % mod;
    }
    scanf("%d" , &n);
    for(int i = 1 ; i <= n ; ++i){
        int tmp;
        scanf("%d" , &tmp);
        ++cnt[tmp];
    }
    inv[0] = 0;
    inv[1] = 1;
    for(int i = 2 ; i < N ; ++i){
        inv[i] = (((1LL * (-(mod / i)) * inv[mod % i]) % mod) + mod) % mod;
    }
    ans = 0;
    for(int i = N - 1 ; i >= 2 ; --i){
        int tot = 0;
        for(int j = i ; j < N ; j += i){
            tot += cnt[j];
        }
        int res = 0;
        if(tot){
            res = (1LL * tot * pw2[tot - 1]) % mod;
            res = (1LL * res * i) % mod;
        }
        for(int j = i + i ; j < N ; j += i){
            res = (res - 1LL * ((1LL * dp[j] * inv[j]) % mod) * i) % mod;
            res += (res < 0) * mod;
        }
        dp[i] = res;
        ans = (ans + res) % mod;
    }
    printf("%d
" , ans);
}
原文地址:https://www.cnblogs.com/BobHuang/p/7357404.html