Codeforces Round #428 (Div. 2) A B C D

来源:互联网 发布:淘宝网国际版 编辑:程序博客网 时间:2024/04/24 05:26

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 ≤ 1001 ≤ 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 31 2
output
2
input
3 1710 10 10
output
3
input
1 910
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 8candies 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 <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;const int maxn=105,inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f;   const ld pi=acos(-1.0L); int a[maxn];int main() {    int n,i,k,sum=0,p=0;    scanf("%d%d",&n,&k);    for (i=1;i<=n;i++) {    scanf("%d",&a[i]);    }    for (i=1;i<=n;i++) {    sum+=a[i];    if (sum>=8) p+=8,sum-=8; else {    p+=sum;    sum=0;    }    if (p>=k) break;    }    if (i!=n+1) printf("%d",i); else printf("-1");return 0;}

B. Game of the Rows
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Daenerys Targaryen has an army consisting of k groups of soldiers, the i-th group contains ai soldiers. She wants to bring her army to the other side of the sea to get the Iron Throne. She has recently bought an airplane to carry her army through the sea. The airplane has n rows, each of them has 8 seats. We call two seats neighbor, if they are in the same row and in seats {1, 2}{3, 4}{4, 5}{5, 6} or {7, 8}.

A row in the airplane

Daenerys Targaryen wants to place her army in the plane so that there are no two soldiers from different groups sitting on neighboring seats.

Your task is to determine if there is a possible arranging of her army in the airplane such that the condition above is satisfied.

Input

The first line contains two integers n and k (1 ≤ n ≤ 100001 ≤ k ≤ 100) — the number of rows and the number of groups of soldiers, respectively.

The second line contains k integers a1, a2, a3, ..., ak (1 ≤ ai ≤ 10000), where ai denotes the number of soldiers in the i-th group.

It is guaranteed that a1 + a2 + ... + ak ≤ 8·n.

Output

If we can place the soldiers in the airplane print "YES" (without quotes). Otherwise print "NO" (without quotes).

You can choose the case (lower or upper) for each letter arbitrary.

Examples
input
2 25 8
output
YES
input
1 27 1
output
NO
input
1 24 4
output
YES
input
1 42 2 1 2
output
YES
Note

In the first sample, Daenerys can place the soldiers like in the figure below:

In the second sample, there is no way to place the soldiers in the plane since the second group soldier will always have a seat neighboring to someone from the first group.

In the third example Daenerys can place the first group on seats (1, 2, 7, 8), and the second group an all the remaining seats.

In the fourth example she can place the first two groups on seats (1, 2) and (7, 8), the third group on seats (3), and the fourth group on seats (5, 6).




赛后极其惨烈的一题贪心,20几个friend里面一大半fst,最后没有一个人过。


把每队的人数按照mod 4的结果分开,大力讨论一波。

先把mod4为1和2的多余的3个人尽量放在中间的4个里。

再放mod4为0和3的,尽量放中间。

这时,再回来放多出来的mod4为2的,把他们6人一组放在中间占8个位置。坐法:1 1 空 2 / 3 3 空 2

再把多出来的mod4为2的尽量放在两边。

再把之前没有放的所有人放好。

啊,终于讲完了,好累~~~

这题不知道交了多少发才过



#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double ld;const int maxn=105,inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f;   const ld pi=acos(-1.0L); int a[maxn]; int main() {int n,k,i,center,side;scanf("%d%d",&n,&k);for (i=1;i<=k;i++) {scanf("%d",&a[i]);}center=side=2*n;if (side<0) {printf("NO");return 0;}int one,two;one=two=0;for (i=1;i<=k;i++) {if (a[i]%4==2) two++;if (a[i]%4==1) one++;}int num=min(one,two);two-=num;one-=num;num*=2;if (center-num>=0) {center-=num;num=0;} else {num-=center/2*2;center%=2;}for (i=1;i<=k;i++) {if (a[i]%4==0) {if (center-a[i]/2>=0) center-=a[i]/2; else {side-=a[i]/2-center;center=0;}}}for (i=1;i<=k;i++) {if (a[i]%4==3) {if (center-a[i]/2-1>=0) center-=a[i]/2+1; else {side-=a[i]/2+1-center;center=0;}}}one+=num/2;two+=num/2;int p=min(center/4,two/3);two-=p*3;center-=p*4;for (i=1;i<=two;i++) {if (side>0) side--; else center-=2;}int sum=center+side;sum-=one;for (i=1;i<=k;i++) {if (a[i]%4==2) sum-=a[i]/2-1;if (a[i]%4==1) sum-=a[i]/2;}if (sum<0) {printf("NO");return 0;}printf("YES");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
41 21 32 4
output
1.500000000000000
input
51 21 33 42 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 1and 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.





水题。终点肯定在叶子上,那么直接dfs就好。

害怕卡精度,专门用了long double


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <iomanip>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))using namespace std;typedef long long ll;typedef long double db;const int maxn=100005,inf=0x3f3f3f3f;const ll llinf=0x3f3f3f3f3f3f3f3f; int head[maxn];int num;bool visit[maxn];db ans;struct Edge {int from,to,pre;};Edge edge[maxn*2];void addedge(int from,int to) {edge[num]=(Edge){from,to,head[from]};head[from]=num++;edge[num]=(Edge){to,from,head[to]};head[to]=num++;}void dfs(int now,db step,db p) {visit[now]=1;db m=0.0L;for (int i=head[now];i!=-1;i=edge[i].pre) {int to=edge[i].to;if (!visit[to]) m+=1.0L;}if (!m) {ans+=step*p;return;}for (int i=head[now];i!=-1;i=edge[i].pre) {int to=edge[i].to;if (!visit[to]) dfs(to,step+1.0L,p/m);}}int main() {int n,i,x,y;num=0;mem0(visit);memset(head,-1,sizeof(head));scanf("%d",&n);for (i=1;i<n;i++) {scanf("%d%d",&x,&y);addedge(x,y);}dfs(1,0.0L,1.0L);cout << setiosflags(ios::fixed) << setprecision(10);cout << ans;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 nsoldiers. 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 khe 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
33 3 1
output
12
input
42 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



题意非常明确,不需要会英文也能知道求什么。


上组合数学:

设 i 的倍数有n个,则从中选择长度为1,2,...k...,n-1,n的下标递增的序列共有


这么多种。

颇有当年江苏高考附加卷最后一题的感觉~

(字好丑。。。有时间去学一下LaTex)

要求gcd恰好为 i 的序列个数,而这么多种当中,可能有gcd比 i 大的序列,这时,需要把这些多加的减掉。

怎么减?一个个往后找呗,反正时间给了3s。减去所有gcd为 i 的倍数的序列的个数,最后就可以求出gcd为 i 的序列个数,每次乘 i , 最后加起来就是答案。


#include <cstdio>#include <iostream>#include <string.h>#include <string> #include <map>#include <queue>#include <vector>#include <set>#include <algorithm>#include <math.h>#include <cmath>#include <stack>#define mem0(a) memset(a,0,sizeof(a))#define meminf(a) memset(a,0x3f,sizeof(a))#define N 1000000using namespace std;typedef long long ll;typedef long double ld;const int maxn=1000005,inf=0x3f3f3f3f;  const ll llinf=0x3f3f3f3f3f3f3f3f,mod=1e9+7;   const ld pi=acos(-1.0L);  ll cnt[maxn],f[maxn],g[maxn];ll fastpower(ll base,int index) {ll k=index,ans=1,l=base;while (k) {if (k%2) ans*=l;ans%=mod;k/=2;l*=l;l%=mod;}return ans;}int main() {int n,i,j,x;mem0(cnt);scanf("%d",&n);for (i=1;i<=n;i++) {scanf("%d",&x);cnt[x]++;}ll k,ans=0;for (i=2;i<=N;i++) {k=0;for (j=i;j<=N;j+=i) {k+=cnt[j];}if (k==0) f[i]=0; else f[i]=k*fastpower(2,k-1);f[i]%=mod;}mem0(g);for (i=N;i>1;i--) {g[i]=f[i];for (j=i*2;j<=N;j+=i) {g[i]-=g[j];if (g[i]<0) g[i]+=mod;}ans+=g[i]*((ll)(i));ans%=mod;}printf("%I64d\n",ans);return 0;}