forked from marioyc/Online-Judge-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
24 changed files
with
1,866 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include <cstdio> | ||
|
||
using namespace std; | ||
|
||
#define MOD 1000000007 | ||
|
||
long long mod_pow(long long a, int b){ | ||
long long ret = 1; | ||
|
||
while(b){ | ||
if(b & 1) ret = ret * a % MOD; | ||
a = a * a % MOD; | ||
b >>= 1; | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
int main(){ | ||
int T,N,S; | ||
|
||
scanf("%d",&T); | ||
|
||
while(T--){ | ||
scanf("%d %d",&N,&S); | ||
|
||
if(N == S) printf("1\n"); | ||
else printf("%lld\n",S * mod_pow(N,N - S - 1) % MOD); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
#include <cstdio> | ||
#include <cstring> | ||
|
||
using namespace std; | ||
|
||
int s,a[50],b[50]; | ||
long long memo[50][51][51]; | ||
|
||
long long solve(int pos, int have, int marble){ | ||
if(have < 0) return 0; | ||
|
||
if(pos == -1){ | ||
if(have > 0 || marble != s) return 0; | ||
return 1; | ||
} | ||
|
||
long long &ret = memo[pos][have][marble]; | ||
|
||
if(ret == -1){ | ||
ret = solve(pos - 1,have,marble); | ||
|
||
if(marble == a[pos]) ret += solve(pos - 1,have - 1,b[pos]); | ||
else if(marble == b[pos]) ret += solve(pos - 1,have - 1,a[pos]); | ||
else ret += solve(pos - 1,have - 1,marble); | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
int main(){ | ||
int T,n,m,k; | ||
|
||
scanf("%d",&T); | ||
|
||
while(T--){ | ||
scanf("%d %d %d %d",&n,&m,&k,&s); | ||
|
||
for(int i = 0;i < m;++i) | ||
scanf("%d %d",&a[i],&b[i]); | ||
|
||
int ans = 1; | ||
long long aux = 0; | ||
memset(memo,-1,sizeof memo); | ||
|
||
for(int i = 1;i <= n;++i){ | ||
long long ret = solve(m - 1,k,i); | ||
|
||
if(ret > aux){ | ||
aux = ret; | ||
ans = i; | ||
} | ||
} | ||
|
||
printf("%d\n",ans); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
#include <cstdio> | ||
#include <cstring> | ||
#include <queue> | ||
|
||
using namespace std; | ||
|
||
int n,m; | ||
char M[500][501]; | ||
bool in[500][500]; | ||
int er[500][500],ec[500][500],ed[500][500]; | ||
|
||
void dfs(int r, int c){ | ||
if(er[r][c] != -1) return; | ||
if(M[r][c] == '0' || M[r][c] == 'W' || M[r][c] == '$' || M[r][c] == '#'){ | ||
er[r][c] = r; | ||
ec[r][c] = c; | ||
ed[r][c] = 0; | ||
}else{ | ||
int r2,c2; | ||
|
||
if(M[r][c] == 'L'){ | ||
r2 = r; | ||
c2 = c - 1; | ||
}else if(M[r][c] == 'R'){ | ||
r2 = r; | ||
c2 = c + 1; | ||
}else if(M[r][c] == 'D'){ | ||
r2 = r + 1; | ||
c2 = c; | ||
}else{ | ||
r2 = r - 1; | ||
c2 = c; | ||
} | ||
|
||
if(r2 == n || r2 == -1 || c2 == -1 || c2 == m || in[r2][c2]){ | ||
er[r][c] = -2; | ||
ec[r][c] = -2; | ||
}else{ | ||
in[r][c] = true; | ||
dfs(r2,c2); | ||
in[r][c] = false; | ||
er[r][c] = er[r2][c2]; | ||
ec[r][c] = ec[r2][c2]; | ||
ed[r][c] = ed[r2][c2] + 1; | ||
} | ||
} | ||
} | ||
|
||
struct node{ | ||
int r,c,mask,dist; | ||
|
||
node(){} | ||
|
||
node(int _r, int _c, int _mask, int _dist): | ||
r(_r), c(_c), mask(_mask), dist(_dist){} | ||
|
||
bool operator < (node X) const{ | ||
return dist > X.dist; | ||
} | ||
}; | ||
|
||
int x1,y1,x2,y2,id[500][500],id2[110 + 10],dist[110 + 10][1 << 10]; | ||
int dr[] = {-1,1,0,0}; | ||
int dc[] = {0,0,-1,1}; | ||
|
||
int dijkstra(){ | ||
memset(dist,-1,sizeof dist); | ||
priority_queue<node> Q; | ||
Q.push(node(x1,y1,0,0)); | ||
dist[ id[x1][y1] ][0] = 0; | ||
|
||
int best = -1,t = -1; | ||
|
||
while(!Q.empty()){ | ||
node aux = Q.top(); | ||
Q.pop(); | ||
|
||
if(dist[ id[aux.r][aux.c] ][aux.mask] != aux.dist) continue; | ||
if(aux.r == x2 && aux.c == y2){ | ||
int ntreasure = __builtin_popcount(aux.mask); | ||
|
||
if(ntreasure > best || (ntreasure == best && aux.dist < t)){ | ||
best = ntreasure; | ||
t = aux.dist; | ||
} | ||
} | ||
|
||
for(int k = 0;k < 4;++k){ | ||
int r = aux.r + dr[k],c = aux.c + dc[k]; | ||
|
||
if(r >= 0 && r < n && c >= 0 && c < m){ | ||
int d = ed[r][c]; | ||
int r2 = er[r][c],c2 = ec[r][c]; | ||
|
||
if(r2 != -2 && (M[r2][c2] == '0' || M[r2][c2] == '$')){ | ||
int to = id[r2][c2]; | ||
int mask = aux.mask; | ||
|
||
if(M[r2][c2] == '$') mask |= id2[to]; | ||
|
||
if(dist[to][mask] == -1 || aux.dist + 1 + d < dist[to][mask]){ | ||
Q.push(node(r2,c2,mask,aux.dist + 1 + d)); | ||
dist[to][mask] = aux.dist + 1 + d; | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
if(best != -1) t += 2 * best; | ||
return t; | ||
} | ||
|
||
int main(){ | ||
while(scanf("%d %d",&n,&m) == 2){ | ||
for(int i = 0;i < n;++i) | ||
scanf("%s",M[i]); | ||
|
||
memset(in,false,sizeof in); | ||
memset(er,-1,sizeof er); | ||
int cont = 0,cont2 = 0; | ||
memset(id,-1,sizeof id); | ||
memset(id2,0,sizeof id2); | ||
|
||
for(int i = 0;i < n;++i){ | ||
for(int j = 0;j < m;++j){ | ||
dfs(i,j); | ||
|
||
if(M[i][j] == '0' || M[i][j] == '$'){ | ||
if(M[i][j] == '$'){ | ||
id2[cont] = (1 << cont2); | ||
++cont2; | ||
} | ||
|
||
id[i][j] = cont; | ||
++cont; | ||
} | ||
} | ||
} | ||
|
||
scanf("%d %d %d %d",&x1,&y1,&x2,&y2); | ||
--x1; --y1; --x2; --y2; | ||
|
||
printf("%d\n",dijkstra()); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
#include <cstdio> | ||
#include <cstring> | ||
#include <vector> | ||
#include <queue> | ||
|
||
using namespace std; | ||
|
||
struct node{ | ||
int cur,mask,t; | ||
|
||
node(){} | ||
|
||
node(int _cur, int _mask, int _t): | ||
cur(_cur),mask(_mask),t(_t){} | ||
|
||
bool operator < (node X)const{ | ||
return t > X.t; | ||
} | ||
}; | ||
|
||
int main(){ | ||
int n,m,t,s,e,j[10],dist[10][1 << 10]; | ||
vector<int> L[10],W[10]; | ||
int val[1 << 10]; | ||
|
||
while(scanf("%d %d %d",&n,&m,&t) == 3){ | ||
scanf("%d %d",&s,&e); | ||
|
||
for(int i = 0;i < n;++i) | ||
scanf("%d",&j[i]); | ||
|
||
val[0] = 0; | ||
|
||
for(int i = 1;i < (1 << n);++i){ | ||
int x = __builtin_ctz(i); | ||
val[i] = j[x] + val[i ^ (1 << x)]; | ||
} | ||
|
||
for(int i = 0;i < n;++i){ | ||
L[i].clear(); | ||
W[i].clear(); | ||
} | ||
|
||
for(int i = 0,a,b,c;i < m;++i){ | ||
scanf("%d %d %d",&a,&b,&c); | ||
L[a].push_back(b); | ||
W[a].push_back(c); | ||
L[b].push_back(a); | ||
W[b].push_back(c); | ||
} | ||
|
||
int ans = 0; | ||
priority_queue<node> Q; | ||
memset(dist,-1,sizeof dist); | ||
|
||
Q.push(node(s,(1 << s),0)); | ||
dist[0][1] = 0; | ||
|
||
while(!Q.empty()){ | ||
node aux = Q.top(); | ||
Q.pop(); | ||
|
||
if(aux.cur == e) ans = max(ans,val[aux.mask]); | ||
|
||
for(int i = L[aux.cur].size() - 1,to,mask,t2;i >= 0;--i){ | ||
to = L[aux.cur][i]; | ||
mask = (aux.mask | (1 << to)); | ||
t2 = aux.t + W[aux.cur][i]; | ||
|
||
if(t2 <= t && (dist[to][mask] == -1 || t2 < dist[to][mask])){ | ||
Q.push(node(to,mask,t2)); | ||
dist[to][mask] = t2; | ||
} | ||
} | ||
} | ||
|
||
printf("%d\n",ans); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#include <cstdio> | ||
#include <cstring> | ||
#include <algorithm> | ||
|
||
using namespace std; | ||
|
||
int N,L,t[30],l[30]; | ||
int memo[331][331]; | ||
|
||
int solve(int curL, int totall){ | ||
if(totall < 0) return 0; | ||
if(totall >= curL) return 1; | ||
|
||
int &ret = memo[curL][totall]; | ||
|
||
if(ret == -1){ | ||
ret = totall == 0? 660 : (curL + totall - 1) / totall; | ||
|
||
for(int i = 0;i < N;++i) | ||
ret = min(ret,t[i] + solve(curL - totall * t[i],totall + l[i])); | ||
} | ||
|
||
return ret; | ||
} | ||
|
||
int main(){ | ||
while(scanf("%d %d",&N,&L) == 2){ | ||
for(int i = 0;i < N;++i) | ||
scanf("%d %d",&t[i],&l[i]); | ||
|
||
memset(memo,-1,sizeof memo); | ||
|
||
printf("%d\n",solve(L,0)); | ||
} | ||
|
||
return 0; | ||
} |
Oops, something went wrong.