当前位置: 首页 > news >正文

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\n", 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\n" , ans);
}

 

转载于:https://www.cnblogs.com/BobHuang/p/7357404.html

相关文章:

  • 【转】搜索算法的剪枝优化
  • vue.js过渡效果之--javascript钩子
  • 吓死猪队友 只用命令行登录Windows就问你怕不怕!
  • 从零开始学习Sencha Touch MVC应用之十四
  • 四 APPIUM GUI讲解(Windows版)(转)
  • net user使用
  • 如何在Ubuntu上使用Grafana监控Docker
  • 电脑快捷键
  • 字符合并[HAOI2016]
  • love——sir thomas browne
  • 开源 java CMS - FreeCMS2.6 积分记录
  • 个人记事本-介绍
  • 如何让Ubuntu在老旧设备上飞速运行!
  • Redis事件驱动库转
  • 开源 java CMS - FreeCMS2.6 站内信
  • 【刷算法】从上往下打印二叉树
  • 2018一半小结一波
  • emacs初体验
  • in typeof instanceof ===这些运算符有什么作用
  • Java教程_软件开发基础
  • LeetCode541. Reverse String II -- 按步长反转字符串
  • SQLServer插入数据
  • 对象管理器(defineProperty)学习笔记
  • 前端之Sass/Scss实战笔记
  • 微服务框架lagom
  • 智能合约Solidity教程-事件和日志(一)
  • ​VRRP 虚拟路由冗余协议(华为)
  • # 日期待t_最值得等的SUV奥迪Q9:空间比MPV还大,或搭4.0T,香
  • #define 用法
  • #Ubuntu(修改root信息)
  • %check_box% in rails :coditions={:has_many , :through}
  • (12)目标检测_SSD基于pytorch搭建代码
  • (4)事件处理——(6)给.ready()回调函数传递一个参数(Passing an argument to the .ready() callback)...
  • (Matalb时序预测)PSO-BP粒子群算法优化BP神经网络的多维时序回归预测
  • (react踩过的坑)Antd Select(设置了labelInValue)在FormItem中initialValue的问题
  • (react踩过的坑)antd 如何同时获取一个select 的value和 label值
  • (四)鸿鹄云架构一服务注册中心
  • (推荐)叮当——中文语音对话机器人
  • ***利用Ms05002溢出找“肉鸡
  • .\OBJ\test1.axf: Error: L6230W: Ignoring --entry command. Cannot find argumen 'Reset_Handler'
  • .naturalWidth 和naturalHeight属性,
  • .NET 设计一套高性能的弱事件机制
  • .net 生成二级域名
  • .NET与 java通用的3DES加密解密方法
  • @javax.ws.rs Webservice注解
  • @SuppressLint(NewApi)和@TargetApi()的区别
  • [20150707]外部表与rowid.txt
  • [ACTF2020 新生赛]Include
  • [Android Pro] AndroidX重构和映射
  • [android] 天气app布局练习
  • [autojs]逍遥模拟器和vscode对接
  • [AutoSAR 存储] 汽车智能座舱的存储需求
  • [AX]AX2012开发新特性-禁止表或者表字段
  • [bzoj2957]楼房重建
  • [c#基础]值类型和引用类型的Equals,==的区别