Codeforces 337
A:
题意不知道,似乎是排序。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstdlib>
3 #include<cstring>
4 #include<algorithm>
5
6 using namespace std;
7
8 const int maxn=60;
9
10 int n,m,z[maxn];
11
12 int main()
13 {
14 scanf("%d%d",&n,&m);
15 for (int a=1;a<=m;a++)
16 scanf("%d",&z[a]);
17 sort(z+1,z+m+1);
18 int ans=z[n]-z[1];
19 for (int a=n+1;a<=m;a++)
20 ans=min(ans,z[a]-z[a-n+1]);
21 printf("%d\n",ans);
22
23 return 0;
24 }
B:
题意是给你一个图片横竖比和一个屏幕横竖比,问最后最少剩下多少的剩余空间。
算一算是横着顶满还是竖着顶满就行了。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstdlib>
3 #include<cstring>
4 #include<algorithm>
5
6 using namespace std;
7
8 int a,b,c,d;
9
10 int gcd(int a,int b)
11 {
12 if (!b) return a;
13 else return gcd(b,a%b);
14 }
15
16 int main()
17 {
18 scanf("%d%d%d%d",&a,&b,&c,&d);
19 if (a*d<b*c) swap(a,b),swap(c,d);
20 int v1=b*c;
21 int v2=a*d;
22 int g=gcd(v1,v2);
23 v1/=g;
24 v2/=g;
25 printf("%d/%d\n",v2-v1,v2);
26
27 return 0;
28 }
C:
答对一题得1分,连续答对k次翻倍,给出题目数和答对了多少题,问少得多少分。
每K次分为一段,每段答对K-1道题,如果还有剩余的答对题数,则尽量往前面补空,用矩阵推出答案即可。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstdlib>
3 #include<cstring>
4 #include<algorithm>
5
6 using namespace std;
7
8 #define inc(a,b) {a+=b;if (a>=mo) a-=mo;}
9
10 const int mo=1000000009;
11
12 int n,m,k,m1[3],m2[3][3],m3[3],m4[3][3];
13
14 int main()
15 {
16 scanf("%d%d%d",&n,&m,&k);
17 int rest=m-(n%k+n/k*(k-1));
18 rest=max(rest,0);
19 m1[1]=0;m1[2]=2*k;
20 m2[1][1]=2;m2[1][2]=0;
21 m2[2][1]=1;m2[2][2]=1;
22 while (rest)
23 {
24 if (rest&1)
25 {
26 memset(m3,0,sizeof(m3));
27 for (int a=1;a<=1;a++)
28 for (int b=1;b<=2;b++)
29 for (int c=1;c<=2;c++)
30 inc(m3[c],(long long)m1[b]*m2[b][c]%mo);
31 for (int a=1;a<=2;a++)
32 m1[a]=m3[a];
33 }
34 memset(m4,0,sizeof(m4));
35 for (int a=1;a<=2;a++)
36 for (int b=1;b<=2;b++)
37 for (int c=1;c<=2;c++)
38 inc(m4[a][c],(long long)m2[a][b]*m2[b][c]%mo);
39 for (int a=1;a<=2;a++)
40 for (int b=1;b<=2;b++)
41 m2[a][b]=m4[a][b];
42 rest>>=1;
43 }
44 int ans=m1[1];
45 rest=m-(n%k+n/k*(k-1));
46 rest=max(rest,0);
47 rest=m-rest*k;
48 inc(ans,rest);
49 printf("%d\n",ans);
50
51 return 0;
52 }
D:
告诉你某些结点被污染了,并且污染源的可污染距离不超过某个值,问可能为污染源的点。
枚举结点,维护被污染结点到其距离的最大值。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstdlib>
3 #include<cstring>
4 #include<algorithm>
5
6 using namespace std;
7
8 const int maxn=100010;
9
10 int n,m,d,en,ans,f[maxn][2];
11
12 bool ins[maxn];
13
14 struct edge
15 {
16 int e;
17 edge *next;
18 }*v[maxn],ed[maxn<<1];
19
20 void add_edge(int s,int e)
21 {
22 en++;
23 ed[en].next=v[s];v[s]=ed+en;v[s]->e=e;
24 }
25
26 void dfs(int now,int pre)
27 {
28 for (edge *e=v[now];e;e=e->next)
29 if (e->e!=pre) dfs(e->e,now);
30 f[now][0]=f[now][1]=-1;
31 for (edge *e=v[now];e;e=e->next)
32 if (e->e!=pre)
33 {
34 int v=f[e->e][0];
35 if (v!=-1) v++;
36 if (ins[e->e]) v=max(v,1);
37 if (v!=-1)
38 {
39 if (v>=f[now][0]) f[now][1]=f[now][0],f[now][0]=v;
40 else
41 {
42 if (v>=f[now][1]) f[now][1]=v;
43 }
44 }
45 }
46 }
47
48 void solve(int now,int pre,int pred)
49 {
50 if (f[now][0]<=d && pred<=d) ans++;
51 for (edge *e=v[now];e;e=e->next)
52 if (e->e!=pre)
53 {
54 int v=pred;
55 if (v!=-1) v++;
56 if (ins[now]) v=max(v,1);
57 int nd=f[e->e][0];
58 if (nd==-1)
59 {
60 if (ins[e->e]) nd=1;
61 }
62 else nd++;
63 if (nd==f[now][0])
64 {
65 if (f[now][1]!=-1) v=max(v,f[now][1]+1);
66 }
67 else
68 {
69 if (f[now][0]!=-1) v=max(v,f[now][0]+1);
70 }
71 solve(e->e,now,v);
72 }
73 }
74
75 int main()
76 {
77 scanf("%d%d%d",&n,&m,&d);
78 for (int a=1;a<=m;a++)
79 {
80 int p;
81 scanf("%d",&p);
82 ins[p]=true;
83 }
84 for (int a=1;a<n;a++)
85 {
86 int s,e;
87 scanf("%d%d",&s,&e);
88 add_edge(s,e);
89 add_edge(e,s);
90 }
91 dfs(1,0);
92 solve(1,0,-1);
93 printf("%d\n",ans);
94
95 return 0;
96 }
E:
告诉你n个必须在因子数上出现的数,问最小所需要的结点数。
爆搜。hza说状压但我不会。
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<cstdio>
2 #include<cstdlib>
3 #include<cstring>
4 #include<algorithm>
5
6 using namespace std;
7
8 int n,ans=0x3f3f3f3f,v[10],vx[10];
9
10 long long z[10],y[10];
11
12 bool prime[10];
13
14 int calc(long long b)
15 {
16 int ans=0;
17 for (long long a=2;a*a<=b;a++)
18 while (b%a==0)
19 ans++,b/=a;
20 if (b!=1) ans++;
21 return ans;
22 }
23
24 void dfs(int now,int nownum,int cnt)
25 {
26 if (nownum>=ans) return;
27 if (now>n)
28 {
29 if (cnt>=2) nownum++;
30 ans=min(ans,nownum);
31 return;
32 }
33 for (int a=now+1;a<=n;a++)
34 if (y[a]%z[now]==0)
35 {
36 y[a]/=z[now];
37 vx[a]-=v[now];
38 dfs(now+1,nownum+vx[now]+(prime[now]!=true),cnt);
39 y[a]*=z[now];
40 vx[a]+=v[now];
41 }
42 dfs(now+1,nownum+vx[now]+(prime[now]!=true),cnt+1);
43 }
44
45 int main()
46 {
47 scanf("%d",&n);
48 for (int a=1;a<=n;a++)
49 scanf("%I64d",&z[a]);
50 sort(z+1,z+n+1);
51 for (int a=1;a<=n;a++)
52 y[a]=z[a];
53 for (int a=1;a<=n;a++)
54 {
55 v[a]=vx[a]=calc(z[a]);
56 if (v[a]==1) prime[a]=true;
57 }
58 dfs(1,0,0);
59 printf("%d\n",ans);
60
61 return 0;
62 }