Gym 101933

B. Baby Bites水题直接模拟即可

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <set>
 7 #include <iostream>
 8 #include <map>
 9 #include <stack>
10 #include <string>
11 #include <vector>
12 #define  pi acos(-1.0)
13 #define  eps 1e-9
14 #define  fi first
15 #define  se second
16 #define  rtl   rt<<1
17 #define  rtr   rt<<1|1
18 #define  bug         printf("******\n")
19 #define  mem(a,b)    memset(a,b,sizeof(a))
20 #define  name2str(x) #x
21 #define  fuck(x)     cout<<#x" = "<<x<<endl
22 #define  f(a)        a*a
23 #define  sf(n)       scanf("%d", &n)
24 #define  sff(a,b)    scanf("%d %d", &a, &b)
25 #define  sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
26 #define  sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
27 #define  pf          printf
28 #define  FRE(i,a,b)  for(i = a; i <= b; i++)
29 #define  FREE(i,a,b) for(i = a; i >= b; i--)
30 #define  FRL(i,a,b)  for(i = a; i < b; i++)+
31 #define  FRLL(i,a,b) for(i = a; i > b; i--)
32 #define  FIN         freopen("data.txt","r",stdin)
33 #define  gcd(a,b)    __gcd(a,b)
34 #define  lowbit(x)   x&-x
35 #define rep(i,a,b) for(int i=a;i<b;++i)
36 #define per(i,a,b) for(int i=a-1;i>=b;--i)
37 using namespace std;
38 typedef long long  LL;
39 typedef unsigned long long ULL;
40 const int INF = 0x3f3f3f3f;
41 const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
42 const int maxn = 1e5 + 10;
43 int n;
44 int main() {
45     sf ( n );
46     char op[10];
47     int flag = 1;
48     for ( int i = 1 ; i <= n ; i++ ) {
49         scanf ( "%s", op );
50         if ( op[0] >= ‘0‘ && op[0] <= ‘9‘ ) {
51             int temp = 0;
52             int len = strlen ( op );
53             for ( int j = 0 ; j < len ; j++ ) temp = temp * 10 + op[j] - ‘0‘;
54             if ( temp != i ) flag = 0;
55         }
56     }
57     if ( flag ) printf ( "makes sense\n" );
58     else printf ( "something is fishy\n" );
59     return 0;
60 }

C. Code Cleanups(难点)在于读题 读题读到自闭

题意: 给你一个数组a[ ]  表示a[ i ]产生一个垃圾,之后每天肮脏度增加1 ,肮脏度到达20时必须清理,问最少清理天数(注意每年的结束要将肮脏度变为0)

直接模拟即可

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <set>
 7 #include <iostream>
 8 #include <map>
 9 #include <stack>
10 #include <string>
11 #include <vector>
12 #define  pi acos(-1.0)
13 #define  eps 1e-9
14 #define  fi first
15 #define  se second
16 #define  rtl   rt<<1
17 #define  rtr   rt<<1|1
18 #define  bug         printf("******\n")
19 #define  mem(a,b)    memset(a,b,sizeof(a))
20 #define  name2str(x) #x
21 #define  fuck(x)     cout<<#x" = "<<x<<endl
22 #define  f(a)        a*a
23 #define  sf(n)       scanf("%d", &n)
24 #define  sff(a,b)    scanf("%d %d", &a, &b)
25 #define  sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
26 #define  sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
27 #define  pf          printf
28 #define  FRE(i,a,b)  for(i = a; i <= b; i++)
29 #define  FREE(i,a,b) for(i = a; i >= b; i--)
30 #define  FRL(i,a,b)  for(i = a; i < b; i++)+
31 #define  FRLL(i,a,b) for(i = a; i > b; i--)
32 #define  FIN         freopen("data.txt","r",stdin)
33 #define  gcd(a,b)    __gcd(a,b)
34 #define  lowbit(x)   x&-x
35 #define rep(i,a,b) for(int i=a;i<b;++i)
36 #define per(i,a,b) for(int i=a-1;i>=b;--i)
37 using namespace std;
38 typedef long long  LL;
39 typedef unsigned long long ULL;
40 const int INF = 0x3f3f3f3f;
41 const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
42 const int maxn = 1e5 + 10;
43 int n, vis[maxn];
44 int main() {
45     sf ( n );
46     for ( int i = 0, x ; i < n ; i++ ) sf ( x ), vis[x] = 1;
47     int sum = 0, cnt = 0, ans = 0;
48     for ( int i = 1 ; i <= 365 ; i++ ) {
49         sum += cnt;
50         if ( sum >= 20 ) sum = 0, ans++, cnt = 0;
51         if ( vis[i] ) cnt++;
52     }
53     if ( cnt || sum ) ans++;
54     printf ( "%d\n", ans );
55     return 0;
56 }

E. Explosion Exploit 记忆化搜索

题意:你有n个士兵,敌方有m个士兵,每一个士兵都有一定的血量(最大为6),如果血量归零,则证明该士兵死亡。现在有d点伤害,每一点伤害都会以等概率分配给任意一个人。现在问你,敌方的m个士兵全都阵亡的概率。

题解:总共只有12个士兵用一个long long 就可以存好状态了 当status<1000000时表示敌军已经死完了

这个概率是倒推过来的

Dfs(status,num)表示状态为status,剩下num点伤害,对方阵亡的概率

ans += ( double ) mp[i][j] / ( double ) sum * res;//表示转移到下一个状态的概率

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <set>
 7 #include <iostream>
 8 #include <map>
 9 #include <stack>
10 #include <string>
11 #include <vector>
12 #include <bits/stdc++.h>
13 #define  pi acos(-1.0)
14 #define  eps 1e-9
15 #define  fi first
16 #define  se second
17 #define  rtl   rt<<1
18 #define  rtr   rt<<1|1
19 #define  bug         printf("******\n")
20 #define  mem(a,b)    memset(a,b,sizeof(a))
21 #define  name2str(x) #x
22 #define  fuck(x)     cout<<#x" = "<<x<<endl
23 #define  f(a)        a*a
24 #define  sf(n)       scanf("%d", &n)
25 #define  sff(a,b)    scanf("%d %d", &a, &b)
26 #define  sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
27 #define  sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
28 #define  pf          printf
29 #define  FRE(i,a,b)  for(i = a; i <= b; i++)
30 #define  FREE(i,a,b) for(i = a; i >= b; i--)
31 #define  FRL(i,a,b)  for(i = a; i < b; i++)+
32 #define  FRLL(i,a,b) for(i = a; i > b; i--)
33 #define  FIN         freopen("data.txt","r",stdin)
34 #define  gcd(a,b)    __gcd(a,b)
35 #define  lowbit(x)   x&-x
36 #define rep(i,a,b) for(int i=a;i<b;++i)
37 #define per(i,a,b) for(int i=a-1;i>=b;--i)
38 using namespace std;
39 typedef long long  LL;
40 typedef unsigned long long ULL;
41 const int INF = 0x3f3f3f3f;
42 const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
43 const int maxn = 2505;
44 const int mod = 1e9 + 7;
45 int n, m, d, mp[2][10];
46 unordered_map<LL, double>dp;
47 LL getstatus() {
48     LL temp = 0;
49     for ( int i = 1 ; i <= 6 ; i++ ) temp = temp * 10 + mp[1][i];
50     for ( int i = 1 ; i <= 6 ; i++ ) temp = temp * 10 + mp[0][i];
51     return temp;
52 }
53 double dfs ( LL status, int num ) {
54     if ( status < 1000000 ) return 1;
55     if ( num == 0 ) return 0;
56     if (dp.count(status)) return dp[status];
57     double ans = 0;
58     int sum = 0;
59     for ( int i = 0 ; i < 2 ; i++ )
60         for ( int j = 1 ; j <= 6 ; j++ ) sum += mp[i][j];
61     for ( int i = 0 ; i < 2 ; i++ ) {
62         for ( int j = 1 ; j <= 6 ; j++ ) {
63             if ( !mp[i][j] ) continue;
64             mp[i][j]--, mp[i][j - 1]++;
65             LL sta=getstatus();
66             double res = dfs ( sta, num - 1 );
67             dp[sta]=res;
68             mp[i][j - 1]--, mp[i][j]++;
69             ans += ( double ) mp[i][j] / ( double ) sum * res;
70         }
71     }
72     return ans;
73 }
74
75 int main() {
76     sfff ( n, m, d );
77     for ( int i = 1, x ; i <= n ; i++ ) {
78         sf ( x );
79         mp[0][x]++;
80     }
81     for ( int i = 1, x; i <= m; i++ ) {
82         sf ( x );
83         mp[1][x]++;
84     }
85     double ans = dfs ( getstatus(), d );
86     printf ( "%.8f\n", ans );
87     return 0 ;
88 }

H. House Lawn

有一个 l 平方米的草坪,给你 m 台割草机。选择其中一台,在一周(10080分钟)的时间内割完草坪。每台割草机输入格式为:“名字,价格,割草效率,工作时间,充电时间” ;

要求选出价格最低的满足条件的一台,如果有多台满足条件且价格相同,按照输入顺序输出割草机名字。

一开始我以为只要一周内可以完成一次就好了,然后拿int写的,后面仔细看题要保证T周也必须清理T次,用int写的话你这周可以清理完,但是下周不一定。所有这题的就相当于充t分钟电能给割草(t/充电时间)*割草时间 所以用double写这题

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <set>
 7 #include <iostream>
 8 #include <map>
 9 #include <stack>
10 #include <string>
11 #include <vector>
12 #define  pi acos(-1.0)
13 #define  eps 1e-9
14 #define  fi first
15 #define  se second
16 #define  rtl   rt<<1
17 #define  rtr   rt<<1|1
18 #define  bug         printf("******\n")
19 #define  mem(a,b)    memset(a,b,sizeof(a))
20 #define  name2str(x) #x
21 #define  fuck(x)     cout<<#x" = "<<x<<endl
22 #define  f(a)        a*a
23 #define  sf(n)       scanf("%d", &n)
24 #define  sff(a,b)    scanf("%d %d", &a, &b)
25 #define  sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
26 #define  sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
27 #define  pf          printf
28 #define  FRE(i,a,b)  for(i = a; i <= b; i++)
29 #define  FREE(i,a,b) for(i = a; i >= b; i--)
30 #define  FRL(i,a,b)  for(i = a; i < b; i++)+
31 #define  FRLL(i,a,b) for(i = a; i > b; i--)
32 #define  FIN         freopen("data.txt","r",stdin)
33 #define  gcd(a,b)    __gcd(a,b)
34 #define  lowbit(x)   x&-x
35 #define rep(i,a,b) for(int i=a;i<b;++i)
36 #define per(i,a,b) for(int i=a-1;i>=b;--i)
37 using namespace std;
38 typedef long long  LL;
39 typedef unsigned long long ULL;
40 const int INF = 0x3f3f3f3f;
41 const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
42 const int maxn = 1e2 + 5;
43 string s[maxn], s1;
44 LL a[maxn], l, n, cnt[10], vis[maxn];
45 int main() {
46     //  FIN;
47     cin >> l >> n;
48     getchar();
49     int ans = INF;
50     for ( int i = 0; i < n ; i++ ) {
51         getline ( cin, s1 );
52         int flag = 0, temp;
53         for ( int j = 0 ; j < s1.size() ; j++ ) {
54             if ( s1[j] == ‘,‘ ) flag++, cnt[flag] = temp, temp = 0;
55             else if ( !flag ) s[i] += s1[j];
56             else  temp = temp * 10 + s1[j] - ‘0‘;
57         }
58         cnt[++flag] = temp;
59         a[i] = cnt[2];
60 //        int num = 10080 / ( cnt[4] + cnt[5] ), res = 10080 % ( cnt[4] + cnt[5] );
61 //        for (int i=2 ;i<=5 ;i++) printf("%d%c",cnt[i],i==5?‘\n‘:‘ ‘);
62 //        printf ( "num = %d res = %d\n", num, res );
63 //        LL sum = 0;
64 //        if ( res >= cnt[4] ) sum = 1LL * ( num + 1 ) * 1LL * cnt[4] * 1LL * cnt[3];
65 //        if ( res < cnt[4] ) sum = 1LL * num * 1LL * cnt[4] * 1LL * cnt[3] + 1LL * res * 1LL * cnt[3];
66         if ( ( double ) ( ( double ) 10080 / ( ( double ) ( cnt[4] + cnt[5] ) ) * ( double ) cnt[3] * ( double ) cnt[4]  ) >=  1.0 * l && ans >= a[i] ) ans = cnt[2],vis[i]=1;
67     }
68     if ( ans == INF ) printf ( "no such mower\n" );
69     for ( int i = 0 ; i < n ; i++ ) if ( ans == a[i] && vis[i]) cout << s[i] << endl;
70     return 0;
71 }

Intergalactic Bidding

有n个人的名字和值,每个人的值相差两倍以上,问是否有一种方案使部分人的值加起来为S

大数直接模拟就好了(相差2倍以上,只有唯一解)

  1 #include <cstdio>
  2 #include <cstring>
  3 #include <queue>
  4 #include <cmath>
  5 #include <algorithm>
  6 #include <set>
  7 #include <iostream>
  8 #include <map>
  9 #include <stack>
 10 #include <string>
 11 #include <vector>
 12 #define  pi acos(-1.0)
 13 #define  eps 1e-9
 14 #define  fi first
 15 #define  se second
 16 #define  rtl   rt<<1
 17 #define  rtr   rt<<1|1
 18 #define  bug         printf("******\n")
 19 #define  mem(a,b)    memset(a,b,sizeof(a))
 20 #define  name2str(x) #x
 21 #define  fuck(x)     cout<<#x" = "<<x<<endl
 22 #define  f(a)        a*a
 23 #define  sf(n)       scanf("%d", &n)
 24 #define  sff(a,b)    scanf("%d %d", &a, &b)
 25 #define  sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
 26 #define  sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
 27 #define  pf          printf
 28 #define  FRE(i,a,b)  for(i = a; i <= b; i++)
 29 #define  FREE(i,a,b) for(i = a; i >= b; i--)
 30 #define  FRL(i,a,b)  for(i = a; i < b; i++)+
 31 #define  FRLL(i,a,b) for(i = a; i > b; i--)
 32 #define  FIN         freopen("data.txt","r",stdin)
 33 #define  gcd(a,b)    __gcd(a,b)
 34 #define  lowbit(x)   x&-x
 35 #define rep(i,a,b) for(int i=a;i<b;++i)
 36 #define per(i,a,b) for(int i=a-1;i>=b;--i)
 37 using namespace std;
 38 typedef long long  LL;
 39 typedef unsigned long long ULL;
 40 const int INF = 0x3f3f3f3f;
 41 const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
 42 const int MAXL = 2500;
 43 const int MAXN = 9999;
 44 const int DLEN = 4;
 45 class Big {
 46 public:
 47     int a[MAXL], len;
 48     Big ( const int b = 0 ) {
 49         int c, d = b;
 50         len = 0;
 51         memset ( a, 0, sizeof ( a ) );
 52         while ( d > MAXN ) {
 53             c = d - ( d / ( MAXN + 1 ) ) * ( MAXN + 1 );
 54             d = d / ( MAXN + 1 );
 55             a[len++] = c;
 56         }
 57         a[len++] = d;
 58     }
 59     Big ( const char *s ) {
 60         int t, k, index, L;
 61         memset ( a, 0, sizeof ( a ) );
 62         L = strlen ( s );
 63         len = L / DLEN;
 64         if ( L % DLEN ) len++;
 65         index = 0;
 66         for ( int i = L - 1; i >= 0; i -= DLEN ) {
 67             t = 0;
 68             k = i - DLEN + 1;
 69             if ( k < 0 ) k = 0;
 70             for ( int j = k; j <= i; j++ ) t = t * 10 + s[j] - ‘0‘;
 71             a[index++] = t;
 72         }
 73     }
 74     Big operator/ ( const LL &b ) const {
 75         Big ret;
 76         LL down = 0;
 77         for ( int i = len - 1; i >= 0; i-- ) {
 78             ret.a[i] = ( a[i] + down * ( MAXN + 1 ) ) / b;
 79             down = a[i] + down * ( MAXN + 1 ) - ret.a[i] * b;
 80         }
 81         ret.len = len;
 82         while ( ret.a[ret.len - 1] == 0 && ret.len > 1 ) ret.len--;
 83         return ret;
 84     }
 85     bool operator> ( const Big &T ) const {
 86         int ln;
 87         if ( len > T.len ) return true;
 88         else if ( len == T.len ) {
 89             ln = len - 1;
 90             while ( a[ln] == T.a[ln] && ln >= 0 ) ln--;
 91             if ( ln >= 0 && a[ln] > T.a[ln] ) return true;
 92             else return false;
 93         } else return false;
 94     }
 95     Big operator+ ( const Big &T ) const {
 96         Big t ( *this );
 97         int big = T.len > len ? T.len : len;
 98         for ( int i = 0; i < big; i++ ) {
 99             t.a[i] += T.a[i];
100             if ( t.a[i] > MAXN ) {
101                 t.a[i + 1]++;
102                 t.a[i] -= MAXN + 1;
103             }
104         }
105         if ( t.a[big] != 0 ) t.len = big + 1;
106         else t.len = big;
107         return t;
108     }
109     Big operator- ( const Big &T ) const {
110         int big;
111         bool flag;
112         Big t1, t2;
113         if ( *this > T ) {
114             t1 = *this;
115             t2 = T;
116             flag = 0;
117         } else {
118             t1 = T;
119             t2 = *this;
120             flag = 1;
121         }
122         big = t1.len;
123         for ( int i = 0; i < big; i++ ) {
124             if ( t1.a[i] < t2.a[i] ) {
125                 int j = i + 1;
126                 while ( t1.a[j] == 0 ) j++;
127                 t1.a[j--]--;
128                 while ( j > i ) t1.a[j--] += MAXN;
129                 t1.a[i] += MAXN + 1 - t2.a[i];
130             } else t1.a[i] -= t2.a[i];
131         }
132         t1.len = big;
133         while ( t1.a[t1.len - 1] == 0 && t1.len > 1 ) {
134             t1.len--;
135             big--;
136         }
137         if ( flag ) t1.a[big - 1] = 0 - t1.a[big - 1];
138         return t1;
139     }
140     LL operator% ( const int &b ) const {
141         LL d = 0;
142         for ( int i = len - 1; i >= 0; i-- ) d = ( ( d * ( MAXN + 1 ) ) % b + a[i] ) % b;
143         return d;
144     }
145     Big operator* ( const Big &T ) const {
146         Big ret;
147         int i, j, up, temp, temp1;
148         for ( i = 0; i < len; i++ ) {
149             up = 0;
150             for ( j = 0; j < T.len; j++ ) {
151                 temp = a[i] * T.a[j] + ret.a[i + j] + up;
152                 if ( temp > MAXN ) {
153                     temp1 = temp - temp / ( MAXN + 1 ) * ( MAXN + 1 );
154                     up = temp / ( MAXN + 1 );
155                     ret.a[i + j] = temp1;
156                 } else {
157                     up = 0;
158                     ret.a[i + j] = temp;
159                 }
160             }
161             if ( up != 0 )  ret.a[i + j] = up;
162         }
163         ret.len = i + j;
164         while ( ret.a[ret.len - 1] == 0 && ret.len > 1 ) ret.len--;
165         return ret;
166     }
167     void print() {
168         printf ( "%d", a[len - 1] );
169         for ( int i = len - 2; i >= 0; i-- ) printf ( "%04d", a[i] );
170     }
171 };
172 int n;
173 struct node {
174     char name[101];
175     char num[2010];
176 } qu[MAXL];
177 int cmp ( node  s1, node s2 ) {
178     int len1 = strlen ( s1.num ), len2 = strlen ( s2.num );
179     if ( len2 > len1 ) return 1;
180     else if ( len2 < len1 ) return 0;
181     else {
182         for ( int i = 0 ; i < len1 ; i++ )
183             if ( s2.num[i] < s1.num[i] )  return 0;
184             else if ( s2.num[i] > s1.num[i] ) return 1;
185     }
186 }
187 char sum[2010];
188 Big s, a[MAXL];
189 vector<int>ans;
190 int main() {
191     scanf ( "%d%s", &n, sum );
192     s = sum;
193     for ( int i = 0 ; i < n ; i++ ) scanf ( "%s%s", qu[i].name, qu[i].num ) ;
194     sort ( qu, qu + n, cmp );
195     //for ( int i = 0 ; i < n ; i++ ) printf ( "%s %s\n", qu[i].name, qu[i].num );
196     for ( int i = 0 ; i < n ; i++ ) a[i] = qu[i].num;
197     int flag = 0;
198     for ( int i = n - 1 ; i >= 0 ; i-- ) {
199         // a[i].print(), printf ( " " ), s.print(), printf ( "\n" );
200         if ( a[i] > s ) continue;
201         else if ( s > a[i] ) {
202             s = s - a[i];
203             ans.push_back ( i );
204         } else {
205             ans.push_back ( i );
206             flag = 1;
207             break;
208         }
209     }
210     if ( !flag )  printf ( "0\n" );
211     else {
212         printf ( "%d\n", ans.size() );
213         for ( int i = 0 ; i < ans.size() ; i++ )
214             printf ( "%s\n", qu[ans[i]].name );
215     }
216     return 0;
217 }

J. Jumbled String

有a个00子顺序串,b个01,c个10,d个11,构造01串

注意一点当全部等于0的时候要输出0或者1

还有a==0 ,0的个数有0或者1个 ,d==0 同理

我的构造是111100001111 然后再0之中插入一个1构造01和10

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <set>
 7 #include <iostream>
 8 #include <map>
 9 #include <stack>
10 #include <string>
11 #include <vector>
12 #define  pi acos(-1.0)
13 #define  eps 1e-9
14 #define  fi first
15 #define  se second
16 #define  rtl   rt<<1
17 #define  rtr   rt<<1|1
18 #define  bug         printf("******\n")
19 #define  mem(a,b)    memset(a,b,sizeof(a))
20 #define  name2str(x) #x
21 #define  fuck(x)     cout<<#x" = "<<x<<endl
22 #define  f(a)        a*a
23 #define  sf(n)       scanf("%d", &n)
24 #define  sff(a,b)    scanf("%d %d", &a, &b)
25 #define  sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
26 #define  sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
27 #define  pf          printf
28 #define  FRE(i,a,b)  for(i = a; i <= b; i++)
29 #define  FREE(i,a,b) for(i = a; i >= b; i--)
30 #define  FRL(i,a,b)  for(i = a; i < b; i++)+
31 #define  FRLL(i,a,b) for(i = a; i > b; i--)
32 #define  FIN         freopen("data.txt","r",stdin)
33 #define  gcd(a,b)    __gcd(a,b)
34 #define  lowbit(x)   x&-x
35 #define rep(i,a,b) for(int i=a;i<b;++i)
36 #define per(i,a,b) for(int i=a-1;i>=b;--i)
37 using namespace std;
38 typedef long long  LL;
39 typedef unsigned long long ULL;
40 const int INF = 0x3f3f3f3f;
41 const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
42 const int maxn = 1e2 + 5;
43 map<LL, int>mp;
44 LL a, b, c, d;
45 int main() {
46     for ( int i = 2 ; i <= 100001 ; i++ ) mp[ ( i - 1 ) *i / 2] = i;
47     scanf ( "%lld%lld%lld%lld", &a, &b, &c, &d );
48     int num0 = mp[a], num1 = mp[d];
49     if ( a == 0 && b == 0 && c == 0 && d == 0 )  return 0 * printf ( "1\n" );
50     if ( ( num0 == 0 && a != 0 ) || ( num1 == 0 && d != 0 ) ) return 0 * printf ( "impossible\n" );
51     if ( num0 == 0 && ( b || c ) ) num0 = 1;
52     if ( num1 == 0 && ( b || c ) ) num1 = 1;
53     if ( num0 * num1 != b + c ) return 0 * printf ( "impossible\n" );
54     if ( num0 == 0 && b == 0 && c == 0 ) {
55         for ( int i = 0 ; i < num1 ; i++ ) printf ( "1" );
56         printf ( "\n" );
57         return 0;
58     }
59     if ( num1 == 0 && b == 0 && c == 0 ) {
60         for ( int i = 0 ; i < num0 ; i++ ) printf ( "0" );
61         printf ( "\n" );
62         return 0;
63     }
64     //printf ( "num0 = %d num1 = %d\n", num0, num1 );
65     int cnt = c / num0, res = c % num0;
66     for ( int i = 0 ; i < cnt ; i++ ) printf ( "1" );
67     for ( int i = 0 ; i < num0 - max ( res - ( res == 0 ), 0 ) ; i++ ) printf ( "0" );
68     if ( res != 0 )  printf ( "1" ), num1--;
69     for ( int i = 0; i < max ( res - ( res == 0 ), 0 ) ; i++ ) printf ( "0" );
70     for ( int i = cnt ; i < num1 ; i++ ) printf ( "1" );
71     printf ( "\n" );
72     return 0;
73 }

K. King‘s Colors

给你一颗树有n个节点,用K种颜色染色,必要用完K种颜色,问方案数

第一次遇到容斥的题目,表示很不会

全部的方案为k*(k-1)^(n-1)-(k-1)*(k-1-1)^ (n-1)+ (k-2)*(k-2-1)^ (n-1)+……

我第一次看到这个式子很懵逼 后面解释是k*(k-1)^(n-1)这个包含了用了k-1的颜色的情况所以要减去

我第一次感觉减去这个就够了,但是这个式子还+上了k-2的颜色的情况  (我第一次想的时候k*(k-1)^(n-1)也包含了k-2的颜色的情况 为什么这里还要加上  直接减去不就好了)

然后我直接手写了一个4个节点和4种颜色的情况弄懂了

所有情况4*3*3*3 – 3*2*2*C(4,3)+C(4,2)*2*1*1*1

看3*2*2*C(4,3)这一项 选择3种颜色的方案数,这个里面包含了选择2种颜色的情况

假设抽出这3种颜色这里的两种颜色为

1 2 3 为 (1,2)(1,3),(2,3)

1 3 4 为 (1,3)(1,4),(3,4)

1 2 4 为 (1,2)(1,4),(2,4)

2 3 4 为 (2,3)(2,4),(3,4)

然后你会发现用了两种颜色的刚好多被减了一次,所以要加上

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <queue>
 4 #include <cmath>
 5 #include <algorithm>
 6 #include <set>
 7 #include <iostream>
 8 #include <map>
 9 #include <stack>
10 #include <string>
11 #include <vector>
12 #define  pi acos(-1.0)
13 #define  eps 1e-9
14 #define  fi first
15 #define  se second
16 #define  rtl   rt<<1
17 #define  rtr   rt<<1|1
18 #define  bug         printf("******\n")
19 #define  mem(a,b)    memset(a,b,sizeof(a))
20 #define  name2str(x) #x
21 #define  fuck(x)     cout<<#x" = "<<x<<endl
22 #define  f(a)        a*a
23 #define  sf(n)       scanf("%d", &n)
24 #define  sff(a,b)    scanf("%d %d", &a, &b)
25 #define  sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
26 #define  sffff(a,b,c,d) scanf("%d %d %d %d", &a, &b, &c, &d)
27 #define  pf          printf
28 #define  FRE(i,a,b)  for(i = a; i <= b; i++)
29 #define  FREE(i,a,b) for(i = a; i >= b; i--)
30 #define  FRL(i,a,b)  for(i = a; i < b; i++)+
31 #define  FRLL(i,a,b) for(i = a; i > b; i--)
32 #define  FIN         freopen("data.txt","r",stdin)
33 #define  gcd(a,b)    __gcd(a,b)
34 #define  lowbit(x)   x&-x
35 #define rep(i,a,b) for(int i=a;i<b;++i)
36 #define per(i,a,b) for(int i=a-1;i>=b;--i)
37 using namespace std;
38 typedef long long  LL;
39 typedef unsigned long long ULL;
40 const int INF = 0x3f3f3f3f;
41 const LL INFLL = 0x3f3f3f3f3f3f3f3fLL;
42 const int maxn = 2505;
43 const int mod = 1e9 + 7;
44 int n, k, C[maxn][maxn];
45 LL exp_mod ( LL a, LL b ) {
46     LL ret = 1;
47     while ( b ) {
48         if ( b & 1 ) ret = ret * a % mod;
49         a = a * a % mod;
50         b = b >> 1;
51     }
52     return ret % mod;
53 }
54 int main() {
55     C[0][0] = 1, C[0][1] = 0;
56     for ( int i = 1 ; i <= 2500 ; i++ ) {
57         C[i][0] = 1, C[i][i + 1] = 0;
58         for ( int j = 1 ; j <= i ; j++ )
59             C[i][j] = ( C[i - 1][j - 1] + C[i - 1][j] ) % mod;
60     }
61     sff ( n, k );
62     for ( int i = 1, u ; i < n ; i++ ) sf ( u );
63     LL ans = 0;
64     for ( int i = k ; i >= 2 ; i-- )
65         if ( ( k - i ) % 2 == 0 ) ans = ( ans + 1LL * i * exp_mod ( i - 1, n - 1 ) % mod * C[k][i] % mod ) % mod;
66         else ans = ( ans - 1LL * i * exp_mod ( i - 1, n - 1 ) % mod * C[k][i] % mod + mod ) % mod;
67     printf ( "%lld\n", ans );
68 }

原文地址:https://www.cnblogs.com/qldabiaoge/p/10527219.html

时间: 2024-10-31 11:40:07

Gym 101933的相关文章

CF gym 101933 K King&#39;s Colors —— 二项式反演

题目:http://codeforces.com/gym/101933/problem/K 其实每个点的颜色只要和父亲不一样即可: 所以至多 i 种颜色就是 \( i * (i-1)^{n-1} \),设为 \( f(i) \),设恰好 i 种颜色为 \( g(i) \) 那么 \( f(i) = \sum\limits_{j=0}^{i} C_{i}^{j} * g(j) \) 二项式反演得到 \( g(i) = \sum\limits_{j=0}^{k} (-1)^{k-j} * C_{k}

Gym 101933 King&#39;s Colors

Link 设用\(k\)种颜色给\(n\)个点的树染色(必须全用)的方案数为\(f(k)\),用\(k\)种颜色给\(n\)个点的树染色(可以缺用)的方案数为\(g(k)\). 显然有\(g(k)=k(k-1)^{n-1}=\sum\limits_{i=2}^k{k\choose i}f(i)\),直接二项式反演即可. #include<cstdio> using i64=long long; const int N=2507,P=1000000007; int read(){int x;sc

Gym .101933 Nordic Collegiate Programming Contest (NCPC 2018) (寒假gym自训第四场)

(本套题算是比较温和吧,就是罚时有点高. B .Baby Bites 题意:给出一个婴儿给出的数组,有一些数字听不清楚,让你还原,问它是否是一个从1开始的一次增加的数组. 思路:从左往右依次固定,看是否有矛盾即可. #include<bits/stdc++.h> #define ll long long #define rep(i,a,b) for(int i=a;i<=b;i++) #define rep2(i,a,b) for(int i=a;i>=b;i--) using n

【2019.09.29】NCPC2018

补题地址:https://codeforces.com/gym/101933 A: B:签到题,注意他每咬一口数一次,所有第一个数应该是从1开始的. C: D: E: F: G: H: I: J: K: 原文地址:https://www.cnblogs.com/ncu2019/p/11610396.html

CodeForces Gym 100935D Enormous Carpet 快速幂取模

Enormous Carpet Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Gym 100935D Description standard input/outputStatements Ameer is an upcoming and pretty talented problem solver who loves to solve problems using computers.

B - Average Gym - 101161B 组合数学

http://codeforces.com/gym/101161/attachments 今天被卡常了,其实是自己对组合数技巧研究的不够. 如果是n, m <= 1e5的,然后取模是质数,那么可以用费马小定理. 如果n, m都比较小,那么其实是直接杨辉三角.不用逆元那些. 这题的思路是,枚举每一一个ave,然后总和就是n * ave 相当于方程  x1 + x2 + .... + xn = n * ave中,在0 <= x[i] <= full的情况下,不同解的个数中,使得x[i] ==

Codeforces Gym 100269 Dwarf Tower (最短路)

题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a new game named "Dwarf Tower". In this game there are n different items,which you can put on your dwarf character. Items are numbered from 1 to n. Vasya want

CodeForces Gym 101063C 二进制压缩

http://codeforces.com/gym/101063/problem/C 给n个人,m样物品,每个人可以从物品中选择几样.两人选择物品的交集元素个数比上并集元素个数如果大于某个比例即可将两人配对.求配对数. n的范围是1e5,直接比较所有人的选择会TLE,应该将所有选择物品的情况用二进制压缩,m最大是10,情况数目小于2048,可以接受.注意配对总数范围应为long long. #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> i

Gym 101246H ``North-East&#39;&#39;(LIS)

http://codeforces.com/gym/101246/problem/H 题意: 给出n个点的坐标,现在有一个乐队,他可以从任一点出发,但是只能往右上方走(包括右方和上方),要经过尽量多的点.输出它可能经过的点和一定会经过的点. 思路: 分析一下第一个案例,在坐标图上画出来,可以发现,他最多可以经过4个点,有两种方法可以走. 观察一下,就可以发现这道题目就是要我们求一个LIS. 首先,对输入数据排一下顺序,x小的排前,相等时则将y大的优先排前面. 用二分法求LIS,这样在d数组中就可