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