CodeForces475

A. Splits

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <string>
 6 #include <vector>
 7 #include <set>
 8 #include <list>
 9 #include <map>
10 #include <queue>
11 #include <algorithm>
12 using namespace std;
13 const long maxn=1e5;
14 const long mod=1e9+7;
15 
16 
17 int main()
18 {
19     long n,i;
20     scanf("%ld",&n);
21     printf("%ld",1+n/2);
22     return 0;
23 }

B. Messages

贪心

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <set>
#include <list>
#include <map>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
const long maxn=1e5;
const long mod=1e9+7;


int main()
{
    long n,a,b,c,T,t,v,i,sum=0;
    scanf("%ld%ld%ld%ld%ld",&n,&a,&b,&c,&T);
    sum=a*n;
    for (i=1;i<=n;i++)
    {
        scanf("%ld",&t);
        if (b<=c)
            sum=sum+(c-b)*(T-t);
    }
    cout<<sum<<endl;
    return 0;
}

C. Alternating Sum

快速幂 求逆元 等比数列

1. 1e9+9不是素数

2. 等比数列,公比可能为0

3. 数的数目为n+1

 1 #include <cstdio>
 2 #include <cstdlib>
 3 #include <cmath>
 4 #include <cstring>
 5 #include <string>
 6 #include <vector>
 7 #include <set>
 8 #include <list>
 9 #include <map>
10 #include <queue>
11 #include <iostream>
12 #include <algorithm>
13 using namespace std;
14 const long maxn=1e5;
15 const long long mod=1e9+9;
16 #define ll long long
17 
18 ll x,y;
19 
20 ll mul(ll s,long ci)
21 {
22     ll r=1;
23     while (ci)
24     {
25         if ((ci & 1)==1)
26             r=r*s%mod;
27         s=s*s%mod;
28         ci=ci>>1;
29     }
30     return r;
31 }
32 
33 void gcd(ll a,ll b)
34 {
35     if (b==0)
36     {
37         x=1;
38         y=0;
39     }
40     else
41     {
42         ll r;
43         gcd(b,a%b);
44         r=x;
45         x=y;
46         y=r-a/b*y;
47     }
48 }
49 
50 ll ni(ll s)
51 {
52     gcd(mod,s);
53     return (y%mod+mod)%mod;
54 }
55 
56 int main()
57 {
58     long n,k,i;
59     long long a,b,ni_a,ni_a_k,a_k,b_k,sum,c,d;
60     char ch;
61     scanf("%ld%lld%lld%ld\n",&n,&a,&b,&k);
62     c=mul(a,n);
63     
64     b_k=mul(b,k);
65     a_k=mul(a,k);
66     
67     ni_a=ni(a);
68     ni_a_k=ni(a_k);
69     
70     sum=0;
71     for (i=1;i<=k;i++)
72     {
73         scanf("%c",&ch);
74         if (ch=='+')
75             sum=(sum+c)%mod;
76         else
77             sum=(sum-c+mod)%mod;
78     }
79 
80     d=(n+1)/k;
81     c=ni_a_k * b_k %mod;
82 
83     if (c==1)
84         printf("%lld",sum*d%mod);
85     else
86         printf("%lld",sum* ((mul(c,d)%mod-1+mod)%mod) %mod *ni(c-1)%mod);
87 
88     return 0;
89 }

D. Destruction of a Tree

树结构,从叶子向根,先确定子节点是否删边,然后父节点是否删边就可以确定,当根节点不删边,则成立,否则不成立

输出删点次序:从叶子向根,遇到需要删边的点,则删边,通过遍历所有的子辈节点,遇到不需要删除边的点,则输出,继续遍历,否则结束

  1 #include <cstdio>
  2 #include <cstdlib>
  3 #include <cmath>
  4 #include <cstring>
  5 #include <string>
  6 #include <vector>
  7 #include <set>
  8 #include <list>
  9 #include <map>
 10 #include <queue>
 11 #include <iostream>
 12 #include <algorithm>
 13 using namespace std;
 14 const long maxn=2e5+100;
 15 const long long mod=1e9+9;
 16 #define ll long long
 17 
 18 struct node
 19 {
 20     long d;
 21     struct node *next;
 22 }*point[maxn];
 23 long need[maxn],fa[maxn];
 24 bool vis[maxn],feng[maxn];
 25 long pr[maxn],pr_count=0;
 26 
 27 
 28 void dfs(long d)
 29 {
 30     long cc=0;
 31     vis[d]=true;
 32     struct node *p=point[d];
 33     while (p)
 34     {
 35         if (!vis[p->d])
 36         {
 37             cc++;
 38             dfs(p->d);
 39             need[d]=need[d] ^ need[p->d];
 40         }
 41         else
 42             fa[d]=p->d;
 43         p=p->next;
 44     }
 45 }
 46 
 47 void print(long d)
 48 {
 49     printf("%ld\n",d);
 50     struct node *p=point[d];
 51     while (p)
 52     {
 53         if (fa[d]!=p->d && need[p->d]==1)
 54             print(p->d);
 55         p=p->next;
 56     }
 57 }
 58 
 59 void work(long d)
 60 {
 61     struct node *p=point[d];
 62     struct node *q;
 63     while (p)
 64     {
 65         if (fa[d]!=p->d)
 66             work(p->d);
 67         p=p->next;    
 68     }
 69     if (need[d]==0)
 70     {
 71         printf("%ld\n",d);
 72         p=point[d];
 73         while (p)
 74         {
 75             if (fa[d]!=p->d && need[p->d]==1)
 76                 print(p->d);
 77             p=p->next;
 78         }
 79     }
 80 }
 81 
 82 int main()
 83 {
 84     struct node *p;
 85     long n,root,i,y;
 86     scanf("%ld",&n); 
 87     
 88     for (i=1;i<=n;i++)
 89     {
 90         point[i]=NULL;
 91         need[i]=1;    //0
 92         vis[i]=false;
 93     }
 94     for (i=1;i<=n;i++)
 95     {
 96         scanf("%ld",&y);
 97         if (y==0)
 98             root=i;
 99         else
100         {
101             p=(struct node *) malloc (sizeof(struct node));
102             p->d=y;
103             p->next=point[i];
104             point[i]=p;
105             
106             p=(struct node *) malloc (sizeof(struct node));
107             p->d=i;
108             p->next=point[y];
109             point[y]=p;
110         }
111     }
112     need[root]=0;
113     
114     fa[root]=0;
115     dfs(root);
116     if (need[root]==1)
117     {
118         printf("NO");
119         return 0;
120     }
121     
122     printf("YES\n");
123     work(root);
124     
125     return 0;
126 }
127 /*
128 9
129 0 1 1 2 2 2 3 6 6
130 
131 9
132 0 1 1 1 3 3 3 6 6
133 
134 5
135 0 1 2 3 4
136 */ 

猜你喜欢

转载自www.cnblogs.com/cmyg/p/8874209.html
今日推荐