水。
#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;
}
赛后极其惨烈的一题贪心,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;
}
水题。终点肯定在叶子上,那么直接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;
}
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.
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.
Print one integer — the strength of John Snow's army modulo 1000000007 (109 + 7).
3 3 3 1
12
4 2 3 4 6
39
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 1000000
using 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;
}