BestCoder Round #25 A,B
挺好的一場比賽,完全被自己的智商給碾壓了啊。。。都是淚啊。。
A,判斷有向圖中是否有環,資料很小簡單粗暴的暴力演算法可解啊。暴力列舉有關係兩個點判斷反向是否可以找到,如果可以就說明有環。。。
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <map>
#include <set>
#define eps 1e-9
///#define M 1000100
///#define LL __int64
#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)
using namespace std;
const int maxn = 210;
int vis[maxn];
int mp[maxn][maxn];
vector<int> g[maxn];
int flag;
void dfs(int x, int y)
{
if(x == y)
{
flag = 1;
return;
}
if(vis[x]) return;
vis[x] = 1;
int n = g[x].size();
for(int i = 0; i < n; i++) dfs(g[x][i], y);
}
int main()
{
int n;
int m;
while(cin >>n>>m)
{
int x, y;
flag = 0;
memset(mp, 0, sizeof(mp));
for(int i = 0; i <= n; i++) g[i].clear();
for(int i = 0; i < m; i++)
{
scanf("%d %d",&x, &y);
mp[x][y] = 1;
g[x].push_back(y);
}
for(int i = 1; i <= n; i++)
{
for(int j = 1; j <= n; j++)
{
if(!mp[i][j]) continue;
memset(vis, 0, sizeof(vis));
dfs(j, i);
if(flag) break;
}
if(flag) break;
}
if(flag) cout<<"NO"<<endl;
else cout<<"YES"<<endl;
}
}
B,dp題目。我們一行一行的考慮。dp[i][j],表示前i行,都滿足了每一行至少有一個寶石的條件,而只有j列滿足了有寶石的條件的情況有多少種。列舉第i+1行放的寶石數k,這k個當中有t個是放在沒有寶石的列上的,那麼我們可以得到轉移方程:
dp[i+1][j+t]+=dp[i][j]*c[m-j][t]*c[j][k-t],其中c[x][y],意為在x個不同元素中無序地選出y個元素的所有組合的個數。
題解裡面解釋的很清楚了啊。。不再瞎說了啊。
#include <algorithm>
#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <iomanip>
#include <stdio.h>
#include <string>
#include <queue>
#include <cmath>
#include <stack>
#include <ctime>
#include <map>
#include <set>
#define eps 1e-9
///#define M 1000100
///#define LL __int64
#define LL long long
///#define INF 0x7ffffff
#define INF 0x3f3f3f3f
#define PI 3.1415926535898
#define zero(x) ((fabs(x)<eps)?0:x)
#define mod 1000000007
using namespace std;
const int maxn = 210;
LL dp[maxn][maxn];
LL cnk[maxn][maxn];
void init()
{
memset(cnk, 0, sizeof(cnk));
cnk[0][0] = 1LL;
for(int i = 1; i < 51; i++) cnk[i][0] = cnk[i][i] = 1LL;
for(int i = 2; i < 51; i++)
for(int j = 1; j < i; j++) cnk[i][j] = (cnk[i-1][j-1] + cnk[i-1][j])%mod;
}
int main()
{
int n, m;
init();
while(~scanf("%d %d",&n, &m))
{
memset(dp, 0, sizeof(dp));
dp[0][0] = 1LL;
for(int i = 0; i < n; i++)
{
for(int j = 0; j <= m; j++)
{
for(int k = 1; k <= m; k++)
{
for(int t = m; t >= 0; t--)
{
if(m-j < 0 || k-t < 0) continue;
dp[i+1][j+t] += (dp[i][j]*cnk[m-j][t]%mod)*cnk[j][k-t]%mod;
dp[i+1][j+t] %= mod;
}
}
}
}
cout<<dp[n][m]%mod<<endl;
}
}
相關文章
- BestCoder Round #3 A,B
- BestCoder Round #20 B,C
- hdu 5086 Revenge of Segment Tree(BestCoder Round #16)
- BestCoder Round #861001Price List(數學)
- BestCoder Round #2 1001 TIANKENG’s restaurant(區間內查詢)REST
- Restaurant Testing Round #12 BREST
- Codeforces Round #399 (A,B,C)
- Codeforces Round #448 (Div. 2)B
- Codeforces Round #450 (Div. 2) B
- CF 2B The least round way(DP)AST
- Codeforces Round 840題解(A、B、C)
- codeforces round 961題解(A、B、C)
- Codeforces Testing Round #10 B. Balancer
- Codeforces Round #251 (Div. 2) A/B/D
- Codeforces Round 949題解(A、B、C、D)
- codeforces round #234B(DIV2) B Inna and New Matrix of Candies
- 【Codeforces Round #499 (Div. 1) B】Rocket
- Codeforces Round #362 (Div. 2) B 模擬
- Codeforces Round #336 (Div. 2) B 暴力
- Codeforces Round #325 (Div. 2) B 遞推
- Codeforces Round #290 (Div. 2) A,B,C,D
- Codeforces Round #245 (Div. 2) B - Balls GameGAM
- Codeforces Round 934 2D/1B
- Codeforces Round #318 [RussianCodeCup Thanks-Round] (Div. 2) B 暴力
- Codeforces Round #491 (Div. 2) B. Getting an A
- Codeforces Round #361 (Div. 2) B BFS最短路
- Codeforces Round #323 (Div. 2) B 模擬
- Codeforces Round #321 (Div. 2) B 二分
- Codeforces Round #288 (Div. 2) A,B,C,D,E
- Codeforces Round #287 (Div. 2)A,B,C,D,E
- Codeforces Round #242 (Div. 2) B. Megacity
- Codeforces Round #249 (Div. 2) B. Pasha Maximizes
- Codeforces Round #247 (Div. 2) B - Shower Line
- Codeforces Round #253 (Div. 2) B - Kolya and Tandem Repeat
- Codeforces Round #252 (Div. 2) B. Valera and FruitsUI
- Codeforces Round #246 (Div. 2) B. Football Kit
- Codeforces Round #244 (Div. 2) B. Prison Transfer
- Codeforces Round #280 (Div. 2 A,B,C,D,E)