Search is not available for this dataset
name
stringlengths 2
112
| description
stringlengths 29
13k
| source
int64 1
7
| difficulty
int64 0
25
| solution
stringlengths 7
983k
| language
stringclasses 4
values |
---|---|---|---|---|---|
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<cstdio>
#include<iostream>
#include<cstring>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#define NO {puts("0");return 0;}
using namespace std;
typedef long long LL;
const int _=1e2;
const int maxn=1e5+_;
const int mod=1e9+7;
inline int ad(int x,int y){return x>=mod-y?x-mod+y:x+y;}
inline int re(int x,int y){return x<y?x-y+mod:x-y;}
inline int mu(int x,int y){return (LL)x*y%mod;}
inline int qp(int x,LL y){int r=1;while(y){if(y&1)r=mu(r,x);x=mu(x,x);y>>=1;}return r;}
inline int iv(int x){return qp(x,mod-2);}
inline int dv(int x,int y){return mu(x,qp(y,mod-2));}
inline int gcd(int x,int y){return x?gcd(y%x,x):y;}
int fac[maxn],fac_inv[maxn],Bin[maxn];
int C(int n,int m){return mu(fac[n],mu(fac_inv[m],fac_inv[n-m]));}
void yu()
{
fac[0]=Bin[0]=1;for(int i=1;i<maxn;i++)fac[i]=mu(fac[i-1],i),Bin[i]=ad(Bin[i-1],Bin[i-1]);
fac_inv[maxn-1]=iv(fac[maxn-1]);
for(int i=maxn-2;i>=0;i--)fac_inv[i]=mu(fac_inv[i+1],i+1);
}
int n,ans,to[maxn],du[maxn];
int num[maxn],v[maxn];
int main()
{
yu();
scanf("%d",&n); ans=1;
for(int i=1;i<=n;i++)
scanf("%d",&to[i]),du[to[i]]++;
for(int i=1;i<=n;i++)
if(du[i]==0)
{
v[i]++; int c=1,x=to[i];
while(du[x]==1)v[x]++,c++,x=to[x];
if(v[x])NO v[x]++;
num[x]=c;
}
for(int i=1;i<=n;i++)
if(du[i]==2&&v[i]==1)
{
v[i]++; int c=1,x=to[i];
while(x!=i)
{
v[x]++;
if(num[x])
{
if(c<num[x])NO
if(c>num[x])ans=ad(ans,ans);
c=0;
}
c++;x=to[x];
}
if(c<num[i])NO
if(c>num[i])ans=ad(ans,ans);
}
memset(num,0,sizeof(num));
for(int i=1;i<=n;i++)
if(du[i]==1&&!v[i])
{
v[i]=true; int x=to[i],c=1;
while(x!=i)v[x]=true,c++,x=to[x];
num[c]++;
}
for(int i=1;i<=n;i++)
if(num[i])
{
int sum=0,inv2=mod/2+1;
for(int j=0,t=1,inv=1;j+j<=num[i];j++,t=mu(t,i),inv=mu(inv,inv2))
sum=ad(sum, mu( ((i&1)&&i!=1)?Bin[num[i]-2*j]:1 , mu(t,mu(mu(C(num[i],2*j),inv),mu(fac[2*j],fac_inv[j]))) ) );
ans=mu(ans,sum);
}
printf("%d\n",ans);
return 0;
}
| CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include <fstream>
#include <vector>
using namespace std;
//ifstream cin("tema.in");
//ofstream cout("tema.out");
const int MAXN = 100000;
const int MOD = 1000000007;
int after[1 + MAXN], inDegree[1 + MAXN], tail[1 + MAXN], cycles[1 + MAXN], dp[1 + MAXN];
int cycle[1 + MAXN];
bool seen[1 + MAXN];
int Tail(int before, int now, int down, int length) {
int delta = (before < now ? now - before : length - before + now);
if (delta < down)
return 0;
if (delta == down)
return 1;
return 2;
}
int Ways(int l, int n) {
dp[0] = 1;
int single = 1;
if (l % 2 && l > 1)
single++;
dp[1] = single;
for (int i = 2; i <= n; i++)
dp[i] = (1LL * ((1LL * l * (i - 1)) % MOD) * dp[i - 2] + 1LL * single * dp[i - 1]) % MOD;
return dp[n];
}
int main() {
//freopen("tema.in", "r", stdin);
//freopen("tema.out", "w", stdout);
int n;
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
scanf("%d", &after[i]);
inDegree[after[i]]++;
}
for (int i = 1; i <= n; i++)
if (inDegree[i] > 2) {
printf("0\n");
return 0;
}
for (int i = 1; i <= n; i++)
if (!inDegree[i]) {
int j = i, length = 0;
while (inDegree[j] < 2) {
seen[j] = true;
length++;
j = after[j];
}
tail[j] = length;
}
int answer = 1;
for (int i = 1; i <= n; i++)
if (!seen[i]) {
int j = i, length = 0;
bool hasTail = false;
while (!seen[j]) {
length++;
cycle[length] = j;
if (tail[j])
hasTail = true;
seen[j] = true;
j = after[j];
}
if (i != j) {
printf("0\n");
return 0;
}
if (!hasTail) {
cycles[length]++;
continue;
}
j = 1;
while (!tail[cycle[j]])
j++;
int before = j;
for (int k = (j == length ? 1 : j + 1); k != j; k == length ? k = 1 : k++)
if (tail[cycle[k]]) {
answer = (1LL * answer * Tail(before, k, tail[cycle[k]], length)) % MOD;
before = k;
}
answer = (1LL * answer * Tail(before, j, tail[cycle[j]], length)) % MOD;
}
for (int i = 1; i <= n; i++)
answer = (1LL * answer * Ways(i, cycles[i])) % MOD;
printf("%d\n", answer);
return 0;
}
| CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
using namespace std;
const int MAXN = 100000 + 1000;
const int MOD = 1000000000 + 7;
const int INF = 0x1f1f1f1f;
int n, p[MAXN];
int ans = 1;
vector<int> redg[MAXN];
vector<int> vc, vl;
bool vis[MAXN], onc[MAXN];
int cnt[MAXN], f[MAXN];
int get_len(int x)
{
vis[x] = true;
int ret = 0;
for(int i = 0; i < redg[x].size(); i++)
if(!onc[redg[x][i]])
ret = ret == 0 ? get_len(redg[x][i]) + 1 : -INF;
return ret;
}
int find_circle(int x)
{
vis[x] = true;
int y = p[x], t = vis[y] ? y : find_circle(y);
if(t)
onc[x] = true, vc.push_back(x);
return t == x ? 0 : t;
}
void work()
{
for(int i = 1; i <= n; i++)
redg[p[i]].push_back(i);
for(int i = 1; i <= n; i++)
if(!vis[i])
{
vc.clear(), vl.clear();
find_circle(i);
reverse(vc.begin(), vc.end());
int last = -1;
for(int j = 0; j < vc.size(); j++)
{
int x = vc[j], t = get_len(x);
if(t < 0)
{
ans = 0; return ;
}
else if(t > 0)
last = j;
vl.push_back(t);
// cerr << vc[j] << ' ' << vl[j] << endl;
}
// cerr << endl;
if(last < 0)
{
cnt[vc.size()]++;
continue;
}
else
{
for(int j = 0; j < vc.size(); j++)
if(vl[j] > 0)
{
int l1 = vl[j], l2 = (j - last + vc.size() - 1) % vc.size() + 1;
// cerr << l1 << ' ' << l2 << endl;
if(l1 < l2)
ans = (ans * 2) % MOD;
else if(l1 == l2)
ans = (ans * 1) % MOD;
else
ans = 0;
last = j;
}
}
}
for(int i = 1; i <= n; i++)
if(cnt[i])
{
int lam = (i == 1 || i % 2 == 0 ? 1 : 2);
f[0] = 1;
for(int j = 1; j <= cnt[i]; j++)
{
f[j] = (long long)f[j - 1] * lam % MOD;
if(j - 2 >= 0)
f[j] = (f[j] + (long long)f[j - 2] * (j - 1) % MOD * i) % MOD;
}
ans = (long long)ans * f[cnt[i]] % MOD;
}
}
int main()
{
ios::sync_with_stdio(false);
// freopen("1.in", "r", stdin);
// freopen("1.out", "w", stdout);
cin >> n;
for(int i = 1; i <= n; i++)
cin >> p[i];
work();
cout << ans << endl;
return 0;
}
| CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef double db;
#define go(u) for(int i = head[u], v = e[i].to; i; i=e[i].lst, v=e[i].to)
#define rep(i, a, b) for(int i = a; i <= b; ++i)
#define pb push_back
#define re(x) memset(x, 0, sizeof x)
inline int gi() {
int x = 0,f = 1;
char ch = getchar();
while(!isdigit(ch)) { if(ch == '-') f = -1; ch = getchar();}
while(isdigit(ch)) { x = (x << 3) + (x << 1) + ch - 48; ch = getchar();}
return x * f;
}
template <typename T> inline bool Max(T &a, T b){return a < b ? a = b, 1 : 0;}
template <typename T> inline bool Min(T &a, T b){return a > b ? a = b, 1 : 0;}
const int N = 1e5 + 7, mod = 1e9 + 7;
int n, sz, tsum;
int vis[N], cir[N], fa[N], oncir[N], val[N], cnt[N], du[N];
LL ans = 1, f[N];
vector<int>G[N];
void tarjan(int u) {
++tsum;
vis[u] = 1;
for(auto v:G[u]) if(!vis[v]) {
fa[v] = u;
tarjan(v);
}else {
cir[sz++] = u;
for(int x = u; x ^ v; x = fa[x]) {
cir[sz++] = fa[x];
}
}
}
int dfs(int u) {
int cnt = 0;
for(auto v:G[u]) if(!oncir[v]) ++cnt;
if(cnt >= 2) puts("0"), exit(0);
for(auto v:G[u]) if(!oncir[v]) {
return dfs(v) + 1;
}
return 0;
}
int calc(int a, int b) {
if(a < b) return 2;
return a == b;
}
void solve() {
rep(i, 0, sz - 1) val[i] = dfs(cir[i]);
LL tot = 1; int L = -1, R = -1;
for(int i = 0; i < sz; ++i) if(val[i]) { L = i; break;}
for(int i = sz - 1; i >= 0; --i) if(val[i]) { R = i; break;}
tot = tot * calc(val[L], sz - 1 - R + L + 1) % mod;
for(int i = L + 1; i < sz; ++i) if(val[i]) {
tot = tot * calc(val[i], i - L) % mod;
L = i;
}
ans = ans * tot % mod;
}
int gg(int a) {
if(a == 1 || a % 2 == 0) return 1;
return 2;
}
vector<int>H[N];
int main() {
#ifdef fwat
freopen("data.in", "r", stdin);
#endif
n = gi();
rep(i, 1, n) oncir[i] = 1;
rep(i, 1, n) {
int a = gi();
G[a].pb(i);
H[i].pb(a);
++du[a];
}
queue<int>Q;
rep(i, 1, n) if(!du[i]) Q.push(i);
while(!Q.empty()) {
int u = Q.front();Q.pop();
oncir[u] = 0;
for(auto v:H[u]) if(--du[v] == 0) Q.push(v);
}
rep(i, 1, n) if(oncir[i] && !vis[i]) {
tsum = sz = 0;
tarjan(i);
if(sz == tsum)
++cnt[tsum];
else
solve();
}
rep(len, 1, n) {
fill(f, f + cnt[len] + 1, 0);
f[0] = 1, f[1] = gg(len);
rep(i, 2, cnt[len]) f[i] = (f[i - 1] * gg(len) + (i - 1ll) * len % mod * f[i - 2]) % mod;
ans = ans * f[cnt[len]] % mod;
}
printf("%lld\n", ans);
return 0;
} | CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<iostream>
#define N 110000
#define mmod 1000000007
#define LL long long
using namespace std;
struct node{LL y,nex;}a[2*N];
LL to[N],du[N],n,fir[N],len,cnt[N],A[N],num,siz,pre[N],l[N],m,pk[N],nt[N];
LL jc[N],ny[N],two[N],ans;
bool v[N],oc[N],b[N],bb[N],has[N];
void ins(LL x,LL y)
{
a[++len].y=y;a[len].nex=fir[x];fir[x]=len;
}
void dfs(LL x)
{
A[++num]=x;
v[x]=1;
for(LL k=fir[x];k;k=a[k].nex)
{
LL y=a[k].y;
if(v[y]) continue;
dfs(y);
}
}
void find_loop(LL x)
{
siz=0;
while(1)
{
b[x]=1;
if(b[to[x]])
{
LL st=x;
do{
siz++;
oc[x]=1;
x=to[x];
}while(x!=st);
break;
}
x=to[x];
}
}
void solve(LL x)
{
num=0;
dfs(x);
find_loop(x);
if(siz==num) {cnt[siz]++;return;}
for(LL i=1;i<=num;i++)
{
LL x=A[i];
if(oc[x]==0 && oc[to[x]]) {if(has[to[x]]) {ans=0;return;}has[to[x]]=1;}
}
for(LL i=1;i<=num;i++)
{
LL x=A[i],t=0;
if(du[x]) continue;
while(oc[x]==0)
{
if(bb[x])
{ans=0;return;}
bb[x]=1;
x=to[x];
t++;
}
l[x]=t;
x=to[x];t=1;
while(has[x]==0) {t++;x=to[x];}
pre[x]=t;
}
for(LL i=1;i<=num;i++)
if(l[A[i]]>pre[A[i]])
{ans=0;return;}
else if(l[A[i]]<pre[A[i]]) ans=ans*2%mmod;
int oo=1;
}
void make(LL k)
{
LL tmp=0;
m=cnt[k];
pk[0]=1;
for(LL i=1;i<=m;i++) pk[i]=pk[i-1]*k%mmod;
if(k%2 && k>1) tmp=two[m];
else tmp=1;
for(LL i=1;i<=m/2;i++)
{
LL t=jc[m]*ny[m-i*2]%mmod*nt[i]%mmod*ny[i]%mmod*pk[i]%mmod;
if(k%2 && k>1) t=t*two[m-i*2]%mmod;
tmp=(tmp+t)%mmod;
}
ans=ans*tmp%mmod;
}
int main()
{
scanf("%lld",&n);
for(LL i=1;i<=n;i++)
{
scanf("%lld",&to[i]);
ins(i,to[i]);ins(to[i],i);
du[to[i]]++;
}
ans=1;
for(LL i=1;i<=n;i++)
{
if(v[i]==0) solve(i);
if(ans==0) {printf("0\n");return 0;}
}
jc[0]=jc[1]=ny[0]=ny[1]=two[0]=1;two[1]=2;
for(LL i=2;i<=n;i++)
{
jc[i]=jc[i-1]*i%mmod;
ny[i]=(LL)(-mmod/i)*ny[mmod%i]%mmod;
two[i]=two[i-1]*2%mmod;
}
for(LL i=1;i<=n;i++) ny[i]=ny[i]*ny[i-1]%mmod;
nt[0]=1;nt[1]=ny[2];
for(int i=2;i<=n;i++) nt[i]=nt[i-1]*ny[2]%mmod;
for(LL i=1;i<=n;i++) if(cnt[i]) make(i);
ans=(ans+mmod)%mmod;
printf("%lld\n",ans);
return 0;
} | CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include <iostream>
#include <vector>
using namespace std;
const int MOD = 1000000007;
int solve(const vector<int>& A){
int N = A.size();
vector< vector<int> > back(N);
for(int i=0;i<N;i++){
back[A[i]].push_back(i);
if(back[A[i]].size() >= 3) return 0;
}
vector<int> check(N, 0);
long long mul = 1;
for(int i=0;i<N;i++){
if(check[i]) continue;
if(back[i].size() < 2) continue;
int cur = i;
while(true){
int p0 = back[cur][0];
int p1 = back[cur][1];
if(back[p0].size() > back[p1].size()) swap(p0, p1);
int s0 = back[p0].size();
int s1 = back[p1].size();
if(s0 == 0 && s1 == 0) return 0;
if(s0 + s1 >= 3) return 0;
if(s0 == 0 && s1 == 1) mul = (2*mul)%MOD;
int l0 = 0, l1 = 0;
while(back[p0].size() == 1 && !check[p0]){ check[p0] = 1; p0 = back[p0][0]; ++l0; }
while(back[p1].size() == 1 && !check[p1]){ check[p1] = 1; p1 = back[p1][0]; ++l1; }
if(back[p0].size() != 2 && back[p1].size() != 2) return 0;
if(back[p0].size() == 2 && back[p1].size() == 2) return 0;
if(back[p0].size() == 2){ swap(p0, p1); swap(l0, l1); }
if(s0 == 1 && s1 == 1){
if(l1 > l0) mul = (2*mul)%MOD;
else if(l1 < l0) return 0;
}
check[p0] = check[p1] = 1;
cur = p1;
if(cur == i) break;
}
}
vector<int> cycleNum(N+1, 0);
for(int i=0;i<N;i++){
if(check[i]) continue;
if(back[i].size() != 1) return 0;
int l = 1, cur = back[i][0];
while(cur != i){
check[cur] = 1;
if(back[cur].size() != 1) return 0;
cur = back[cur][0];
if(check[cur]) return 0;
++l;
}
check[i] = 1;
cycleNum[l]++;
}
vector<long long> inv(1000000);
vector<long long> fact(1000000);
vector<long long> factInv(1000000);
vector<long long> pow2(1000000);
inv[0] = inv[1] = 1;
fact[0] = fact[1] = 1;
pow2[0] = 1, pow2[1] = 2;
factInv[0] = factInv[1] = 1;
for(int i=2;i<inv.size();i++){
inv[i] = inv[MOD%i] * (MOD - MOD/i) % MOD;
fact[i] = fact[i-1] * i % MOD;
factInv[i] = factInv[i-1] * inv[i] % MOD;
pow2[i] = pow2[i-1] * 2 % MOD;
}
for(int i=1;i<=N;i++){
if(!cycleNum[i]) continue;
long long add = (i%2 == 1 && i >= 3 ? pow2[cycleNum[i]] : 1);
long long cur = 1;
for(int j=1;2*j<=cycleNum[i];j++){
cur = (cur * (2*j-1)) % MOD;
cur = (cur * (2*j)) % MOD;
cur = (cur * i) % MOD;
cur = (cur * inv[j]) % MOD;
cur = (cur * inv[2]) % MOD;
long long comb = fact[cycleNum[i]] * factInv[2*j] % MOD * factInv[cycleNum[i] - 2*j] % MOD;
long long curAdd = comb * cur % MOD;
if(i%2 == 1 && i >= 3){
curAdd = curAdd * pow2[cycleNum[i] - 2*j] % MOD;
}
add = (add + curAdd) % MOD;
}
mul = (mul * add) % MOD;
}
return mul;
}
int main(){
int N;
while(cin >> N){
vector<int> A(N);
for(int i=0;i<N;i++){
cin >> A[i];
--A[i];
}
cout << solve(A) << endl;
}
}
| CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<cstdio>
#include<algorithm>
#define fo(i,a,b) for(i=a;i<=b;i++)
using namespace std;
typedef long long ll;
const int maxn=100000+10,mo=1000000007;
int f[maxn],g[maxn],num[maxn],a[maxn],h[maxn],go[maxn],nxt[maxn],d[maxn];
int bz[maxn];
int i,j,k,l,r,s,t,n,m,tot,ans;
bool czy;
void add(int x,int y){
go[++tot]=y;
nxt[tot]=h[x];
h[x]=tot;
}
bool pd(int x){
if (d[x]>1) return 0;
bz[x]=1;
if (!d[x]) return 1;
int t=h[x];
bool gjx=pd(go[t]);
g[x]=g[go[t]]+1;
return gjx;
}
int main(){
scanf("%d",&n);
fo(i,1,n){
scanf("%d",&a[i]);
d[a[i]]++;
if (d[a[i]]>2){
printf("0\n");
return 0;
}
add(a[i],i);
}
ans=1;
fo(i,1,n)
if (!bz[i]){
j=i;
while (!bz[j]){
bz[j]=1;
j=a[j];
}
while (bz[j]==1){
bz[j]=2;
j=a[j];
}
k=j;
czy=1;
s=0;
while (1){
s++;
if (d[j]>1){
czy=0;
t=h[j];
while (t){
if (bz[go[t]]!=2){
l=go[t];
break;
}
t=nxt[t];
}
if (!pd(l)){
printf("0\n");
return 0;
}
g[j]=g[l]+1;
}
j=a[j];
if (j==k) break;
}
if (czy){
num[s]++;
continue;
}
while (d[j]==1) j=a[j];
k=j;s=0;
while (1){
j=a[j];
s++;
if (d[j]>1){
if (g[j]>s){
printf("0\n");
return 0;
}
else if (g[j]<s) ans=(ll)ans*2%mo;
s=0;
}
if (j==k) break;
}
}
fo(i,1,n){
if (i==1) k=1;
else if (i%2==1) k=2;
else k=1;
f[0]=1;
fo(j,1,num[i]){
f[j]=(ll)f[j-1]*k%mo;
if (j>=2) (f[j]+=(ll)f[j-2]*(j-1)%mo*i%mo)%=mo;
}
ans=(ll)ans*f[num[i]]%mo;
}
(ans+=mo)%=mo;
printf("%d\n",ans);
} | CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<cstdio>
#include<cstring>
#include<algorithm>
#define boynextdoor return puts("0"),0
using namespace std;
const int N=100005,mod=1000000007;
int n,x,ans=1,ring[N],to[N],f[N],e[N][2];
char ch[5000005],*p=ch;
bool vis[N];
inline int rd(){
register int res=0;
while(*p<'0'||*p>'9'){
p++;
}
while(*p>='0'&&*p<='9'){
res=res*10+*p-'0';
p++;
}
return res;
}
int main(){
fread(ch,5000000,1,stdin);
n=rd();
for(int i=1;i<=n;i++){
to[i]=rd();
if(!e[to[i]][0]){
e[to[i]][0]=i;
}else if(!e[to[i]][1]){
e[to[i]][1]=i;
}else{
boynextdoor;
}
}
for(int i=1;i<=n;i++){
if(!vis[i]){
int now=i,siz=0,last=0,prod=1;
bool flag=false;
while(!vis[now]){
vis[now]=true;
now=to[now];
}
int beg=now;
do{
siz++;
if(e[to[now]][0]!=now){
swap(e[to[now]][0],e[to[now]][1]);
}
now=to[now];
}while(now!=beg);
do{
now=to[now];
if(e[now][1]){
flag=true;
break;
}
}while(now!=beg);
if(!flag){
ring[siz]++;
continue;
}else{
beg=now;
}
do{
now=to[now];
last++;
if(e[now][1]){
flag=true;
int tmp=e[now][1],dep=0;
while(tmp){
dep++;
vis[tmp]=true;
if(e[tmp][1]){
boynextdoor;
}else{
tmp=e[tmp][0];
}
}
if(dep>last){
boynextdoor;
}else if(dep<last){
prod=prod*2%mod;
}
last=0;
}
}while(now!=beg);
ans=1LL*ans*prod%mod;
}
}
for(int i=1;i<=n;i++){
if(!ring[i]){
continue;
}
int flag=(i&1)&&i!=1;
f[0]=1,f[1]=flag+1;
for(int j=2;j<=ring[i];j++){
f[j]=(1LL*f[j-2]*(j-1)%mod*i+(f[j-1]<<flag))%mod;
}
ans=1LL*ans*f[ring[i]]%mod;
}
printf("%d\n",ans);
return 0;
} | CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include <bits/stdc++.h>
#define pb push_back
#define cmin(a,b) (a>b?a=b:a)
#define cmax(a,b) (a<b?a=b:a)
#define mem(a,k) memset(a,k,sizeof(a))
#define lop(i,s,t) for(int i=s;i<(t);++i)
#define rep(i,s,t) for(int i=s;i<=(t);++i)
#define dec(i,s,t) for(int i=s;i>=(t);--i)
#define fore(i,v) for(int i=g[v],d=es[i].d;i;i=es[i].nxt,d=es[i].d)
using namespace std;
#define Pr(f,...) //fprintf(stderr,f,##__VA_ARGS__),fflush(stderr)
typedef long long ll;
template<typename T>
void read(T &x){
x=0;
char c;
for(c=getchar();!isdigit(c);c=getchar());
for(;isdigit(c);c=getchar())x=x*10+c-'0';
}
const int N=1e5+50,P=1e9+7;
int mul(int a,int b){ return 1ll*a*b%P; }
int add(int a,int b){ a+=b; return a>=P?a-P:a; }
int sub(int a,int b){ a-=b; return a<0?a+P:a; }
vector<int> cir[N];
int fail,len[N],vis[N],f[N];
int n,to[N],deg[N],blg[N],m,cnt[N];
void dfs(int v){
vis[v]=2;
if(vis[to[v]]==2){
++m;
for(int x=to[v];;x=to[x]){
blg[x]=m;
cir[m].pb(x);
if(x==v)break;
}
}else if(!vis[to[v]]){
dfs(to[v]);
}
vis[v]=1;
}
int process(vector<int> &c){
vector<int> b;
lop(i,0,(int)c.size()){
if(len[c[i]])b.pb(i);
if(len[c[i]])Pr("len[%d]=%d\n",c[i],len[c[i]]);
}
int res=1,nb=b.size(),nc=c.size();
if(b.empty()){
++cnt[c.size()];
return res;
}
Pr("process : "); for(int x:c)Pr("%d ",x); Pr("\n");
lop(i,0,(int)b.size()){
int p=(i-1+nb)%nb,d=(b[i]-b[p]+nc)%nc,l=len[c[b[i]]];
if(d==0)d=nc;
Pr("i=%d,b[i]=%d,c[b[i]]=%d,d=%d,l=%d\n",i,b[i],c[b[i]],d,l);
res=mul(res,(d>=l)+(d>l));
}
return res;
}
int main(int argc,char *argv[]){
#ifdef CURIOUSCAT
freopen("dat.in","r",stdin);
freopen("my.out","w",stdout);
#endif
read(n);
rep(i,1,n){
read(to[i]);
++deg[to[i]];
Pr("to[%d]=%d\n",i,to[i]);
}
rep(i,1,n){
if(!vis[i]){
dfs(i);
}
}
rep(i,1,n){
if(blg[i]&°[i]>2)fail=true;
if(!blg[i]&°[i]>1)fail=true;
}
rep(i,1,n){
if(deg[i])continue;
int x,c=0;
for(x=i;!blg[x];x=to[x])++c;
len[x]=c;
}
int ans=1;
rep(k,1,m){
ans=mul(ans,process(cir[k]));
}
if(!ans)fail=true;
Pr("ans=%d\n",ans);
rep(l,1,n){
Pr("f[%d]=%d\n",l,f[l]);
int w=(l&1)+(l>1);
f[0]=1;
rep(i,1,cnt[l]){
f[i]=mul(f[i-1],w);
if(i>1)f[i]=add(f[i],mul(f[i-2],mul(i-1,l)));
}
ans=mul(ans,f[cnt[l]]);
}
Pr("fail=%d\n",(int)fail);
printf("%d\n", fail?0:ans);
return 0;
}
| CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include <bits/stdc++.h>
#define xx first
#define yy second
#define mp make_pair
#define pb push_back
#define mset(x, y) memset(x, y, sizeof x)
#define mcpy(x, y) memcpy(x, y, sizeof x)
using namespace std;
typedef long long LL;
typedef pair <int, int> pii;
inline int Read()
{
int x = 0, f = 1, c = getchar();
for (; !isdigit(c); c = getchar())
if (c == '-')
f = -1;
for (; isdigit(c); c = getchar())
x = x * 10 + c - '0';
return x * f;
}
const int MAXN = 100005;
const int mod = 1e9 + 7;
int n, ans = 1, a[MAXN], c[MAXN], d[MAXN], f[MAXN], cnt[MAXN];
bool v[MAXN];
inline int Dp(int l, int c)
{
int t = ((l & 1) && (l ^ 1)) + 1;
f[0] = 1, f[1] = t;
for (int i = 2; i <= c; i ++)
f[i] = (1LL * f[i - 1] * t + 1LL * f[i - 2] * (i - 1) % mod * l) % mod;
return f[c];
}
int main()
{
#ifdef wxh010910
freopen("data.in", "r", stdin);
#endif
n = Read();
for (int i = 1; i <= n; i ++)
a[i] = Read(), d[a[i]] ++;
for (int i = 1; i <= n; i ++)
if (d[a[i]] > 2)
return puts("0"), 0;
for (int i = 1; i <= n; i ++)
if (!d[i])
{
int x = i, len = 0;
while (d[x] < 2)
v[x] = 1, len ++, x = a[x];
f[x] = len;
}
for (int i = 1; i <= n; i ++)
if (!v[i])
{
int x = i, len = 0;
bool flg = false;
while (!v[x])
v[x] = 1, c[len ++] = x, flg |= f[x], x = a[x];
if (i ^ x)
return puts("0"), 0;
if (!flg)
cnt[len] ++;
else
{
int sta = 0, lst;
while (!f[c[sta]])
sta ++;
lst = sta;
for (int j = (sta + 1) % len; j ^ sta; j = (j + 1) % len)
if (f[c[j]])
{
int x = (j - lst + len) % len;
if (x < f[c[j]])
return puts("0"), 0;
if (x > f[c[j]])
ans = (ans << 1) % mod;
lst = j;
}
int x = (sta - lst + len - 1) % len + 1;
if (x < f[c[sta]])
return puts("0"), 0;
if (x > f[c[sta]])
ans = (ans << 1) % mod;
}
}
for (int i = 1; i <= n; i ++)
if (cnt[i])
ans = 1LL * ans * Dp(i, cnt[i]) % mod;
return printf("%d\n", ans), 0;
}
| CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<cstdio>
#include<cctype>
#define RI register int
#define g (p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++)
using namespace std;
typedef long long ll;
const int mod=1e9+7,N=1e5+10,size=N<<2;
char buf[size],*p1=buf,*p2=buf;
void qr(int &x) {
char c=g;x=0;
while(!isdigit(c))c=g;
while(isdigit(c))x=x*10+c-'0',c=g;
}
int n; ll ans;
int a[N],deg[N],vis[N],footL[N],sum[N],f[N];
bool cir[N];
int qm(int x) {return x>=mod?x-mod:x;}
void workcir(int x) {
int now=0,fr=0,frL=0,ed=0;
//now是当前环的长度,fr是第一个有脚点的位置,frL是位置对应的脚长,ed为上一个有脚点的位置。
while(cir[x]) {
++now; cir[x]=0;
if(footL[x]) {
if(!fr) ed=fr=now,frL=footL[x];
else {
ans=ans*((footL[x]<now-ed)+(footL[x]<=now-ed))%mod;//脚的放法
ed=now;
}
}
x=a[x];
}
if(!ed) ++sum[now];
else ans=ans*((frL<now-ed+fr)+(frL<=now-ed+fr))%mod;
}
void work() {
for(RI i=1;i<=n;i++) {//脚对环上交接点贡献
if(deg[i]) continue;
int x=i,len=0; while(!cir[x])x=a[x],++len;
footL[x]=len;//脚长
}
ans=f[0]=1;
for(RI i=1;i<=n;i++) if(cir[i]) workcir(i);//以环为基准计算基环内向树的情况
for(RI i=1;i<=n;i++) {//计算简单环
for(RI j=1;j<=sum[i];j++) {
if(i>1&&(i&1)) f[j]=qm(f[j-1]<<1);
else f[j]=f[j-1];
if(j>1) f[j]=qm(f[j]+(ll)f[j-2]*(j-1)%mod*i%mod);
}
ans=ans*f[sum[i]]%mod;
}
}
int main() {
qr(n);
for(RI i=1;i<=n;i++) qr(a[i]),++deg[a[i]];
for(RI i=1;i<=n;i++) if(!vis[i]) {
int x=i;while(!vis[x]) vis[x]=i,x=a[x];
if(vis[x]!=i) continue;//脚
while(!cir[x]) cir[x]=1,x=a[x];//标记环上点
}
for(RI i=1;i<=n;i++)
if((cir[i]&°[i]>2)||(!cir[i]&°[i]>1)) return puts("0"),0;//开叉就不行
work(); printf("%lld\n",ans); return 0;
} | CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include <bits/stdc++.h>
#define LL long long
#define INF 0x7FFFFFFF//or 0x3f3f3f3f ? -Wall
using namespace std;
template<class T> inline
void read(T& x) {
int f = 1; x = 0;
char ch = getchar();
while (ch < '0' || ch > '9') {if (ch == '-') f = -1; ch = getchar();}
while (ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
x *= f;
}
/*============ Header Template ============*/
const int N = 100000 + 5;
const int mo = 1000000007;
int n;
int a[N], in[N], c[N], l[N], deg[N], vis[N], ch[N], pos[N];
LL f[N];
queue<int> Q;
std::vector<int> zkx;
void Topo() {
for (int i = 1; i <= n; i++) if (!in[i]) Q.push(i);
while (!Q.empty()) {
int x = Q.front(); Q.pop();
ch[x] = 1;
if (--in[a[x]] == 0) Q.push(a[x]);
}
}
int main() {
read(n);
for (int i = 1; i <= n; i++) read(a[i]), in[a[i]]++, deg[a[i]]++;
for (int i = 1; i <= n; i++) if (in[a[i]] > 2) {printf("0\n"); return 0;}
Topo();
for (int i = 1; i <= n; i++) if (ch[i] && deg[i] > 1) {printf("0\n"); return 0;}
for (int i = 1; i <= n; i++)
if (ch[i] && !deg[i]) {
int x = i, cnt = 0;
while (ch[x]) {cnt++; x = a[x];}
l[x] = cnt;
}
//for (int i = 1; i <= n; i++) printf("%d ", l[i]); printf("\n");
LL ans = 1;
for (int i = 1; i <= n; i++) if (!ch[i] && !vis[i]) {
int x = i, cnt = 0; LL tmp = 1;
zkx.clear();
while (!vis[x]) {
vis[x] = 1; cnt++;
if (l[x]) zkx.push_back(x);
pos[x] = cnt; x = a[x];
}
if (!zkx.size()) {c[cnt]++; continue;}
//printf("%d\n", cnt);
zkx.push_back(0); pos[0] = pos[zkx[0]] + cnt; l[0] = l[zkx[0]];
for (int i = 1; i < zkx.size(); i++) {
int l1 = l[zkx[i]];
int l2 = pos[zkx[i]] - pos[zkx[i - 1]];
if (l1 == l2) continue;
(tmp *= (l1 < l2 ? 2LL : 0)) %= mo;
}
(ans *= tmp) %= mo;
}
//printf("%lld\n", ans);
//for (int i = 1; i <= n; i++) printf("%d ", c[i]); printf("\n");
for (int k = 1; k <= n; k++) {
if (!c[k]) continue;
LL w = (k > 1 && (k & 1) ? 2 : 1);
f[0] = 1; f[1] = w;
for (int i = 2; i <= c[k]; i++)
f[i] = (f[i - 1] * w % mo + f[i - 2] * (i - 1) % mo * k % mo) % mo;
(ans *= f[c[k]]) %= mo;
}
printf("%lld\n", (ans + mo) % mo);
return 0;
} | CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define MOD 1000000007
#define MAX 100100
#define ll long long
inline int read()
{
int x=0;bool t=false;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=true,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return t?-x:x;
}
int n,ans=1;
int a[MAX],dg[MAX];
bool cir[MAX];
int col[MAX],foot[MAX],cnt[MAX];
int f[MAX];
int main()
{
n=read();
for(int i=1;i<=n;++i)a[i]=read(),++dg[a[i]];
for(int i=1;i<=n;++i)
{
if(col[i])continue;
int x=i;
for(;!col[x];x=a[x])col[x]=i;
if(col[x]!=i)continue;//circle with foot
for(;!cir[x];x=a[x])cir[x]=true;//circle
}
for(int i=1;i<=n;++i)//illegal part
if((cir[i]&&dg[i]>2)||(!cir[i]&&dg[i]>1)){puts("0");return 0;}
for(int i=1;i<=n;++i)
{
if(dg[i])continue;//start of a foot do not have egde in
int x=i,len=0;
while(!cir[x])x=a[x],++len;
foot[x]=len;//record length
}
for(int i=1;i<=n;++i)//calculate answer of circle with feet
{
if(!cir[i])continue;//calculate circles
int x=i,pts=0,st=0,l1=0,fr=0;
//st:last root of foot,fr:first root of foot,l1:length of the first foot
//pts:number of the node in the circle
while(cir[x])
{
++pts;cir[x]=false;
if(foot[x])//a foot in the circle
{
if(!fr)st=fr=pts,l1=foot[x];
else
{
int ways=(foot[x]<(pts-st))+(foot[x]<=(pts-st));
ans=ans*ways%MOD;st=pts;
}
}
x=a[x];
}
if(fr)//circle of feet
{
int ways=(l1<(pts+fr-st))+(l1<=(pts+fr-st));
ans=ans*ways%MOD;
}
else//just a circle
cnt[pts]+=1;
}
for(int i=1;i<=n;++i)//dp,in order to calc each length of circles
{
if(!cnt[i])continue;
f[0]=1;
for(int j=1;j<=cnt[i];++j)
{
if(i>1&&(i&1))//odd circle
f[j]=(f[j-1]<<1)%MOD;//two ways
else//even circle
f[j]=f[j-1];//only one way
if(j>1)f[j]=(f[j]+1ll*f[j-2]*(j-1)%MOD*i%MOD)%MOD;
//merge two circle in the same length
}
ans=1ll*ans*f[cnt[i]]%MOD;
}
printf("%d\n",ans);
return 0;
}
| CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | //============================================================================
// Author : Sun YaoFeng
//============================================================================
//#pragma comment(linker, "/STACK:100240000,100240000")
//#include <cstdio>
//#include <cstdlib>
//#include <cstring>
//#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
#define DB double
#define lf else if
#define I64 long long
#define Rd() (rand()<<15|rand())
#define For(i,a,b) for(int i=a,lim=b;i<=lim;i++)
#define Rep(i,a,b) for(int i=a,lim=b;i>=lim;i--)
#define fi first
#define se second
#define MK make_pair
#define pb push_back
#define PA pair<int, int>
//#define min(a,b) ((a)<(b)?(a):(b))
//#define max(a,b) ((a)<(b)?(b):(a))
#define CH (ch=getchar())
int IN() {
int x= 0, f= 0, ch;
for (; CH < '0' || ch > '9';) f= (ch == '-');
for (; ch >= '0' && ch <= '9'; CH) x= x*10 + ch -'0';
return f? -x : x;
}
#define n 200005
#define P 1000000007
int N, L[n], R[n];
int A[n], B[n], S[n], V[n], Q[n], G[n], Deg[n], Now[n], Siz[n];
int Pow(int a,int b){
int ret=1;
for (;b;b>>=1,a=1ll*a*a%P)
if (b&1) ret=1ll*ret*a%P;
return ret;
}
int C(int a, int b) {
return 1ll*L[a]*R[b]%P*R[a-b]%P;
}
int main(int argc, char* argv[]){
N= IN();
L[0]= 1;
For(i, 1, N) L[i]= 1ll*L[i-1]*i%P;
R[N]= Pow(L[N], P-2);
Rep(i, N, 1) R[i-1]= 1ll*R[i]*i%P;
For(i, 1, N) A[i]= IN(), Deg[A[i]]++, Siz[i]= 1;
For(i, 1, N) {
Now[i]= Deg[i];
if (! Now[i]) Q[++ *Q]= i;
}
for (int i= 1; i <= *Q; i++) {
int u= Q[i], f= A[u];
Siz[f]+= Siz[u];
Now[f]--;
if (! Now[f]) Q[++ *Q]= f;
}
For(i, 1, *Q) V[Q[i]]= 1;
For(i, 1, N) {
if (V[i] && Deg[i] > 1) return puts("0"), 0;
if (! V[i] && Deg[i] > 2) return puts("0"), 0;
}
int Ans= 1;
For(i, 1, N) if (! V[i]) {
int j= i, len= 0;
for (; ;) {
V[j]= 1;
B[++ len]= Siz[j] - 1;
j= A[j];
if (j == i) break;
}
For(i, 1, len) B[len + i]= B[i];
For(i, 1, 2*len) S[i]= S[i-1] + B[i];
if (! S[len]) {
G[len]++; continue;
}
if (S[len] > len) return puts("0"), 0;
int ret= 1;
For(i, 1, len) {
int x= B[i], t= 0;
if (! x) continue;
if (S[len+i-1] - S[len+i-x] == 0) t++;
if (S[len+i-1] - S[len+i-x-1] == 0) t++;
ret= 1ll*ret*t%P;
}
Ans= 1ll*Ans*ret%P;
}
// printf("%d\n", Ans);
For(i, 1, N) if (G[i]) {
int x= G[i], t= 1, v= (i > 1 && (i&1)) ? 2 : 1, ret= Pow(v, x);
For(j, 1, x / 2) {
t= 1ll*t*C(x - (j-1)*2, 2)%P;
ret= (ret + 1ll*t*R[j]%P*Pow(i, j)%P*Pow(v, x - 2*j))%P;
}
Ans= 1ll*Ans*ret%P;
}
printf("%d\n", Ans);
return 0;
} | CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<cstdlib>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define gt getchar()
#define ll long long
#define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)
typedef std::pair<int,int> P;
#define mk std::make_pair
#define fr first
#define sc second
inline int in()
{
int k=0;char ch=gt;bool p=1;
while(ch<'-')ch=gt;if(ch=='-')ch=gt,p=0;
while(ch>'-')k=k*10+ch-'0',ch=gt;
return p?k:-k;
}
const int N=2e6+5,YL=1e9+7;
inline int MO(const int &x){return x>=YL?x-YL:x;}
inline void add(int &x,int y){if((x+=y)>=YL)x-=YL;}
int dep[N],a[N],bj[N],Q[N],len,top,b[N],tot,du[N],o[N],st[N],f[N],fa[N];
void check_circle()
{
for(int i=1;i<=len;++i)if(du[Q[i]]^1)return;
for(int i=1;i<=len;++i)bj[Q[i]]=3;b[++tot]=len;
}
void dfs(int u)
{
bj[u]=1,o[u]=1;
if(!bj[a[u]])return fa[a[u]]=u,dfs(a[u]),o[u]=0,void();
if(o[a[u]])
{
len=0;
for(int now=u;;now=fa[now])
{Q[++len]=now,bj[now]=2;if(now==a[u])break;}
check_circle();
}
o[u]=0;
}
void Dfs(int u){dep[a[u]]=dep[u]+1;if(bj[a[u]]>1)return;Dfs(a[u]);}
int solve(int x)
{
int ans=1;Q[len=1]=x;
for(int now=a[x];now^x;now=a[now])Q[++len]=now;
std::reverse(Q+1,Q+len+1);
for(int i=1;i<=len;++i)bj[Q[i]]=4,Q[len+i]=Q[i];
for(int i=1,j;i<=len;++i)
if(dep[Q[i]])
{
for(j=i+1;j<=(len<<1)&&!dep[Q[j]];++j);
if(dep[Q[i]]>j-i)puts("0"),exit(0);
if(dep[Q[i]]<j-i)add(ans,ans);i=j-1;
}
return ans;
}
int main()
{
int n=in();for(int i=1;i<=n;++i)++du[a[i]=in()];
for(int i=1;i<=n;++i)if(!du[i])dfs(i);
for(int i=1;i<=n;++i)if(!bj[i])dfs(i);
for(int i=1;i<=n;++i)
if((bj[i]>1&&du[i]>2)||(bj[i]==1&&du[i]>1))return puts("0"),0;
std::sort(b+1,b+tot+1);int ans=1;
for(int i=1,j;i<=n;i=j)
{
for(j=i;j<=n&&b[i]==b[j];++j);f[i-1]=1;
for(int k=i;k<j;++k)
{
f[k]=f[k-1];
if(b[i]>1&&(b[i]&1))add(f[k],f[k-1]);
if(k>i)f[k]=(1ll*f[k-2]*b[i]%YL*(k-i)+f[k])%YL;
}
ans=1ll*ans*f[j-1]%YL;
}
for(int i=1;i<=n;++i)if(bj[i]==1)bj[i]=0;
for(int i=1;i<=n;++i)if(!du[i])Dfs(i);
for(int i=1;i<=n;++i)if(bj[i]==2)ans=1ll*ans*solve(i)%YL;
return printf("%d\n",ans),0;
}
| CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<bits/stdc++.h>
#define LL long long
#define db double
using namespace std;
const int N=2e5+10,mod=1e9+7,inf=1<<30;
int rd()
{
int x=0,w=1;char ch=0;
while(ch<'0'||ch>'9'){if(ch=='-') w=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+(ch^48);ch=getchar();}
return x*w;
}
void ad(int &x,int y){x+=y,x-=x>=mod?mod:0;}
int fpow(int a,int b){int an=1;while(b){if(b&1) an=1ll*an*a%mod;a=1ll*a*a%mod,b>>=1;}return an;}
int ginv(int a){return fpow(a,mod-2);}
vector<int> e[N];
int n,a[N],v1[N],ti,cr[N],st[N],tp,f[N],g[N],b[N],h[N],fac[N],iac[N];
int C(int a,int b){return b<0||a<b?0:1ll*fac[a]*iac[b]%mod*iac[a-b]%mod;}
bool mk[N];
void dfs(int x)
{
int nn=e[x].size();
for(int i=0;i<nn;++i)
{
int y=e[x][i];
dfs(y),f[x]=f[x]?-inf:f[y]+1;
}
}
int main()
{
fac[0]=1;
for(int i=1;i<=N-5;++i) fac[i]=1ll*fac[i-1]*i%mod;
iac[N-5]=ginv(fac[N-5]);
for(int i=N-5;i;--i) iac[i-1]=1ll*iac[i]*i%mod;
n=rd();
for(int i=1;i<=n;++i) a[i]=rd();
for(int i=1;i<=n;++i)
if(!v1[i])
{
++ti;
int x=i;
tp=0;
while(!v1[x]) v1[x]=ti,st[++tp]=x,x=a[x];
if(cr[x]) {mk[x]=1;continue;}
if(v1[x]<ti) continue;
int cn=0;
for(int j=tp;;--j)
{
++cn;
if(st[j]==x) break;
}
for(int j=tp;;--j)
{
cr[st[j]]=cn;
if(st[j]==x) break;
}
mk[x]=tp>cn;
}
++ti;
for(int i=1;i<=n;++i)
if(cr[i]&&v1[i]<ti)
{
int x=i;
for(int j=1;j<=cr[i]*2;++j) v1[x]=ti,mk[a[x]]|=mk[x],x=a[x];
}
for(int i=1;i<=n;++i)
if(!cr[i]) e[a[i]].push_back(i);
int ans=1;
for(int i=1;i<=n;++i)
if(cr[i])
{
if(mk[i])
{
int x=i;
tp=0;
for(int j=1;j<=cr[i];++j) st[++tp]=x,x=a[x];
for(int j=1;j<=cr[i];++j) dfs(st[j]);
for(int j=1;j<=cr[i];++j)
if(f[st[j]]<0||f[st[j]]>cr[i]){puts("0");return 0;}
for(int j=1;j<=cr[i];++j) g[j]=g[j-1]+(bool)f[st[j]];
for(int j=1;j<=cr[i];++j) g[j+cr[i]]=g[j-1+cr[i]]+(bool)f[st[j]];
for(int j=cr[i]+1;j<=cr[i]+cr[i];++j)
{
int y=st[j-cr[i]];
if(!f[y]) continue;
if(cr[i]>1&&g[j-1]-g[j-f[y]]){puts("0");return 0;}
ans=1ll*ans*(2-(bool)(g[j-1]-g[j-f[y]-1]))%mod;
}
}
else ++b[cr[i]];
int x=i,lm=cr[i];
for(int j=1;j<=lm;++j) cr[x]=0,x=a[x];
}
h[0]=h[1]=1;
for(int i=2;i<=n;++i) h[i]=1ll*h[i-1]*(2*i-1)%mod;
for(int i=1;i<=n;++i)
{
if(!b[i]) continue;
int sm=0,dx=1+(i>1&&(i&1));
for(int j=0;j+j<=b[i];++j)
ad(sm,1ll*C(b[i],j+j)*h[j]%mod*fpow(i,j)%mod*fpow(dx,b[i]-j-j)%mod);
ans=1ll*ans*sm%mod;
}
printf("%d\n",ans);
return 0;
}
| CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<cstdio>
#include<cstdlib>
#include<cassert>
typedef long long LL;
const int md=1e9+7,N=1e5+5;
int tot[N],n,a[N],deg[N],cnt,bel[N],L[N],ans=1,top[N];
bool vis[N],inc[N];
inline void fail(){puts("0");exit(0);}
inline void upd(int&a){a+=a>>31&md;}
int solve(int L){
static int f[N];
*f=1;
for(int i=1;i<=tot[L];++i){
if((L&1)&&(L>1))upd(f[i]=f[i-1]*2-md);else f[i]=f[i-1];
if(i>1)f[i]=(f[i]+(i-1LL)*f[i-2]%md*L)%md;
}
return f[tot[L]];
}
int solve_tree(int id){
int len=0,now=top[id],r=1;
do{
++len;
now=a[now];
if(L[now]){
if(L[now]>len)return 0;
if(L[now]<len)upd(r=r*2-md);
len=0;
}
}while(now!=top[id]);
return r;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;++i)scanf("%d",a+i),++deg[a[i]];
for(int i=1;i<=n;++i)
if(deg[i]==2&&!vis[i]){
int j;
top[bel[i]=++cnt]=i;
vis[i]=inc[i]=1;
for(j=a[i];!vis[j];j=a[j])vis[j]=1,bel[j]=cnt,inc[j]=1;
if(j!=i)fail();
}else if(deg[i]>2)fail();
for(int i=1;i<=n;++i)
if(!vis[i]&°[i]==0){
int x=i;
while(!vis[x])x=a[x];
if(!inc[x]||L[x])fail();
int len=0;
for(x=i;!inc[x];x=a[x])vis[x]=1,++len;
L[x]=len;
}
for(int i=1;i<=n;++i)if(!vis[i]){
assert(deg[i]==1),vis[i]=1;
int x,len=1;
for(x=a[i];!vis[x];x=a[x])vis[x]=1,++len;
assert(x==i);
++tot[len];
}
for(int i=1;i<=n;++i)if(tot[i])ans=(LL)ans*solve(i)%md;
for(int i=1;i<=cnt;++i)
ans=(LL)ans*solve_tree(i)%md;
printf("%d\n",ans);
return 0;
} | CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<bits/stdc++.h>
#define pb push_back
#define mp make_pair
#define FL "a"
using namespace std;
typedef vector<int> VI;
typedef long long ll;
typedef double dd;
const int N=1e5+10;
const int mod=1e9+7;
inline ll read(){
ll data=0,w=1;char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch<='9'&&ch>='0')data=data*10+ch-48,ch=getchar();
return data*w;
}
inline void file(){
freopen(FL".in","r",stdin);
freopen(FL".out","w",stdout);
}
inline void upd(int &a,int b){a+=b;if(a>=mod)a-=mod;}
inline void dec(int &a,int b){a-=b;if(a<0)a+=mod;}
inline int poww(int a,int b){
int res=1;
for(;b;b>>=1,a=1ll*a*a%mod)
if(b&1)res=1ll*res*a%mod;
return res;
}
inline void er(){puts("0");exit(0);}
int n,a[N],ans;
int d[N],head[N],nxt[N<<1],to[N<<1],cnt;
inline void add(int u,int v){to[++cnt]=v;nxt[cnt]=head[u];head[u]=cnt;}
int t[N],f[N];
int vis[N],line[N],dis[N],cal[N],top,cir[N],len;
void dfs(int u){
vis[u]=1;cal[++top]=u;
for(int i=head[u],v;i;i=nxt[i])
if(~i&1){
if(!vis[v=to[i]])dfs(v);
else
for(int j=top;cal[j]!=v;j--)
cir[++len]=cal[j];
}
top--;
}
inline void solve(int x){
top=len=0;
for(int p=0;!vis[x];vis[x]=1,cal[++top]=x,p=x,x=a[x]);
for(int i=top,k=1;i;i--){
k?cir[++len]=cal[i],dis[cal[i]]=len:vis[cal[i]]=0;
if(cal[i]==x)k=0;
}
reverse(cir+1,cir+len+1);
for(int i=1;i<=len;i++)dis[cir[i]]=len-dis[cir[i]]+1;
for(int k=1,u;k<=len;k++){
u=cir[k];if(d[u]>2)er();line[u]=0;
for(int i=head[u],v;i;i=nxt[i])
if(!vis[v=to[i]])
for(line[u]++,vis[v]=1;d[v];v=to[head[v]],vis[v]=1,line[u]++)
if(d[v]>1)er();
}
for(int k=1;!line[cir[k]];k++)
if(k==len){t[len]++;return;}
if(len==1&&line[cir[1]]<=1)return;
top=0;
for(int k=1;k<=len;k++)
if(line[cir[k]])cal[++top]=cir[k];
if(line[cal[1]]>dis[cal[1]]+(len-dis[cal[top]]))er();
else ans=1ll*min(2,dis[cal[1]]+(len-dis[cal[top]])-line[cal[1]]+1)*ans%mod;
for(int i=2;i<=top;i++)
if(line[cal[i]]>dis[cal[i]]-dis[cal[i-1]])er();
else ans=1ll*min(2,dis[cal[i]]-dis[cal[i-1]]-line[cal[i]]+1)*ans%mod;
}
int main()
{
n=read();cnt=1;ans=1;
for(int i=1;i<=n;i++){
a[i]=read();d[a[i]]++;add(a[i],i);
if(a[i]>n)er();
}
for(int i=1;i<=n;i++)
if(!vis[i])solve(i);
for(int i=1;i<=n;i++)
if(t[i]){
f[0]=1;
for(int j=1;j<=t[i];j++){
f[j]=1ll*((i&1)&&i!=1?2:1)*f[j-1]%mod;
upd(f[j],1ll*(j-1)*i%mod*f[j-2]%mod);
}
ans=1ll*ans*f[t[i]]%mod;
}
printf("%d\n",ans);
return 0;
}
| CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 100000
#define MO 1000000007
using namespace std;
int a[MAXN+5],pre[MAXN+5],n;
int d[MAXN+5],cnt[MAXN+5],f[MAXN+5];
bool vis[MAXN+5];
int main()
{
// freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
int ans=1;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]),d[a[i]]++;
for(int i=1;i<=n;i++)
{
if(d[i]>=3)
{
printf("0\n");
return 0;
}
if(d[i]<=1||vis[i]==true)
continue;
int p=i;
do
{
if(vis[p]==true)
{
printf("0\n");
return 0;
}
vis[p]=true;
pre[a[p]]=p;
p=a[p];
}while(p!=i);
}
for(int i=1;i<=n;i++)
{
if(d[i]==0)
{
int p=i,len1=0,len2=0;
while(vis[p]==false)
vis[p]=true,len1++,p=a[p];
do
{
p=pre[p];
len2++;
}while(d[p]!=2);
if(len1<len2)
ans=2LL*ans%MO;
else if(len1>len2)
{
printf("0\n");
return 0;
}
}
}
for(int i=1;i<=n;i++)
{
if(vis[i]==false)
{
int p=i,len=0;
do
{
p=a[p];
len++;
vis[p]=true;
}while(p!=i);
cnt[len]++;
}
}
for(int i=1;i<=n;i++)
{
int mul=1;
if(i!=1&&(i%2==1))
mul++;
f[0]=1,f[1]=mul;
for(int j=2;j<=cnt[i];j++)
f[j]=(1LL*f[j-2]*(1LL*j-1LL)%MO*i%MO+1LL*f[j-1]*mul%MO)%MO;
ans=(1LL*ans*f[cnt[i]])%MO;
}
printf("%d\n",ans);
return 0;
} | CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | // package agc.agc008;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.*;
public class Main {
static final long MOD = 1000000007;
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int n = in.nextInt();
int[] a = new int[n];
int[] indeg = new int[n];
for (int i = 0; i < n ; i++) {
a[i] = in.nextInt()-1;
indeg[a[i]]++;
}
// doit(a);
// 10
// 2 3 4 5 1 1 6 7 8 9
for (int i = 0; i < n ; i++) {
if (indeg[i] >= 3) {
out.println(0);
out.flush();
return;
}
}
UnionFind f = new UnionFind(n);
for (int i = 0; i < n ; i++) {
f.unite(i, a[i]);
}
int[] wcnt = new int[n+1];
long ans = 1;
List<Integer>[] list = new List[n];
for (int i = 0; i < n ; i++) {
int id = f.find(i);
if (list[id] == null) {
list[id] = new ArrayList<>();
}
list[id].add(i);
}
boolean[] visitedG = new boolean[n];
boolean[] inloop = new boolean[n];
for (int i = 0; i < n ; i++) {
int id = f.find(i);
if (visitedG[id]) {
continue;
}
visitedG[id] = true;
int gcount = f.groupCount(id);
int cnt = 0;
int ptr = i;
for (int j = 0; j < gcount ; j++) {
ptr = a[ptr];
}
while (!inloop[ptr]) {
inloop[ptr] = true;
cnt++;
ptr = a[ptr];
}
if (cnt == f.groupCount(id)) {
wcnt[cnt]++;
} else {
ans *= solveLeggedRing(a, list[id], inloop, indeg, cnt);
ans %= MOD;
}
}
for (int i = 1 ; i <= n; i++) {
if (wcnt[i] >= 1) {
ans *= solveRing(i, wcnt[i]);
ans %= MOD;
}
}
out.println(ans);
out.flush();
}
private static long solveRing(int size, int count) {
long[] dp = new long[count+1];
long ptn = (size >= 3 && size % 2 == 1) ? 2 : 1;
dp[0] = 1;
dp[1] = ptn;
for (int i = 2 ; i <= count ; i++) {
dp[i] = (dp[i-1] * ptn) + (dp[i-2] * size % MOD * (i-1) % MOD);
dp[i] %= MOD;
}
return dp[count];
}
private static long solveLeggedRing(int[] a, List<Integer> group, boolean[] inloop, int[] indeg, int loopSize) {
List<Integer> lp = new ArrayList<>();
Map<Integer,Integer> lpmap = new HashMap<>();
for (int v : group) {
if (!inloop[v] && indeg[v] >= 2) {
return 0;
}
}
for (int v : group) {
if (inloop[v]) {
int ptr = v;
for (int i = 0; i < loopSize; i++) {
lp.add(ptr);
ptr = a[ptr];
}
Collections.reverse(lp);
for (int i = 0 ; i < lp.size() ; i++) {
lpmap.put(lp.get(i), i);
}
break;
}
}
List<int[]> legs = new ArrayList<>();
boolean[] isRootOfLeg = new boolean[lp.size()];
for (int v : group) {
if (indeg[v] == 0) {
int lcnt = 0;
int ptr = v;
while (!inloop[ptr]) {
lcnt++;
ptr = a[ptr];
}
int lidx = lpmap.get(ptr);
legs.add(new int[]{lidx, lcnt});
isRootOfLeg[lidx] = true;
}
}
legs.sort(Comparator.comparingInt(u -> u[0]));
if (legs.size() == 0) {
throw new RuntimeException("arien");
}
long ptn = 1;
for (int[] leg : legs) {
int now = leg[0];
int last = 2;
for (int i = 0 ; i < leg[1] ; i++) {
now = (now + 1) % lp.size();
if (isRootOfLeg[now]) {
if (i == leg[1]-1) {
last = 1;
} else {
return 0;
}
}
}
ptn *= last;
ptn %= MOD;
}
return ptn;
}
static class UnionFind {
int[] rank;
int[] parent;
int[] cnt;
public UnionFind(int n) {
rank = new int[n];
parent = new int[n];
cnt = new int[n];
for (int i = 0; i < n; i++) {
parent[i] = i;
cnt[i] = 1;
}
}
public int find(int a) {
if (parent[a] == a) {
return a;
}
parent[a] = find(parent[a]);
return parent[a];
}
public void unite(int a, int b) {
a = find(a);
b = find(b);
if (a == b) {
return;
}
if (rank[a] < rank[b]) {
parent[a] = b;
cnt[b] += cnt[a];
cnt[a] = cnt[b];
} else {
parent[b] = a;
cnt[a] += cnt[b];
cnt[b] = cnt[a];
if (rank[a] == rank[b]) {
rank[a]++;
}
}
}
public int groupCount(int a) {
return cnt[find(a)];
}
private boolean issame(int a, int b) {
return find(a) == find(b);
}
}
public static void doit(int[] a) {
int n = a.length;
int[] p = new int[n];
for (int i = 0; i < n ; i++) {
p[i] = i;
}
// 10
// 1 0 3 2 5 4 7 6 9 8
int cnt = 0;
do {
if (isOK(p, a)) {
debug(p);
cnt++;
}
} while (next_permutation(p));
debug(cnt, "ways");
}
public static int reverseDoit(int[] p, int[] a, int idx) {
int n = a.length;
if (idx == n) {
if (isOK(p, a)) {
debug(a);
return 1;
}
return 0;
}
int sum = 0;
for (int i = 0; i < n ; i++) {
a[idx] = i;
sum += reverseDoit(p, a, idx+1);
}
return sum;
}
public static boolean next_permutation(int[] a) {
int len = a.length;
int x = len - 2;
while (x >= 0 && a[x] >= a[x + 1]) {
x--;
}
if (x == -1) {
return false;
}
int y = len - 1;
while (y > x && a[y] <= a[x]) {
y--;
}
int tmp = a[x];
a[x] = a[y];
a[y] = tmp;
Arrays.sort(a, x + 1, len);
return true;
}
public static boolean isOK(int[] p, int[] a) {
for (int i = 0; i < a.length ; i++) {
if (!(p[i] == a[i] || p[p[i]] == a[i])) {
return false;
}
}
return true;
}
public static void debug(Object... o) {
System.err.println(Arrays.deepToString(o));
}
public static class InputReader {
private static final int BUFFER_LENGTH = 1 << 12;
private InputStream stream;
private byte[] buf = new byte[BUFFER_LENGTH];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
private int next() {
if (numChars == -1) {
throw new InputMismatchException();
}
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public char nextChar() {
return (char) skipWhileSpace();
}
public String nextToken() {
int c = skipWhileSpace();
StringBuilder res = new StringBuilder();
do {
res.append((char) c);
c = next();
} while (!isSpaceChar(c));
return res.toString();
}
public int nextInt() {
return (int) nextLong();
}
public long nextLong() {
int c = skipWhileSpace();
long sgn = 1;
if (c == '-') {
sgn = -1;
c = next();
}
long res = 0;
do {
if (c < '0' || c > '9') {
throw new InputMismatchException();
}
res *= 10;
res += c - '0';
c = next();
} while (!isSpaceChar(c));
return res * sgn;
}
public double nextDouble() {
return Double.valueOf(nextToken());
}
int skipWhileSpace() {
int c = next();
while (isSpaceChar(c)) {
c = next();
}
return c;
}
boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
}
} | JAVA |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<algorithm>
#include<cstdio>
#define mxn 1000010
#define LL long long
using namespace std;
const int mod=1e9+7;
int n,id,sl,fh,ans,a[mxn],d[mxn],l[mxn],dp[mxn],col[mxn],cir[mxn],tot[mxn];
int rd()
{
sl=0;fh=1;
char ch=getchar();
while(ch<'0'||'9'<ch) {if(ch=='-') fh=-1; ch=getchar();}
while('0'<=ch&&ch<='9') sl=sl*10+ch-'0',ch=getchar();
return sl*fh;
}
int main()
{
// freopen("dayinf.in","r",stdin);
// freopen("dayinf.out","w",stdout);
n=rd();int x;ans=1;
for(int i=1;i<=n;++i) a[i]=rd();
for(int i=1;i<=n;++i)
{
d[a[i]]++;
x=i;for(;!col[x];col[x]=i,x=a[x]);
if(col[x]==i) for(++id;!cir[x];cir[x]=id,x=a[x]);
}
for(int i=1;i<=n;++i)
if((cir[i]&&d[i]>2)||(!cir[i]&&d[i]>1))
return puts("0"),0;
for(int c,i=1;i<=n;++i)
if(!d[i])
{
for(c=0,x=i;!cir[x];++c,x=a[x]);
l[x]=c;
}
for(int ln,st,ed,cnt,i=1;i<=n;++i)
if(cir[i])
{
st=ed=cnt=0;
for(x=i;cir[x];cir[x]=0,x=a[x])
{
cnt++;
if(l[x])
{
if(!st) st=ed=cnt,ln=l[x];
else ans=1ll*ans*((l[x]<cnt-ed)+(l[x]<=cnt-ed))%mod,ed=cnt;
}
}
if(st) ans=1ll*ans*((ln<cnt+st-ed)+(ln<=cnt+st-ed))%mod;
else tot[cnt]++;
}
dp[0]=1;
for(int i=1;i<=n;++i)
if(tot[i])
{
for(int j=1;j<=tot[i];++j)
{
dp[j]=1ll*dp[j-1]*((i>1&&(i&1))+1)%mod;
if(j>1) dp[j]=(dp[j]+1ll*i*(j-1)%mod*dp[j-2])%mod;
}
ans=1ll*ans*dp[tot[i]]%mod;
}
printf("%d\n",ans);
fclose(stdin);fclose(stdout);
return 0;
} | CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<bits/stdc++.h>
#define mod 1000000007
typedef long long ll;
ll gi(){
ll x=0,f=1;
char ch=getchar();
while(!isdigit(ch))f^=ch=='-',ch=getchar();
while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
return f?x:-x;
}
int pow(int x,int y){
int ret=1;
while(y){
if(y&1)ret=1ll*ret*x%mod;
x=1ll*x*x%mod;y>>=1;
}
return ret;
}
int a[100010],p[100010],cir[100010],CIR,vis[100010];
int L[100010],D[100010],c[100010];
int fact[100010],ifact[100010],f[100010];
int C(int n,int m){return 1ll*fact[n]*ifact[m]%mod*ifact[n-m]%mod;}
int main(){
#ifdef XZZSB
//freopen("in.in","r",stdin);
//freopen("out.out","w",stdout);
#endif
int n=gi();
for(int i=1;i<=n;++i)a[i]=gi();
int ans=1;
for(int i=1;i<=n;++i)
if(!vis[i]){
int j;
for(j=i;!vis[j];j=a[j])vis[j]=i;
if(vis[j]==i){
++CIR;
while(!cir[j])cir[j]=CIR,j=a[j];
}
}
for(int i=1;i<=n;++i)
if(!cir[i]){
if(p[a[i]])return puts("0"),0;
p[a[i]]=i;
}
memset(vis,0,sizeof vis);
for(int i=1;i<=n;++i)
if(cir[i]&&!vis[cir[i]]){
vis[cir[i]]=1;
int m=0,id=0,j=i;
do{
++id;
if(p[j]){
L[++m]=id;D[m]=0;
for(int o=p[j];o;o=p[o])++D[m];
}
}while(j=a[j],j!=i);
if(m){
L[0]=L[m]-id,D[0]=D[m];
for(int j=1;j<=m;++j)
if(L[j]-L[j-1]>D[j])ans=2*ans%mod;
else if(L[j]-L[j-1]<D[j])return puts("0"),0;
}else ++c[id];
}
fact[0]=1;for(int i=1;i<=n;++i)fact[i]=1ll*fact[i-1]*i%mod;
ifact[n]=pow(fact[n],mod-2);for(int i=n;i;--i)ifact[i-1]=1ll*ifact[i]*i%mod;
f[0]=1;for(int i=1;i*2<=n;++i)f[i]=1ll*(2*i-1)*f[i-1]%mod;
for(int i=1;i<=n;++i){
int res=0;
for(int j=0;j*2<=c[i];++j)
res=(res+1ll*C(c[i],2*j)*f[j]%mod*pow(i,j)%mod*((i>1&&(i&1))?pow(2,c[i]-2*j):1))%mod;
ans=1ll*ans*res%mod;
}
printf("%d\n",ans);
return 0;
}
| CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include <bits/stdc++.h>
#define Return(x) { printf("%d\n",x); return ; }
typedef long long LL;
const int N=1e5+10,Mod=1e9+7;
int n,a[N],In[N],len[N],ccir[N],dp[N];
void Init() {
scanf("%d",&n);
for (int i=1;i<=n;++i) scanf("%d",&a[i]),In[a[i]]++;;
}
int vis[N];
bool cir[N];
int pd(int x,int y) {
if (y<x) return 0;
if (y==x) return 1;
return 2;
}
void Solve() {
for (int i=1;i<=n;++i) {
if (vis[i]) continue;
int x=i;
while (!vis[x]) vis[x]=i,x=a[x];
if (vis[x]!=i) continue;
while (!cir[x]) cir[x]=1,x=a[x];
}
for (int i=1;i<=n;++i) {
if (cir[i] && In[i]>2) Return(0);
if (!cir[i] && In[i]>1) Return(0);
}
for (int i=1;i<=n;++i) {
if (In[i]) continue;
int x=i,cnt=0; while (!cir[x]) x=a[x],cnt++;
len[x]=cnt;
}
int ans=1;
for (int i=1;i<=n;++i) if (cir[i]) {
int first=0,last=0,rk=0,x=i,flen;
while (cir[x]) {
++rk; cir[x]=0;
if (len[x]) {
if (!first) { first=rk; last=rk; flen=len[x]; }
else {
ans=1ll*ans*pd(len[x],rk-last)%Mod;
last=rk;
}
}
x=a[x];
}
if (!first) ccir[rk]++;else ans=1ll*ans*pd(flen,first+rk-last)%Mod;
}
for (int i=1;i<=n;++i) if (ccir[i]) {
dp[0]=1; int tp=(i&1)+(i>1);
for (int j=1;j<=ccir[i];++j) {
dp[j]=dp[j-1]*tp%Mod;
if (j>1) dp[j]=(dp[j]+1ll*(j-1)*i%Mod*dp[j-2])%Mod;
}
ans=1ll*ans*dp[ccir[i]]%Mod;
}
Return(ans);
}
int main() {
Init();
Solve();
return 0;
} | CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include <cstdio>
#include <algorithm>
using namespace std;
#define ILLEGAL puts("0"), exit(0)
const int MAXN = 100000;
const int MOD = int(1E9) + 7;
const int INV2 = (MOD + 1) / 2;
int pow_mod(int b, int p) {
int ret = 1;
for(int i=p;i;i>>=1,b=1LL*b*b%MOD)
if( i & 1 ) ret = 1LL*ret*b%MOD;
return ret;
}
int fct[MAXN + 5], ifct[MAXN + 5], f[MAXN + 5];
int comb(int n, int m) {
return 1LL*fct[n]*ifct[m]%MOD*ifct[n-m]%MOD;
}
void init() {
fct[0] = 1;
for(int i=1;i<=MAXN;i++)
fct[i] = 1LL*fct[i-1]*i%MOD;
ifct[MAXN] = pow_mod(fct[MAXN], MOD - 2);
for(int i=MAXN-1;i>=0;i--)
ifct[i] = 1LL*ifct[i+1]*(i+1)%MOD;
f[0] = 1;
for(int i=2;i<=MAXN;i+=2)
f[i] = 1LL*comb(i, 2)*f[i-2]%MOD*pow_mod(i/2, MOD-2)%MOD;
}
int a[MAXN + 5], N;
bool tag[MAXN + 5], vis[MAXN + 5];
int cnt[MAXN + 5], b[MAXN + 5], ind[MAXN + 5];
int main() {
init(), scanf("%d", &N);
for(int i=1;i<=N;i++)
scanf("%d", &a[i]), ind[a[i]]++;
for(int i=1;i<=N;i++)
if( ind[i] >= 3 ) ILLEGAL;
for(int i=1;i<=N;i++) {
if( ind[i] == 2 ) {
if( tag[i] ) continue;
int p = i;
while( !vis[p] )
vis[p] = true, p = a[p];
if( p != i ) ILLEGAL;
p = i;
do {
tag[p] = true, p = a[p];
}while( p != i );
}
}
for(int i=1;i<=N;i++) {
if( ind[i] == 0 ) {
int t = 0, p = i;
while( !tag[p] )
vis[p] = true, t++, p = a[p];
b[p] = t;
}
}
int ans = 1;
for(int i=1;i<=N;i++) {
if( b[i] ) {
int t = 0, p = i;
do {
t++, p = a[p];
}while( !b[p] );
if( t < b[p] ) ILLEGAL;
else if( t > b[p] ) ans = 2LL*ans%MOD;
}
}
for(int i=1;i<=N;i++) {
if( !vis[i] ) {
int t = 0, p = i;
do {
vis[p] = true, t++, p = a[p];
}while( !vis[p] );
cnt[t]++;
}
}
for(int i=1;i<=N;i++) {
int del = 0;
for(int j=0;j<=cnt[i];j+=2) {
int tmp = 1;
tmp = 1LL*tmp*comb(cnt[i], j)%MOD*f[j]%MOD*pow_mod(i, j/2)%MOD;
if( (i & 1) && i != 1 ) {
tmp = 1LL*tmp*pow_mod(2, cnt[i] - j)%MOD;
}
del = (del + tmp) % MOD;
}
ans = 1LL*ans*del%MOD;
}
printf("%d\n", ans);
} | CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<bits/stdc++.h>
using namespace std;
#define RI register int
int read() {
int q=0;char ch=' ';
while(ch<'0'||ch>'9') ch=getchar();
while(ch>='0'&&ch<='9') q=q*10+ch-'0',ch=getchar();
return q;
}
const int mod=1e9+7,N=100005;
int n,ans;
int a[N],du[N],kj[N],vis[N],hjw[N],sum[N],f[N];
int qm(int x) {return x>=mod?x-mod:x;}
void workkj(int x) {
int now=0,fr=0,ed=0,hpp=0;
while(kj[x]) {
++now,kj[x]=0;
if(hjw[x]) {
if(!fr) ed=fr=now,hpp=hjw[x];
else {
int kl=(hjw[x]<now-ed)+(hjw[x]<=now-ed);
ans=1LL*ans*kl%mod,ed=now;
}
}
x=a[x];
}
if(!fr) ++sum[now];
else {
int kl=(hpp<now-ed+fr)+(hpp<=now-ed+fr);
ans=1LL*ans*kl%mod;
}
}
void work() {
for(RI i=1;i<=n;++i) {
if(du[i]) continue;
int x=i,len=0;while(!kj[x]) x=a[x],++len;
hjw[x]=len;
}
ans=1;
for(RI i=1;i<=n;++i) if(kj[i]) workkj(i);
for(RI i=1;i<=n;++i) {
if(!sum[i]) continue;
f[0]=1;
for(RI j=1;j<=sum[i];++j) {
if(i>1&&(i&1)) f[j]=qm(f[j-1]+f[j-1]);
else f[j]=f[j-1];
if(j>1) f[j]=qm(f[j]+1LL*f[j-2]*(j-1)%mod*i%mod);
}
ans=1LL*ans*f[sum[i]]%mod;
}
}
int main()
{
n=read();
for(RI i=1;i<=n;++i) a[i]=read(),++du[a[i]];
for(RI i=1;i<=n;++i) {
if(vis[i]) continue;
int x=i;while(!vis[x]) vis[x]=i,x=a[x];
if(vis[x]!=i) continue;
while(!kj[x]) kj[x]=1,x=a[x];
}
for(RI i=1;i<=n;++i)
if((kj[i]&&du[i]>2)||(!kj[i]&&du[i]>1)) {puts("0");
return 0;
}
work();
printf("%d\n",ans);
return 0;
} | CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | //Relive your past life.
//Face your demons.
//The past is never dead,it is not even past.
//The memories are not only the key to the past but...also to the future.
//coded in Rusty Lake
#include<cmath>
#include<math.h>
#include<ctype.h>
#include<algorithm>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cerrno>
#include<cfloat>
#include<ciso646>
#include<climits>
#include<clocale>
#include<complex>
#include<csetjmp>
#include<csignal>
#include<cstdarg>
#include<cstddef>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cwchar>
#include<cwctype>
#include<deque>
#include<exception>
#include<fstream>
#include<functional>
#include<iomanip>
#include<ios>
#include<iosfwd>
#include<iostream>
#include<istream>
#include<iterator>
#include<limits>
#include<list>
#include<locale>
#include<map>
#include<memory>
#include<new>
#include<numeric>
#include<ostream>
#include<queue>
#include<set>
#include<sstream>
#include<stack>
#include<stdexcept>
#include<streambuf>
#include<string>
#include<typeinfo>
#include<utility>
#include<valarray>
#include<vector>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
#define ll long long
#define pb push_back
#define mp make_pair
#define orz 1000000007ll
using namespace std;
int n,a[100005],d[100005],c[100005],cnt[100005],f[100005],s[100005];
ll ans=1,dp[100005];
void get(int x){
int t=0;
ll res=1;
cnt[x]=1;
while(cnt[x]<2){
x=a[x];
++cnt[x],++t;
if(f[x]){
if(t<f[x])res=0;
else if(t>f[x])res=res*2%orz;
t=0;
}
}
(ans*=res)%=orz;
}
int main(){
scanf("%d",&n);
for(int i=1;i<=n;++i)scanf("%d",a+i),++d[a[i]],c[i]=1;
queue<int> q;
for(int i=1;i<=n;++i)if(!d[i])q.push(i);
while(!q.empty()){
int x=q.front();
q.pop();
c[x]=0;
++cnt[a[x]];
if(cnt[a[x]]==d[a[x]])q.push(a[x]);
}
for(int i=1;i<=n;++i){
if(d[i]-c[i]>1){
puts("0");
return 0;
}
}
for(int i=1;i<=n;++i){
if(!d[i]){
int x=i,t=0;
while(!c[x])x=a[x],++t;
f[x]=t;
}
}
memset(cnt,0,sizeof(cnt));
for(int i=1;i<=n;++i)if(!cnt[i]&&c[i]&&f[i])get(i);
for(int i=1;i<=n;++i){
if(!cnt[i]&&c[i]){
int x=i,sum=0;
while(!cnt[x])cnt[x]=1,++sum,x=a[x];
++s[sum];
}
}
for(int i=1;i<=n;++i){
dp[0]=1;
if(!s[i]) continue;
int o;
if(i>1&&(i&1))o=2;
else o=1;
for(int j=1;j<=s[i];++j){
dp[j]=dp[j-1]*o%orz;
if(j>1)(dp[j]+=dp[j-2]*(j-1)%orz*i)%=orz;
}
(ans*=dp[s[i]])%=orz;
}
printf("%lld\n",ans);
//system("pause");
return 0;
}
| CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include <queue>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
#define N 100100
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define mod 1000000007
#define rep(x, a, b) for(int x=a; x<=b; x++)
#define drp(x, a, b) for(int x=a; x>=b; x--)
int st[N<<1], deg[N], d[N], n, vis[N], a[N], tot, num[N], l[N], ans=1, fac[N], inv[N];
typedef long long LL;
int power(int x, int y){
int an = 1;
for(; y; y >>= 1, x = (LL)x * x % mod) if(y & 1) an = (LL)an * x % mod;
return an;
}
int C(int x, int y){
if(x < 0 || y < 0 || x < y) return 0;
return (LL)fac[x] * inv[y] % mod * inv[x - y] % mod;
}
void renew(int &x, const int y){
x += y;
if(x < 0) x += mod;
if(x >= mod) x -= mod;
}
void init(){
fac[0] = inv[0] = fac[1] = inv[1] = 1;
rep(i, 2, n){
fac[i] = (LL)fac[i - 1] * i % mod;
inv[i] = power(fac[i], mod - 2);
}
}
int getdp(){
int fr = 0;
rep(i, 1, tot) st[i + tot] = st[i];
rep(i, 1, tot) if(l[st[i]]){
fr = i;
break;
}
reverse(st + fr + 1, st + fr + tot);
int a = fr + l[st[fr]] - 1, b = a + 1, c = 1;
rep(i, fr + 1, fr + tot - 1)if(l[st[i]]){
int na = i + l[st[i]] - 1, nb = na + 1, cc = 0;
if(a < i) renew(cc, c);
if(b < i) renew(cc, c);
a = na;
b = nb;
c = cc;
}
int ans = 0;
if(a < fr + tot) renew(ans, c);
if(b < fr + tot) renew(ans, c);
return ans;
}
int que[N], h, t;
int main(){
scanf("%d", &n);
rep(i, 1, n) scanf("%d", a + i);
rep(i, 1, n){
deg[a[i]] ++;
if(deg[a[i]] > 2){ puts("0"); exit(0); }
}
rep(i, 1, n) d[i] = deg[i];
rep(i, 1, n) if(!d[i]) que[++t] = i;
rep(h, 1, t){
int u = que[h];
--d[a[u]];
vis[u] = 1;
if(!d[a[u]]) que[++t] = a[u];
}
rep(i, 1, n) if(vis[i] && deg[i] >= 2){
puts("0");
exit(0);
}
rep(i, 1, n) if(vis[i] && !deg[i]){
int x = i, cnt = 0;
for(x = i; vis[x]; x = a[x])cnt++;
l[x] = cnt;
}
init();
ans=1;
rep(i, 1, n)if(!vis[i])
{
int u = i, cu = 1;
tot = 0;
int res = 0;
do{
st[++tot] = u;
cu &= !l[u];
vis[u] = 1;
u = a[u];
}while(u != i);
if(cu){
num[tot] ++;
res = 1;
}else{
res = getdp();
}
ans = (LL)ans * res % mod;
}
rep(i, 1, n) if(num[i])
{
int tot = num[i];
int res = 0;
for(int x = 0; x <= tot; x += 2){
int cb = 1;
cb = (LL)cb * power(i, (x >> 1)) % mod;
cb = (LL)cb * C(tot, x) % mod;
cb = (LL)cb * fac[x] % mod;
cb = (LL)cb * inv[x >> 1] % mod;
cb = (LL)cb * power((mod + 1) >> 1, x >> 1) % mod;
if((i > 1) && (i & 1)){
cb = (LL)cb * power(2, tot - x) % mod;
}
renew(res, cb);
}
ans = (LL)ans * res % mod;
}
printf("%d\n", ans);
} | CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<cstdio>
#include<vector>
using namespace std;
#define sz(x) (int)x.size()
const int _ = 1e5 + 7 , P = 1e9 + 7;
vector < int > nxt1[_] , nxt2[_]; int N;
bool vis[_] , incirc[_];
int dfs1(int x , vector < int > &pot){
vis[x] = 1; int r = 0;
for(auto t : nxt1[x])
if(!vis[t]){int v = dfs1(t , pot); if(v){pot.push_back(x); incirc[x] = 1; r = v;}}
else if(vis[t] && !incirc[t]){r = t; pot.push_back(x); incirc[x] = 1;}
return r == x ? 0 : r;
}
int dfs2(int x){
int len = 0; vis[x] = 1;
for(auto t : nxt2[x])
if(!incirc[t]) if(!len) len = dfs2(t) + 1; else len = -1e9;
return len;
}
int main(){
scanf("%d" , &N); for(int i = 1 ; i <= N ; ++i){int x; scanf("%d" , &x); nxt1[i].push_back(x); nxt2[x].push_back(i);}
int ans = 1; static int num[_];
for(int i = 1 ; i <= N ; ++i)
if(!vis[i]){
vector < int > pot; dfs1(i , pot); vector < pair < int , int > > link;
for(int i = 0 ; i < sz(pot) ; ++i){
int p = dfs2(pot[i]); if(p < 0) ans = 0; else if(p > 0) link.push_back(make_pair(i , p));
}
if(link.empty()){++num[sz(pot)]; continue;}
for(int i = 1 ; i < sz(link) ; ++i)
if(link[i].first < link[i - 1].first + link[i - 1].second) ans = 0;
else if(link[i].first > link[i - 1].first + link[i - 1].second) ans = 2 * ans % P;
if(link.back().first + link.back().second > link.front().first + sz(pot)) ans = 0;
else if(link.back().first + link.back().second < link.front().first + sz(pot)) ans = 2 * ans % P;
}
for(int i = 1 ; i <= N ; ++i)
if(num[i]){
static int dp[_]; dp[0] = 1;
for(int j = 1 ; j <= num[i] ; ++j)
dp[j] = ((i > 1 && (i & 1) ? 2 : 1) * dp[j - 1] + (j > 1 ? 1ll * dp[j - 2] * (j - 1) % P * i : 0)) % P;
ans = 1ll * ans * dp[num[i]] % P;
}
printf("%d\n" , ans);
return 0;
}
| CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 |
#include <bits/stdc++.h>
#define MAXN 100005
typedef long long lint;
using namespace std;
const int mod = 1e9 + 7;
int n, a[MAXN], val[MAXN], deg[MAXN];
int sum[MAXN];
lint f[MAXN];
bool oncir[MAXN];
int visit[MAXN];
lint ans = 1;
int read() {
char c = getchar();
int x = 0;
while (!isdigit(c))
c = getchar();
while (isdigit(c)) {
x = (x << 3) + (x << 1) + c - '0';
c = getchar();
}
return x;
}
void calc(int u) {
vector<int> g;
while (oncir[u]) {
oncir[u] = false;
g.push_back(u);
u = a[u];
}
reverse(g.begin(), g.end());
int first = -1, last = -1;
for (int i = 0; i < (int)g.size(); ++i) {
int u = g[i];
if (val[u]) {
if (!(~first)) {
last = first = i;
continue;
}
int l1 = val[g[last]], l2 = i - last;
ans = ans * ((l2 > l1) + (l2 >= l1)) % mod;
last = i;
}
}
if (~first && first == last) {
int l1 = val[g[last]], l2 = (int)g.size();
ans = ans * ((l2 > l1) + (l2 >= l1)) % mod;
}
else if (~first) {
int l1 = val[g[last]], l2 = (int)g.size() - last + first;
ans = ans * ((l2 > l1) + (l2 >= l1)) % mod;
}
else
++sum[(int)g.size()];
}
void solve() {
for (int i = 1; i <= n; ++i)
if (oncir[i])
calc(i);
for (int i = 1; i <= n; ++i) {
f[0] = 1;
for (int j = 1; j <= sum[i]; ++j) {
if ((i & 1) && i != 1)
f[j] = f[j - 1] * 2 % mod;
else
f[j] = f[j - 1];
if (j > 1)
f[j] = (f[j] + f[j - 2] * i % mod * (j - 1) % mod) % mod;
}
ans = ans * f[sum[i]] % mod;
}
}
int main() {
// freopen("1_12.txt", "r",stdin);
n = read();
for (int i = 1; i <= n; ++i) {
a[i] = read();
++deg[a[i]];
}
for (int i = 1; i <= n; ++i) {
int u = i;
if (visit[u])
continue;
while (!visit[u]) {
visit[u] = i;
u = a[u];
}
if (visit[u] != i)
continue;
while (!oncir[u]) {
oncir[u] = true;
u = a[u];
}
}
for (int i = 1; i <= n; ++i)
if ((oncir[i] && deg[i] > 2) || (!oncir[i] && deg[i] > 1)) {
puts("0");
return 0;
}
for (int i = 1; i <= n; ++i) {
if (deg[i])
continue;
int u = i, d = 0;
while (!oncir[u]) {
u = a[u];
++d;
}
val[u] = d;
}
solve();
printf("%lld\n", ans);
}
| CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include <cstdio>
#define MOD 1000000007
long long dp[100005], ans = 1;
int deg[100005], nxt[100005], app[100005], pre[100005];
bool vis[100005];
int main()
{
// freopen("AGC008-E.in", "r", stdin);
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
{
scanf("%d", nxt + i);
deg[--nxt[i]]++;
}
for (int i = 0; i < n; i++)
{
if (deg[i] > 2)
{
puts("0");
return 0;
}
if (deg[i] != 2 || vis[i])
continue;
int u = i;
do
{
if (vis[u])
{
puts("0");
return 0;
}
vis[u] = true;
pre[nxt[u]] = u;
u = nxt[u];
}
while (u != i);
}
for (int i = 0; i < n; i++)
{
if (deg[i])
continue;
int u = i, foot_len = 0, cyc_len = 0;
while (!vis[u])
{
vis[u] = true;
u = nxt[u];
foot_len++;
}
do
{
u = pre[u];
cyc_len++;
}
while (deg[u] == 1);
if (foot_len < cyc_len)
ans = ans * 2 % MOD;
if (foot_len > cyc_len)
{
puts("0");
return 0;
}
}
for (int i = 0; i < n; i++)
{
if (vis[i])
continue;
int u = i, len = 0;
do
{
vis[u] = true;
u = nxt[u];
len++;
}
while (u != i);
app[len]++;
}
for (int i = 1; i <= n; i++)
{
dp[0] = 1;
for (int j = 1; j <= app[i]; j++)
{
dp[j] = dp[j - 1] * ((i & 1 ^ 1) || i == 1 ? 1 : 2) % MOD;
if (j >= 2)
(dp[j] += dp[j - 2] * (j - 1) * i) %= MOD;
}
ans = ans * dp[app[i]] % MOD;
}
printf("%lld\n", ans);
return 0;
}
| CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;
const int mod = 1e9 + 7;
int n, a[100005];
int indeg[100005];
int cnt[100005];
vector<int> gph[100005];
vector<int> v;
int dfs2(int x, int p){
vector<int> v;
for(auto &i : gph[x]){
if(indeg[i] || i == p) continue;
v.push_back(dfs2(i, x) + 1);
}
if(v.size() >= 2) return 1e8;
if(v.size() == 0) return 0;
if(v.size() == 1) return v[0];
}
lint ipow(lint x, lint p){
lint ret = 1, piv = x % mod;
while(p){
if(p&1) ret *= piv;
piv *= piv;
ret %= mod;
piv %= mod;
p >>= 1;
}
return ret % mod;
}
int rv[100005], lb[100005];
lint solve(int x){
queue<int> que;
for(auto &i : v){
if(indeg[i] == 0) que.push(i);
}
while(!que.empty()){
int x = que.front();
que.pop();
indeg[a[x]]--;
if(indeg[a[x]] == 0) que.push(a[x]);
}
vector<int> w;
lint ret = 1;
for(auto &i : v){
if(indeg[i] == 1){
w.push_back(i);
for(int j=a[i]; j!=i; j=a[j]){
w.push_back(j);
}
reverse(w.begin(), w.end());
for(int i=0; i<w.size(); i++){
rv[w[i]] = i;
lb[w[i]] = dfs2(w[i], -1);
if(lb[w[i]] > 1e7) return 0;
}
for(int i=0; i<w.size(); i++){
if(lb[w[i]] > 0){
int brk = 0;
for(int j=i+1; j<=i+w.size(); j++){
if(lb[w[j%w.size()]] > 0){
brk = j - i;
break;
}
}
if(brk < lb[w[i]]) return 0;
if(brk > lb[w[i]]) ret *= 2;
ret %= mod;
}
}
break;
}
}
return ret;
}
int vis[100005];
void dfs(int x){
v.push_back(x);
vis[x] = 1;
for(auto &i : gph[x]){
if(!vis[i]) dfs(i);
}
}
lint fact[100005], invf[100005], invp[100005];
lint match(int x, int k){
return (fact[x] * invf[x-2*k] % mod) * (invf[k] * invp[k] % mod) % mod;
}
int main(){
scanf("%d",&n);
fact[0] = invf[0] = invp[0] = 1;
for(int i=1; i<=n; i++){
fact[i] = fact[i-1] * i % mod;
invf[i] = ipow(fact[i], mod-2);
invp[i] = invp[i-1] * (mod + 1) / 2;
invp[i] %= mod;
}
for(int i=1; i<=n; i++){
scanf("%d",&a[i]);
indeg[a[i]]++;
gph[i].push_back(a[i]);
gph[a[i]].push_back(i);
}
lint ret = 1;
for(int i=1; i<=n; i++){
if(vis[i]) continue;
v.clear();
dfs(i);
int notsimple = 0;
for(auto &i : v){
if(indeg[i] != 1){
notsimple = 1;
}
}
if(!notsimple){
cnt[v.size()]++;
}
else{
lint w = solve(i);
ret *= w;
ret %= mod;
}
}
for(int i=1; i<=n; i++){
if(cnt[i] >= 1){
lint tmp = 0;
for(int j=0; j<=cnt[i]/2; j++){
lint tmp2 = match(cnt[i], j) * ipow(i, j)% mod;
if(i > 2 && i % 2 == 1) tmp2 *= ipow(2, cnt[i] - 2 * j) % mod;
tmp += tmp2;
tmp %= mod;
}
ret *= tmp;
ret %= mod;
}
}
cout << ret;
}
| CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<bits/stdc++.h>
#define N 100005
#define LL long long
using namespace std;
const int mo=1e9+7,I2=(mo+1)/2;
vector<int> G[N],E[N];
int n,a[N],ufs[N],g[N],d[N],c[N],w[N],lw,vis[N],nu[N],ans=1,cnt,tot,L[N],X[N],cir[N],fc[N],xf[N],f[N];
LL fpm(LL x,LL y){ LL s=1; while(y){ if(y&1) s=(s*x)%mo; y>>=1,x=(x*x)%mo;} return s;}
LL C(int n,int m){ return 1LL*fc[n]*xf[m]%mo*xf[n-m]%mo;}
int find(int x){ return ufs[x] ? ufs[x]=find(ufs[x]) : x;}
void dfs(int t)
{
int i; vis[t]=1,w[++lw]=t;
for(i=0;i<G[t].size();i++)
if(!vis[G[t][i]]) dfs(G[t][i]);
}
int DFS(int t)
{
int i,cnt=0;
for(i=0;i<E[t].size();i++)
if(!cir[E[t][i]]) cnt++;
if(cnt>1) ans=0;
for(i=0;i<E[t].size();i++)
if(!cir[E[t][i]]) return DFS(E[t][i])+1;
return 0;
}
void solve(int t)
{
int i,j,x,y,z;
lw=cnt=0,dfs(t);
for(i=1;i<=lw;i++)
if(d[w[i]]&1) break;
if(i>lw){ c[lw]++; return ;}
for(i=1;i<=lw;i++)
for(j=0;j<E[w[i]].size();j++){
if(find(E[w[i]][j])==find(w[i])) y=w[i],x=E[w[i]][j];
else ufs[E[w[i]][j]]=w[i];
}
tot=0,cir[x]=cir[y]=1;
for(i=y;i!=x;i=a[i]) cir[i]=1;
for(i=y;i!=x;i=a[i]){
nu[i]=++cnt,z=DFS(i);
if(z) ++tot,L[tot]=z,X[tot]=cnt;
}
nu[x]=++cnt,z=DFS(x);
if(z) ++tot,L[tot]=z,X[tot]=cnt;
for(i=2;i<=tot;i++){
if(L[i]>X[i]-X[i-1]) ans=0;
if(L[i]<X[i]-X[i-1]) ans=(ans+ans)%mo;
}
if(L[1]>X[1]+cnt-X[tot]) ans=0;
if(L[1]<X[1]+cnt-X[tot]) ans=(ans+ans)%mo;
}
void dp(int t)
{
int i,sum=0,pw=1;
if(t%2==1&&t>1)
for(i=1;i<=c[t];i++) pw=(pw*2)%mo;
for(i=0;i*2<=c[t];i++){
sum=(sum+C(c[t],2*i)*pw%mo*f[i]%mo)%mo,pw=1LL*pw*t%mo;
if((t&1)&&t>1) pw=1LL*pw*I2%mo*I2%mo;
}
ans=1LL*ans*sum%mo;
}
int main()
{
int i;
scanf("%d",&n),fc[0]=xf[0]=f[0]=1;
for(i=1;i<=n;i++){
fc[i]=1LL*fc[i-1]*i%mo;
xf[i]=fpm(fc[i],mo-2);
f[i]=f[i-1]*(2LL*i-1)%mo;
}
for(i=1;i<=n;i++){
scanf("%d",&a[i]);
G[a[i]].push_back(i);
G[i].push_back(a[i]);
E[a[i]].push_back(i);
d[i]++,d[a[i]]++;
}
for(i=1;i<=n;i++)
if(d[i]>=4){ printf("0"); return 0;}
for(i=1;i<=n;i++){
if(vis[i]) continue;
solve(i);
}
for(i=1;i<=n;i++) dp(i);
cout<<ans;
return 0;
}
| CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#define N 100010
#define gc getchar()
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define lint long long
#define mod 1000000007
#define debug(x) cerr<<#x<<"="<<x
#define sp <<" "
#define ln <<endl
using namespace std; inline int inn() { int x,ch;while((ch=gc)<'0'||ch>'9'); x=ch^'0';while((ch=gc)>='0'&&ch<='9') x=(x<<1)+(x<<3)+(ch^'0');return x; } int fac[N],facinv[N],f[N],onc[N],sz[N],p[N],cnt[N]; int cyc_cnt,vis[N],l[N],pre[N],jhs[N],in[N],mi[N],it[N]; inline int fast_pow(int x,int k,int ans=1) { for(;k;k>>=1,x=(lint)x*x%mod) (k&1)?ans=(lint)ans*x%mod:0;return ans; } inline int prelude(int n) { rep(i,fac[0]=1,n) fac[i]=(lint)fac[i-1]*i%mod; facinv[n]=fast_pow(fac[n],mod-2); for(int i=n-1;i>=0;i--) facinv[i]=facinv[i+1]*(i+1ll)%mod; rep(i,mi[0]=1,n) mi[i]=mi[i-1]*2,(mi[i]>=mod?mi[i]-=mod:0); rep(i,f[0]=1,n/2) f[i]=f[i-1]*(2*i-1ll)%mod;return 0; } inline int C(int n,int m) { if(n<0||m<0||n<m) return 0;return (lint)fac[n]*facinv[m]%mod*facinv[n-m]%mod; } int main() { int n=inn();rep(i,1,n) in[p[i]=inn()]++; for(int i=1,x;i<=n;i++) { for(x=i;!vis[x];x=p[x]) vis[x]=i; if(vis[x]^i) continue; for(cyc_cnt++;!onc[x];x=p[x]) onc[x]=cyc_cnt,sz[cyc_cnt]++; } memset(vis,0,sizeof(int)*(n+1)); for(int i=1,x,c;i<=n;i++) if(!in[i]) { for(x=i,c=0;!onc[x];vis[x]=1,x=p[x],c++) if(vis[x]) return !printf("0\n"); if(vis[x]) return !printf("0\n");vis[x]=1,l[x]=c; } rep(i,1,n) if(onc[i]&&l[i]) jhs[onc[i]]=1; rep(i,1,cyc_cnt) if(!jhs[i]) cnt[sz[i]]++; prelude(n);int ans=1; for(int i=1,k,t,s,v;i<=n;i++) if(cnt[i]) { rep(j,it[0]=1,cnt[i]/2) it[j]=(lint)it[j-1]*i%mod; for(k=cnt[i],t=0,s=0;t<=cnt[i]/2;t++) v=(lint)C(k,2*t)*f[t]%mod*it[t]%mod, (((i&1)&&i>1)?v=(lint)v*mi[k-2*t]%mod:0), s+=v,(s>=mod?s-=mod:0); ans=(lint)ans*s%mod; } for(int i=1,x,c;i<=n;i++) if(onc[i]&&l[i]) { for(x=i,c=1;!l[p[x]];x=p[x],c++);pre[p[x]]=c; } rep(i,1,n) if(onc[i]&&l[i]) { if(l[i]>pre[i]) return !printf("0\n"); else if(l[i]<pre[i]) ans=ans*2,(ans>=mod?ans-=mod:0); } return !printf("%d\n",ans); }
| CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <cmath>
#include <bitset>
#include <vector>
#include <map>
#include <set>
#include <queue>
#include <deque>
#include <algorithm>
#include <complex>
#include <unordered_map>
#include <unordered_set>
#include <random>
#include <cassert>
#include <fstream>
#include <utility>
#include <functional>
#include <time.h>
#include <stack>
#include <array>
#define popcount __builtin_popcount
using namespace std;
typedef long long int ll;
typedef pair<int, int> P;
const ll MOD=1e9+7;
ll powmod(ll a, ll k){
ll ap=a, ans=1;
while(k){
if(k&1){
ans*=ap;
ans%=MOD;
}
ap=ap*ap;
ap%=MOD;
k>>=1;
}
return ans;
}
ll inv(ll a){
return powmod(a, MOD-2);
}
ll f[100010], invf[100010];
void fac(int n){
f[0]=1;
for(ll i=1; i<=n; i++) f[i]=f[i-1]*i%MOD;
invf[n]=inv(f[n]);
for(ll i=n-1; i>=0; i--) invf[i]=invf[i+1]*(i+1)%MOD;
}
ll comb(int x, int y){
if(!(0<=y && y<=x)) return 0;
return f[x]*invf[y]%MOD*invf[x-y]%MOD;
}
ll p2[100010];
int main()
{
int n; cin>>n;
p2[0]=1;
for(int i=1; i<=n; i++) p2[i]=p2[i-1]*2%MOD;
//vector<int> g[100010];
int deg[100010]={};
int a[100010];
for(int i=0; i<n; i++){
cin>>a[i];
a[i]--;
//g[i].push_back(a);
deg[a[i]]++;
}
for(int i=0; i<n; i++){
if(deg[i]>=3){
cout<<0<<endl;
return 0;
}
}
int myon[100010]={};
int nuo[100010]={};
bool used[100010]={};
for(int i=0; i<n; i++){
if(deg[i]==0){
int x=i, cnt=0;
while(deg[x]<2){
used[x]=1;
cnt++;
x=a[x];
}
myon[x]=cnt;
}
}
for(int i=0; i<n; i++){
if(deg[i]==2){
int x=i, cnt=0;
do{
used[x]=1;
cnt++;
x=a[x];
}while(deg[x]<2);
nuo[x]=cnt;
}
}
map<int, int> mp;
for(int i=0; i<n; i++){
if(!used[i]){
int x=i, cnt=0;
while(!used[x]){
used[x]=1;
cnt++;
x=a[x];
}
mp[cnt]++;
}
}
ll ans=1;
for(int i=0; i<n; i++){
if(myon[i]){
//cout<<nuo[i]<<" "<<myon[i]<<endl;
if(nuo[i]<myon[i]){
cout<<0<<endl;
return 0;
}
if(nuo[i]>=myon[i]+1) (ans*=2)%=MOD;
}
}
//cout<<"myon"<<endl;
fac(n);
for(auto p:mp){
int d=p.first, m=p.second;
//cout<<d<<" "<<m<<endl;
ll pp=1, q=1, s=0;
for(int i=0; i<=m/2; i++){
if((d&1) && d>1) (s+=pp*q%MOD*comb(m, 2*i)%MOD*p2[m-2*i])%=MOD;
else (s+=pp*q%MOD*comb(m, 2*i))%=MOD;
(pp*=d)%=MOD;
(q*=(2*i+1))%=MOD;
}
(ans*=s)%=MOD;
}
cout<<ans<<endl;
return 0;
} | CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<bits/stdc++.h>
#define ll long long
#define ull unsigned ll
#define uint unsigned
#define db long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define IT iterator
#define PB push_back
#define MK make_pair
#define LB lower_bound
#define UB upper_bound
#define EB emplace_back
#define fi first
#define se second
#define For(i,j,k) for (int i=(int)(j);i<=(int)(k);i++)
#define Rep(i,j,k) for (int i=(int)(j);i>=(int)(k);i--)
#define UPD(x,y) (((x)+=(y))>=mo?x-=mo:233)
#define CLR(a,v) memset(a,v,sizeof(a));
#define CPY(a,b) memcpy(a,b,sizeof(a));
#define LS3 k*2,l,mid
#define RS3 k*2+1,mid+1,r
#define LS5 k*2,l,mid,x,y
#define RS5 k*2+1,mid+1,r,x,y
#define GET pushdown(k);int mid=(l+r)/2
#define INF ((1ll<<60)-233)
#define sqr(x) ((x)*(x))
#define debug puts("wzpkking")
using namespace std;
const int N=100005;
const int mo=1000000007;
int n,ans=1;
int a[N],deg[N];
int pre[N],cnt[N],f[N];
bool vis[N];
int main(){
scanf("%d",&n);
for (int i=1;i<=n;i++)
scanf("%d",&a[i]),deg[a[i]]++;
for (int i=1;i<=n;i++){
if (deg[i]>2) return puts("0"),0;
if (deg[i]<2||vis[i]) continue;
int j=i;
do{
if (vis[j]) return puts("0"),0;
vis[j]=1; pre[a[j]]=j; j=a[j];
}while (i!=j);
}
for (int i=1;i<=n;i++) if (!deg[i]){
int l1=0,l2=0,j=i;
for (;!vis[j];j=a[j]) vis[j]=1,l1++;
for (j=pre[j],l2++;deg[j]==1;j=pre[j],l2++);
if (l1>l2) return puts("0"),0;
if (l1<l2) ans=1ll*ans*2%mo;
}
for (int i=1;i<=n;i++) if (!vis[i]){
int l=0;
for (int j=i;!vis[j];j=a[j])
vis[j]=1,l++;
cnt[l]++;
}
for (int i=1;i<=n;i++){
int l=((i&1)&&(i>1))+1;
f[0]=1; f[1]=l;
for (int j=2;j<=cnt[i];j++)
f[j]=(1ll*f[j-2]*(j-1)*i+1ll*f[j-1]*l)%mo;
ans=1ll*ans*f[cnt[i]]%mo;
}
printf("%d\n",ans);
} | CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<bits/stdc++.h>
using namespace std;
const int N(111111);
int modulo(1e9 + 7);
int a[N], stmp[N], zc[N], cnt[N], deg[N], rela[N], f[N];
bool vst[N], incyc[N];
int getr(int x) {
int p(x);
while(p != rela[p])
p = rela[p];
int p1(p); p = x;
while(p != rela[p]) {
int p2(rela[p]);
rela[p] = p1;
p = p2;
}
return p1;
}
int main() {
int n;
scanf("%d", &n);
for(int i(1); i <= n; i++) {
scanf("%d", &a[i]);
deg[a[i]]++;
if(deg[a[i]] >= 3) {
printf("0\n");
return 0;
}
}
int t(0);
vector<vector<int> > cyc;
fill(vst + 1, vst + 1 + n, false);
for(int i(1); i <= n; i++) {
if(!vst[i]) {
int x(i);
++t;
while(!vst[x]) {
stmp[x] = t;
vst[x] = true;
x = a[x];
}
if(stmp[x] == t) {
int y(x);
vector<int> vec;
do {
vec.push_back(y);
incyc[y] = true;
y = a[y];
} while(y != x);
cyc.push_back(vec);
}
}
}
for(int i(1); i <= n; i++)
if(!incyc[i] && deg[i] >= 2) {
printf("0\n");
return 0;
}
for(int i(1); i <= n; i++)
rela[i] = i;
for(int i(1); i <= n; i++) {
if(!incyc[i]) {
rela[getr(i)] = getr(a[i]);
}
}
for(int i(1); i <= n; i++)
if(!incyc[i])
cnt[getr(i)]++;
int ans(1);
for(int i(0); i < (int)cyc.size(); i++) {
int len(cyc[i].size());
int cz(0);
for(int j(0); j < len; j++) {
if(cnt[getr(cyc[i][j])]) {
int c(0);
for(int k((j - 1 + len) % len); k != j && cnt[getr(cyc[i][k])] == 0; k = (k - 1 + len) % len) {
c++;
}
if(c + 1 == cnt[getr(cyc[i][j])]) {
}else if(c + 1 > cnt[getr(cyc[i][j])]) {
ans = (ans * 2 % modulo);
}else {
ans = 0;
}
}else {
cz++;
}
}
if(cz == len) {
zc[len]++;
}
}
for(int i(1); i <= n; i++) {
if(zc[i]) {
//cout << i << '!' << zc[i] << endl;
f[1] = 1 + (i % 2 == 1 && i > 1);
f[0] = 1;
for(int j(2); j <= zc[i]; j++) {
f[j] = ((1 + (i % 2 == 1 && i > 1)) * f[j - 1] + (j - 1ll) * i * f[j - 2]) % modulo;
}
ans = (long long)ans * f[zc[i]] % modulo;
}
}
cout << ans << endl;
}
| CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | /*
https://blog.csdn.net/litble/article/details/83118814
图论神啊,好题好题好题!!!
i 向 pi 连条边,构成的图简化以后就都是 “i 向 ai 连条边” 这个图了!!!
注意:是由一个环和若干条指向环的链组成的基环内向树,至于为什么是链,详见样例4
现在手上只有 a,那么就**倒推**有多少种可能图吧
*/
#include <bits/stdc++.h>
#define rep(i, x, y) for (int i = x; i <= y; i++)
using namespace std;
const int mod = 1e9 + 7, N = 1e5 + 10;
typedef long long ll;
int n, a[N], deg[N], vis[N], cir[N], L[N], sum[N];
ll ans, f[N];
void calc(int x) {
int now = 0, fr = 0, ed = 0, l = 0;
// fr: 第一个脚的位置 ed: 上一个脚的位置 l: 第一个脚的长度 now: 当前节点是从 x 开始走环走到的第几个点
while (cir[x]) {
++now, cir[x] = 0;
if (L[x]) {
if (!fr) ed = fr = now, l = L[x];
else {
int tmp = (L[x] < now - ed) + (L[x] <= now - ed);
ans = 1ll * ans * tmp % mod, ed = now;
}
}
x = a[x];
}
if (!fr) ++sum[now];
else {
int tmp = (l < now - ed + fr) + (l <= now - ed + fr);
ans = 1ll * ans * tmp % mod;
}
}
void solve() {
rep(i, 1, n) {
if (deg[i]) continue;
int x = i, len = 0; while (!cir[x]) ++len, x = a[x];
L[x] = len;
}
ans = 1;
rep(i, 1, n) if (cir[i]) calc(i);
// 计算每种长度的简单环的方案
rep(i, 1, n) {
f[0] = 1;
rep(j, 1, sum[i]) {
if (i > 1 && (i & 1)) f[j] = f[j - 1] * 2 % mod;
else f[j] = f[j - 1];
if (j > 1) f[j] = (f[j] + f[j - 2] * (j - 1) % mod * i % mod) % mod;
}
ans = 1ll * ans * f[sum[i]] % mod;
}
}
int main() {
cin >> n;
rep(i, 1, n) scanf("%d", &a[i]), ++deg[a[i]];
rep(i, 1, n) {
if (vis[i]) continue;
int x = i;
while (!vis[x]) vis[x] = i, x = a[x];
if (vis[x] != i) continue;
while (!cir[x]) cir[x] = 1, x = a[x];
}
rep(i, 1, n)
if ((!cir[i] && deg[i] > 1) || (cir[i] && deg[i] > 2)) return puts("0"), 0;
solve();
printf("%lld\n", ans);
return 0;
}
| CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define X first
#define Y second
#define REP(i,a) for(int i=0;i<a;++i)
#define REPP(i,a,b) for(int i=a;i<b;++i)
#define FILL(a,x) memset(a,x,sizeof(a))
#define foreach( gg,itit ) for( typeof(gg.begin()) itit=gg.begin();itit!=gg.end();itit++ )
#define mp make_pair
#define pb push_back
#define all(s) s.begin(),s.end()
#define present(c,x) ((c).find(x) != (c).end())
const double EPS = 1e-8;
const int mod = 1e9+7;
const int N = 1e6+10;
const ll INF = 1e18;
//#define DEBUG
ll power(ll x,ll y){
ll t=1;
while(y>0){
if(y%2) y-=1,t=t*x%mod;
else y/=2,x=x*x%mod;
}
return t;
}
#ifdef DEBUG
#define dprintf(fmt,...) fprintf(stderr,fmt,__VA_ARGS__)
#else
#define dprintf(fmt,...)
#endif
int nx[N],u[N];
vector<int> G[N];
vector<int> cycles[N];
int ct[N],cy[N],ln[N];ll dp[N];
int main(){
int n; scanf("%d",&n);
REP(i,n){
scanf("%d",&nx[i]); nx[i]--;
}
int tl=0;
ll ans=1;
REP(i,n) if(u[i]==0){
u[i]=i+1;
int x=nx[i];
while(u[x]==0){
u[x]=i+1; x=nx[x];
}
if(u[x]==i+1){
// printf("%d\n",x+1);
cycles[tl].pb(x);
int t=nx[x]; cy[x]=1;
while(t!=x){
cy[t]=1; cycles[tl].pb(t); t=nx[t];
// printf("%d\n",t);
}
tl++;
}
}
// printf("h\n");
REP(i,n) if(cy[i]==0) G[nx[i]].pb(i);
REP(i,n) if(G[i].size()>1) ans=0;
if(ans==0) {printf("0\n"); exit(0);}
FILL(u,0);
REP(i,n) if(cy[i]){
ln[i]=0;
int c=i;
while(G[c].size()){
c=G[c][0]; ln[i]++;
}
}
// printf("lol\n");
REP(i,tl){
int fl=0;
REP(j,cycles[i].size()) if(G[cycles[i][j]].size()) fl=1;
if(fl==0) ct[cycles[i].size()]++;
else{
int cr = 0;
while(G[cycles[i][cr]].size()==0) cr++;
int tk=cr;
int lg=0,n=cycles[i].size();
while(lg<n){
int pv=(tk-1+n)%n,ex=1;
while(ln[cycles[i][pv]]==0) pv=(pv-1+n)%n,ex++;//,printf("%d %d\n",pv,);
if(ln[cycles[i][tk]]>ex) ans=0;
else if(ln[cycles[i][tk]] <ex) ans=ans*2%mod;
// printf("%d %d %d %lld\n",cycles[i][tk]+1,ln[cycles[i][tk]],ex,ans);
tk=pv; lg+=ex;
}
}
}
REP(i,n+1) if(ct[i]){
dp[0]=1;
// printf("%d %d\n",i,ct[i]);
REPP(j,1,ct[i]+1){
if(i%2&&i>1) dp[j]=dp[j-1]*2%mod;
else dp[j]=dp[j-1];
if(j>1) dp[j]=(dp[j]+dp[j-2]*(j-1)%mod*i%mod)%mod;
}
ans=ans*dp[ct[i]]%mod;
}
printf("%lld\n",ans);
return 0;
} | CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include <bits/stdc++.h>
typedef long long ll;
typedef long long llong;
typedef long double ld;
typedef unsigned long long ull;
using namespace std;
void ex() {
cout << 0 << "\n";
exit(0);
}
const ll MOD = 1e9 + 7;
ll pw(ll a, ll b) {
ll ans = 1; while (b) {
while (!(b & 1)) b >>= 1, a = (a * a) % MOD;
ans = (ans * a) % MOD, --b;
} return ans;
}
const int MAXN = 120000;
int was[MAXN];
int fl[MAXN];
vector<int> vv;
int go[MAXN * 2];
vector<int> beds[MAXN];
int a[MAXN];
int cc[MAXN];
ll fc[MAXN];
ll bfc[MAXN];
int n;
int tm1;
int lst[MAXN];
void dfs1(int v) {
if (was[v]) {
if (lst[v] == tm1)
fl[v] = 1;
return;
}
was[v] = 1;
lst[v] = tm1;
dfs1(a[v]);
}
void dfs2(int v) {
vv.push_back(v);
was[v] = 1;
if (!was[a[v]])
dfs2(a[v]);
}
int dfs3(int v) {
if (beds[v].size() >= 2)
ex();
int a = 1;
for (int u: beds[v])
a += dfs3(u);
return a;
}
int main() {
scanf("%d", &n);
fc[0] = bfc[0] = 1;
for (int i = 1; i <= n; ++i)
fc[i] = (fc[i - 1] * i) % MOD, bfc[i] = pw(fc[i], MOD - 2);
for (int i = 0; i < n; ++i) {
scanf("%d", a + i), --a[i];
beds[a[i]].push_back(i);
}
for (int i = 0; i < n; ++i) {
if (was[i])
continue;
++tm1;
dfs1(i);
}
memset(was, 0, sizeof(was));
ll ans = 1;
for (int i = 0; i < n; ++i) {
if (was[i] || !fl[i])
continue;
vv.clear();
dfs2(i);
int f = 0;
for (int i = 0; i < vv.size(); ++i) {
if (beds[vv[i]].size() > 2)
ex();
go[i] = 0;
for (int j: beds[vv[i]])
if (!was[j])
f = 1, go[i] = dfs3(j);
}
if (!f) {
++cc[vv.size()];
}
else {
for (int i = 0; i < vv.size(); ++i)
go[i + vv.size()] = go[i];
int lst = 0;
for (int i = 0; i < vv.size(); ++i)
if (go[i])
lst = i;
for (int i = vv.size(); i < 2 * vv.size(); ++i) {
if (!go[i])
continue;
if (i - lst < go[i])
ex();
if (i - lst > go[i])
ans = (ans * 2) % MOD;
lst = i;
}
}
}
ll B2 = pw(2, MOD - 2);
for (int i = 1; i <= n; ++i) {
if (!cc[i])
continue;
ll go = 0;
if (i != 1 && i % 2 == 1) {
for (int j = 0; j * 2 <= cc[i]; ++j) {
go = (go + pw(2, cc[i] - j * 2) * pw(i, j) % MOD * fc[cc[i]] % MOD * bfc[cc[i] - j * 2] % MOD * pw(B2, j) % MOD * bfc[j] % MOD) % MOD;
}
}
else {
for (int j = 0; j * 2 <= cc[i]; ++j) {
go = (go + pw(i, j) % MOD * fc[cc[i]] % MOD * bfc[cc[i] - j * 2] % MOD * pw(B2, j) % MOD * bfc[j] % MOD) % MOD;
}
}
ans = (ans * go) % MOD;
}
cout << ans << "\n";
return 0;
}
| CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
typedef long long i64;
const int MAXN = 100000 + 5;
const int MOD = 1e9 + 7;
int N;
i64 answer, dp[MAXN];
int degree[MAXN], lenCnt[MAXN];
int pre[MAXN], nxt[MAXN];
bool vis[MAXN];
bool noSolution = false;
void findCycle()
{
for (int i = 1; i <= N; i++)
{
if (degree[i] > 2)
return noSolution = true, (void) 0;
if (degree[i] != 2 || vis[i])
continue;
int cur = i;
do
{
if (vis[cur])
return noSolution = true, (void) 0;
vis[cur] = true;
pre[nxt[cur]] = cur;
cur = nxt[cur];
} while (cur != i);
}
}
void processCircleBasedTree()
{
for (int i = 1; i <= N; i++)
{
if (degree[i])
continue;
int cur = i, footLen = 0, cycleLen = 0;
while (!vis[cur])
vis[cur] = true,
cur = nxt[cur],
footLen++;
do
cur = pre[cur],
cycleLen++;
while (degree[cur] == 1);
if (footLen < cycleLen)
answer = answer * 2 % MOD;
if (footLen > cycleLen)
return noSolution = true, (void) 0;
}
}
void countCycle()
{
for (int i = 1; i <= N; i++)
{
if (vis[i])
continue;
int cur = i, len = 0;
do
{
vis[cur] = true;
cur = nxt[cur];
len++;
} while (cur != i);
lenCnt[len]++;
}
}
int main()
{
answer = 1;
ios::sync_with_stdio(false), cin.tie(NULL);
cin >> N;
for (int i = 1; i <= N; i++)
cin >> nxt[i], degree[nxt[i]]++;
findCycle();
if (noSolution)
return cout << 0 << endl, 0;
processCircleBasedTree();
if (noSolution)
return cout << 0 << endl, 0;
countCycle();
if (noSolution)
return cout << 0 << endl, 0;
for (int i = 1; i <= N; i++)
{
dp[0] = 1LL;
for (int j = 1; j <= lenCnt[i]; j++)
{
dp[j] = dp[j - 1] * (((i % 2 == 0) || i == 1) ? 1 : 2);
if (j >= 2)
dp[j] = (dp[j] + ((((dp[j - 2] * (j - 1)) % MOD) * i) % MOD))
% MOD;
}
answer = answer * dp[lenCnt[i]] % MOD;
}
cout << answer << endl;
}
| CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<bits/stdc++.h>
using namespace std;
#define LL long long
const int maxn=1e5+10;
const int mo=1e9+7;
int n,f[maxn],Ans=1,deg[maxn];
int feet[maxn],sz[maxn],dp[maxn];
bool vis[maxn],cir[maxn];
queue<int> q;
int main()
{
#ifdef h10
freopen("E.in","r",stdin);
freopen("E.out","w",stdout);
#endif
int i,j,u;
scanf("%d",&n);
for (i=1;i<=n;i++)
{
scanf("%d",&f[i]);
deg[f[i]]++;
}
for (i=1;i<=n;i++)
if (deg[i]>2) {puts("0"); return 0;}
for (i=1;i<=n;i++)
if (!deg[i]) q.push(i);
while (!q.empty())
{
u=q.front(); q.pop();
if (!(--deg[f[u]])) q.push(f[u]);
}
for (i=1;i<=n;i++) cir[i]=deg[i]!=0;
memset(deg,0,sizeof(deg));
for (i=1;i<=n;i++) deg[f[i]]++;
for (i=1;i<=n;i++)
if (!cir[i]&°[i]==2) {puts("0"); return 0;}
// for (i=1;i<=n;i++)
// printf("%d ",cir[i]);
// puts("");
for (i=1;i<=n;i++)
if (deg[i]==0)
{
int cnt=0;
for (u=i;!cir[u];u=f[u]) cnt++;
feet[u]=cnt;
// printf("feet[%d]=%d\n",u,cnt);
}
for (i=1;i<=n;i++)
if (cir[i]&&!vis[i])
{
int cnt=0,flag=1,pos=0;
for (u=i;;u=f[u])
{
vis[u]=1;
cnt++;
if (feet[u]) flag=0,pos=u;
if (f[u]==i) break;
}
if (flag) sz[cnt]++;
else
{
cnt=0;
for (u=f[pos];;u=f[u])
{
cnt++;
if (feet[u])
{
if (feet[u]<cnt) Ans=Ans*2%mo;
if (feet[u]>cnt) Ans=0;
cnt=0;
}
if (u==pos) break;
}
}
}
for (i=1;i<=n;i++)
if (sz[i])
{
// printf("sz[%d]=%d\n",i,sz[i]);
dp[0]=1;
for (j=1;j<=sz[i];j++)
{
if (i>1&&i&1) dp[j]=dp[j-1]*2%mo;
else dp[j]=dp[j-1];
if (j>1) (dp[j]+=(LL)dp[j-2]*(j-1)%mo*i%mo)%=mo;
}
// printf("dp[%d]=%d\n",sz[i],dp[sz[i]]);
Ans=(LL)Ans*dp[sz[i]]%mo;
}
printf("%d\n",Ans);
}
| CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<cstdio>
#include<cctype>
const int N=100007,P=1000000007;
int a[N],deg[N],vis[N],pre[N],cnt[N],f[N];
int read(){int x=0,c=getchar();while(isspace(c))c=getchar();while(isdigit(c))(x*=10)+=c&15,c=getchar();return x;}
int add(int a,int b){return a+=b-P,a+(a>>31&P);}
int mul(int a,int b){return 1ll*a*b%P;}
int main()
{
int n=read(),ans=1;
for(int i=1;i<=n;++i) ++deg[a[i]=read()];
for(int i=1;i<=n;++i)
{
if(deg[i]>2) return puts("0"),0;
if(deg[i]<2||vis[i]) continue;
//for(int p=i;!vis[p];vis[p]=1,pre[a[p]]=p,p=a[p]) if(vis[p]) return puts("0"),0;
int p=i;
do
{
if(vis[p]) { printf("0\n"); return 0; }
vis[p]=1,pre[a[p]]=p,p=a[p];
}while(p!=i);
}
for(int i=1,p,l1,l2;i<=n;++i)
if(!deg[i])
{
for(p=i,l1=0,l2=0;!vis[p];p=a[p]) vis[p]=1,++l1;
do ++l2,p=pre[p]; while(deg[p]^2);
if(l1<l2) ans=add(ans,ans); else if(l1>l2) return puts("0"),0;
}
for(int i=1;i<=n;++i)
if(!vis[i])
{
int p=i,l=0;
do ++l,p=a[p],vis[p]=1; while(p^i);
++cnt[l];
}
for(int i=1;i<=n;ans=mul(ans,f[cnt[i++]]))
{
f[0]=1,f[1]=1+(i^1&&i&1);
for(int j=2;j<=cnt[i];++j) f[j]=add(mul(i,mul(j-1,f[j-2])),mul(f[1],f[j-1]));
}
printf("%d",ans);
}
| CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<cstdio>
#define ll long long
#define mod 1000000007
#define N 110000
inline char gc(){
static char now[1<<16],*S,*T;
if (T==S){T=(S=now)+fread(now,1,1<<16,stdin);if (T==S) return EOF;}
return *S++;
}
inline int read(){
int x=0;char ch=gc();
while (ch<'0'||ch>'9') ch=gc();
while (ch<='9'&&ch>='0'){x=x*10+ch-'0';ch=gc();}
return x;
}
int a[N],in[N],color[N],mark[N],cnt[N],n,foot[N];bool circle[N];
ll dp[N];
int main(){
// freopen("agc.in","r",stdin);
n=read();ll ans=1;
for (int i=1;i<=n;++i) a[i]=read(),++in[a[i]];
for (int i=1;i<=n;++i){
if (color[i]) continue;int x=i;
for(;!color[x];x=a[x]) color[x]=i;
if (color[x]!=i) continue;
for (;!circle[x];x=a[x]) circle[x]=1;
}
for (int i=1;i<=n;++i) if ((circle[i]&&in[i]>2)||(!circle[i]&&in[i]>1)) {printf("0");return 0;}
//处理痞老板(基环内向树)
for (int i=1;i<=n;++i){
if (in[i]) continue;int x=i,tmp=0;
while(!circle[x]) x=a[x],tmp++;foot[x]=tmp;
}
for (int i=1;i<=n;++i){
if (!circle[i]) continue;int x=i,st=0,first=0,id=0,len=0;
while (circle[x]){
++id;circle[x]=0;
if (foot[x]){
if (!first){
st=first=id;len=foot[x];x=a[x];continue;
}else{
int numm=(foot[x]<(id-st))+(foot[x]<=(id-st));
(ans*=numm)%=mod;if (!ans) {printf("0");return 0;}st=id;x=a[x];continue;
}
}x=a[x];
}if (first){
int numm=(len<(id+first-st))+(len<=(id+first-st));
(ans*=numm)%=mod;if (!ans) {printf("0");return 0;}
}else cnt[id]++;
}
//for (int i=1;i<=n;++i) if (cnt[i]) printf("%d %d\n",i,cnt[i]);
for (int i=1;i<=n;++i){
dp[0]=1;if (!cnt[i]) continue;
if (i>1&&(i%2)){
for (int j=1;j<=cnt[i];++j) {
dp[j]=dp[j-1]*2%mod;if (j>1) (dp[j]+=dp[j-2]*(j-1)*i%mod)%=mod;
}
}else{
for (int j=1;j<=cnt[i];++j){
dp[j]=dp[j-1];if (j>1) (dp[j]+=dp[j-2]*(j-1)*i%mod)%=mod;
}
}
(ans*=dp[cnt[i]])%=mod;if (!ans){printf("0");return 0;}
}printf("%lld",ans);
return 0;
} | CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<cstdio>
#include<cctype>
#define RI register int
#define g (p1==p2&&(p2=(p1=buf)+fread(buf,1,size,stdin),p1==p2)?EOF:*p1++)
using namespace std;
typedef long long ll;
const int mod=1e9+7,N=1e5+10,size=N<<2;
char buf[size],*p1=buf,*p2=buf;
void qr(int &x) {
char c=g;x=0;
while(!isdigit(c))c=g;
while(isdigit(c))x=x*10+c-'0',c=g;
}
int n; ll ans;
int a[N],deg[N],vis[N],footL[N],sum[N],f[N];
bool cir[N];
inline int qm(int x) {return x>=mod?x-mod:x;}
void workcir(int x) {
int now=0,fr=0,frL=0,ed=0;
//now是当前环的长度,fr是第一个有脚点的位置,frL是位置对应的脚长,ed为上一个有脚点的位置。
while(cir[x]) {
++now; cir[x]=0;
if(footL[x]) {
if(!fr) ed=fr=now,frL=footL[x];
else {
ans=ans*((footL[x]<now-ed)+(footL[x]<=now-ed))%mod;//脚的放法
ed=now;
}
}
x=a[x];
}
if(!ed) ++sum[now];
else ans=ans*((frL<now-ed+fr)+(frL<=now-ed+fr))%mod;
}
void work() {
for(RI i=1;i<=n;i++) {//脚对环上交接点贡献
if(deg[i]) continue;
int x=i,len=0; while(!cir[x])x=a[x],++len;
footL[x]=len;//脚长
}
ans=f[0]=1;
for(RI i=1;i<=n;i++) if(cir[i]) workcir(i);//以环为基准计算基环内向树的情况
for(RI i=1;i<=n&&ans;i++) {//计算简单环
for(RI j=1;j<=sum[i];j++) {
if(i>1&&(i&1)) f[j]=qm(f[j-1]<<1);
else f[j]=f[j-1];
if(j>1) f[j]=qm(f[j]+(ll)f[j-2]*(j-1)%mod*i%mod);
}
ans=ans*f[sum[i]]%mod;
}
}
int main() {
qr(n);
for(RI i=1;i<=n;i++) qr(a[i]),++deg[a[i]];
for(RI i=1;i<=n;i++) if(!vis[i]) {
int x=i;while(!vis[x]) vis[x]=i,x=a[x];
if(vis[x]!=i) continue;//脚
while(!cir[x]) cir[x]=1,x=a[x];//标记环上点
}
for(RI i=1;i<=n;i++)
if((cir[i]&°[i]>2)||(!cir[i]&°[i]>1)) return puts("0"),0;//开叉就不行
work(); printf("%lld\n",ans); return 0;
} | CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<bits/stdc++.h>
#define ll long long
#define mod 1000000007
#define N 100009
using namespace std;
int n,a[N],fst[N],nxt[N],dgr[N],vis[N],num[N],f[N];
int main(){
scanf("%d",&n);
int i,j,k,sz,tmp,rst;
for (i=1; i<=n; i++){
scanf("%d",&a[i]);
nxt[i]=fst[a[i]]; fst[a[i]]=i; dgr[a[i]]++;
}
int ans=1; bool flag;
for (i=1; i<=n; i++) if (!vis[i]){
for (j=i; !vis[j]; j=a[j]) vis[j]=1;
sz=0; flag=1;
for (; vis[j]!=2; j=a[j]){
sz++; vis[j]=2;
if (dgr[j]>2){ puts("0"); return 0; }
else if (dgr[j]>1) flag=0;
}
if (flag){
num[sz]++; continue;
}
for (; dgr[j]==1; j=a[j]); j=a[j];
for (rst=0; vis[j]!=3; j=a[j]){
vis[j]=3; rst++;
for (k=fst[j]; k; k=nxt[k]) if (vis[k]<2){
for (tmp=1; dgr[k]==1; k=fst[k]){
vis[k]=1; tmp++;
}
vis[k]=1;
if (dgr[k] || tmp>rst){ puts("0"); return 0; }
if (rst>tmp) ans=(ans<<1)%mod;
rst=0;
}
}
}
for (i=1; i<=n; i++){
k=i>1 && (i&1)?2:1;
for (j=f[0]=1; j<=num[i]; j++){
f[j]=f[j-1]*k%mod;
if (j>1) f[j]=(f[j]+(ll)f[j-2]*(j-1)%mod*i)%mod;
}
ans=(ll)ans*f[num[i]]%mod;
}
printf("%d\n",ans);
return 0;
} | CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1e5;
const int mod=1e9+7;
int n,cnt,ans=1;
int a[maxn+8],deg[maxn+8],vis[maxn+8],cir[maxn+8],l1[maxn+8],l2[maxn+8],sum[maxn+8];
int f[maxn+8];
bool tree[maxn+8];
int read()
{
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
return x*f;
}
void solve(int x)
{
int siz_line=0;
while(!cir[x]) siz_line++,x=a[x];
l1[x]=siz_line;
tree[cir[x]]=1;
siz_line=1,x=a[x];
while(deg[x]!=2) siz_line++,x=a[x];
l2[x]=siz_line;
}
int main()
{
n=read();
for (int i=1;i<=n;i++) a[i]=read(),deg[a[i]]++;
for (int i=1;i<=n;i++)
{
if (vis[i]) continue;
int x=i;
while(!vis[x]) vis[x]=i,x=a[x];
if (vis[x]!=i) continue;
++cnt;
while(!cir[x]) cir[x]=cnt,x=a[x];
}
memset(vis,0,sizeof(vis));
for (int i=1;i<=n;i++) if (deg[i]>(1+(cir[i]>0))) {puts("0");return 0;}
for (int i=1;i<=n;i++) if (!deg[i]) solve(i);
for (int i=1;i<=n;i++)
if (l1[i]) ans=1ll*ans*((l2[i]>=l1[i])+(l2[i]>l1[i]))%mod;
for (int i=1;i<=n;i++)
{
if (!cir[i]) continue;
if (tree[cir[i]]) continue;
if (vis[i]) continue;
int x=i,siz=0;while(!vis[x]) siz++,vis[x]=1,x=a[x];
sum[siz]++;
}
for (int i=1;i<=n;i++)
{
if (!sum[i]) continue;
//printf("%d %d\n",i,sum[i]);
f[0]=1;
for (int j=1;j<=sum[i];j++)
{
f[j]=f[j-1];
if (i>1&&(i&1)) f[j]=(f[j]+f[j-1])%mod;
if (j>1) f[j]=(f[j]+1ll*f[j-2]*(j-1)%mod*i%mod)%mod;
}
ans=1ll*ans*f[sum[i]]%mod;
}
printf("%d\n",ans);
return 0;
}
| CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<cstdio>
#include<cstring>
#include<cstdlib>
using namespace std;
int p1[110000],p2[110000];
int a[110000];
int lb[110000];
inline void jio(){puts("0");exit(0);}
bool lowb[110000];
long long ff[110000];
int ljrose[110000];
long long renleizhihui[110000];
int main()
{
int n;scanf("%d",&n);
memset(p1,0,sizeof(p1));
memset(p2,0,sizeof(p2));
int lben=0;
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
if(a[i]<1||a[i]>n)jio();
if(a[i]==i)lb[++lben]=i;
if(p1[a[i]]==0)p1[a[i]]=i;
else if(p2[a[i]]==0)p2[a[i]]=i;
else jio();
}
int ss=0;
memset(lowb,false,sizeof(lowb));
for(int i=1;i<=lben;i++)
{
lowb[lb[i]]=true;
if(p1[lb[i]]!=lb[i])p1[lb[i]]^=p2[lb[i]]^=p1[lb[i]]^=p2[lb[i]];
if(p2[lb[i]]!=0)lowb[p2[lb[i]]]=true;
if(p2[lb[i]]==0)ss++;
else if(p1[p2[lb[i]]]!=0)jio();
}
long long lbsl=1;
for(int i=1;i<=n;i++)
{
int xx=0,yy=0;
long long s=2;
if(p2[i]!=0&&lowb[i]==false)
{
lowb[i]=true;
xx=p1[i];yy=p2[i];
lowb[xx]=lowb[yy]=true;
while(true)
{
if(yy>0)
{
int xr=xx,yr=yy;
if(p2[xr]!=0&&p1[yr]!=0)jio();
if(p1[xr]!=0&&p2[yr]!=0)jio();
if(p1[xr]!=0&&p1[yr]!=0)xx=p1[xr],yy=p1[yr];
else if(p2[xr]!=0)xx=p1[xr],yy=p2[xr];
else if(p2[yr]!=0)xx=p1[yr],yy=p2[yr];
else if(p1[xr]!=0)xx=p1[xr],yy=0;
else if(p1[yr]!=0)xx=p1[yr],yy=0;
else xx=yy=0;
}
else
{
int xr=xx;
if(p2[xr]!=0)xx=p1[xr],yy=p2[xr],s=s*2%1000000007;
else if(p1[xr]!=0)xx=p1[xr],yy=0;
else xx=yy=0;
}
lowb[xx]=lowb[yy]=true;
if(xx==0&&yy==0)jio();
if(xx==i||yy==i)
{
if(yy==i)xx^=yy^=xx^=yy;
if(yy==0)break;
if(p1[yy]!=0)jio();
else
{
if(s%2==0)s=s/2;
else s=(s+1000000007)/2;
break;
}
}
}
lbsl=lbsl*s%1000000007;
}
}
ff[0]=1;ff[1]=1;ff[2]=2;
for(int i=3;i<=n;i++)ff[i]=(ff[i-1]+ff[i-2]*(i-1)%1000000007)%1000000007;
memset(ljrose,0,sizeof(ljrose));
for(int i=1;i<=n;i++)
{
if(lowb[i]==false)
{
long long sr=1;int j=a[i];
lowb[i]=true;
while(j!=i)lowb[j]=true,j=a[j],sr++;
ljrose[sr]++;
}
}
for(int i=2;i<=n;i++)
{
if(i%2==1)
{
renleizhihui[0]=1;renleizhihui[1]=2;renleizhihui[2]=4+i;
for(int j=3;j<=ljrose[i];j++)renleizhihui[j]=(renleizhihui[j-1]*2+renleizhihui[j-2]*(j-1)%1000000007*i%1000000007)%1000000007;
lbsl=lbsl*renleizhihui[ljrose[i]]%1000000007;
continue;
}
renleizhihui[0]=1;renleizhihui[1]=1;renleizhihui[2]=1+i;
for(int j=3;j<=ljrose[i];j++)renleizhihui[j]=(renleizhihui[j-1]+renleizhihui[j-2]*(j-1)%1000000007*i%1000000007)%1000000007;
lbsl=lbsl*renleizhihui[ljrose[i]]%1000000007;
}
lbsl=lbsl*ff[ss]%1000000007;
printf("%lld\n",lbsl);
return 0;
} | CPP |
p03842 AtCoder Grand Contest 008 - Next or Nextnext | You are given an integer sequence a of length N. How many permutations p of the integers 1 through N satisfy the following condition?
* For each 1 ≤ i ≤ N, at least one of the following holds: p_i = a_i and p_{p_i} = a_i.
Find the count modulo 10^9 + 7.
Constraints
* 1 ≤ N ≤ 10^5
* a_i is an integer.
* 1 ≤ a_i ≤ N
Input
The input is given from Standard Input in the following format:
N
a_1 a_2 ... a_N
Output
Print the number of the permutations p that satisfy the condition, modulo 10^9 + 7.
Examples
Input
3
1 2 3
Output
4
Input
2
1 1
Output
1
Input
3
2 1 1
Output
2
Input
3
1 1 1
Output
0
Input
13
2 1 4 3 6 7 5 9 10 8 8 9 11
Output
6 | 6 | 0 | #include<cstdio>
#include<cctype>
const int N=100007,P=1000000007;
int a[N],deg[N],vis[N],pre[N],cnt[N],f[N];
int read(){int x=0,c=getchar();while(isspace(c))c=getchar();while(isdigit(c))(x*=10)+=c&15,c=getchar();return x;}
int add(int a,int b){return a+=b-P,a+(a>>31&P);}
int mul(int a,int b){return 1ll*a*b%P;}
int main()
{
int n=read(),ans=1;
for(int i=1;i<=n;++i) ++deg[a[i]=read()];
for(int i=1,p;(p=i)<=n;++i)
{
if(deg[i]>2) return puts("0"),0;
if(deg[i]<2||vis[i]) continue;
do
{
if(vis[p]) return puts("0"),0;
vis[p]=1,pre[a[p]]=p,p=a[p];
}while(p^i);
}
for(int i=1,p,l1,l2;i<=n;++i)
if(!deg[i])
{
for(p=i,l1=0,l2=0;!vis[p];p=a[p]) vis[p]=1,++l1;
do ++l2,p=pre[p]; while(deg[p]^2);
if(l1<l2) ans=add(ans,ans); else if(l1>l2) return puts("0"),0;
}
for(int i=1;i<=n;++i)
if(!vis[i])
{
int p=i,l=0;
do ++l,p=a[p],vis[p]=1; while(p^i);
++cnt[l];
}
for(int i=1;i<=n;ans=mul(ans,f[cnt[i++]]))
{
f[0]=1,f[1]=1+(i^1&&i&1);
for(int j=2;j<=cnt[i];++j) f[j]=add(mul(i,mul(j-1,f[j-2])),mul(f[1],f[j-1]));
}
printf("%d",ans);
}
| CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
#define si short int
#define MAX 101
si f[MAX][MAX][MAX][MAX],s[MAX][MAX];
si sum(int a,int b,int c,int d)
{
if(a>c)swap(a,c);if(b>d)swap(b,d);
return s[c][d]-s[a-1][d]-s[c][b-1]+s[a-1][b-1];
}
si n,m,X,Y,ans;
char g[MAX][MAX];
void upd(si &x,si y){if(x<y)x=y;}
int main()
{
cin>>n>>m;
for(int i=1;i<=n;++i)scanf("%s",g[i]+1);
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
if(g[i][j]=='E')X=i,Y=j;
else if(g[i][j]=='o')s[i][j]=1;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
s[i][j]+=s[i-1][j]+s[i][j-1]-s[i-1][j-1];
for(int a=X;a;--a)
for(int b=Y;b;--b)
for(int c=X;c<=n;++c)
for(int d=Y;d<=m;++d)
{
ans=max(ans,f[a][b][c][d]);
int L=d-Y+1,R=m-Y+b,U=c-X+1,D=n-X+a;
if(a>U)upd(f[a-1][b][c][d],f[a][b][c][d]+sum(a-1,max(b,L),a-1,min(d,R)));
if(b>L)upd(f[a][b-1][c][d],f[a][b][c][d]+sum(max(a,U),b-1,min(c,D),b-1));
if(c<D)upd(f[a][b][c+1][d],f[a][b][c][d]+sum(c+1,max(b,L),c+1,min(d,R)));
if(d<R)upd(f[a][b][c][d+1],f[a][b][c][d]+sum(max(a,U),d+1,min(c,D),d+1));
}
cout<<ans<<endl;
return 0;
}
| CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<bits/stdc++.h>
#define up(x,y) (x<(y)?x=(y):0)
#define N 109
using namespace std;
int m,n,ans,tx,ty,dp[2][N][N][N],s[N][N];
char ch[N][N];
void ins(int i,int j,int k,int l,int v){
up(ans,v);
if (i<=j && k<=l) up(dp[i&1][j][k][l],v);
}
int S(int x1,int y1,int x2,int y2){
if (x1>x2 || y1>y2) return 0;
return s[x2][y2]-s[x2][y1-1]-s[x1-1][y2]+s[x1-1][y1-1];
}
int main(){
scanf("%d%d",&m,&n);
int i,j,k,l,u,d,x,y,tmp;
for (i=1; i<=m; i++){
scanf("%s",ch[i]+1);
for (j=1; j<=n; j++){
if (ch[i][j]=='E'){
tx=i; ty=j;
}
s[i][j]=s[i-1][j]+s[i][j-1]-s[i-1][j-1]+(ch[i][j]=='o');
}
}
for (i=1; i<=m; i++){
memset(dp[!(i&1)],0,sizeof(dp[i&1]));
for (j=m; j>=i; j--)
for (k=1; k<=n; k++)
for (l=n; l>=k; l--){
tmp=dp[i&1][j][k][l];
u=max(1,tx-(m-j)); d=min(m,tx+(i-1));
x=max(1,ty-(n-l)); y=min(n,ty+(k-1));
if (u<=i && i<=d)
ins(i+1,j,k,l,tmp+S(i,max(k,x),i,min(l,y)));
if (u<=j && j<=d)
ins(i,j-1,k,l,tmp+S(j,max(k,x),j,min(l,y)));
if (x<=k && k<=y)
ins(i,j,k+1,l,tmp+S(max(i,u),k,min(j,d),k));
if (x<=l && l<=y)
ins(i,j,k,l-1,tmp+S(max(i,u),l,min(j,d),l));
}
}
printf("%d\n",ans);
return 0;
}
| CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
using namespace std;
char a[105][105];
int f[2][105][105][105],line[105][105],list[105][105],n,m;
void upd(int &x,int w){
x=(x<w)?w:x;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%s",a[i]+1);
int x,y;
for(int i=1;i<=n;i++){
for(int p=1;p<=m;p++){
if(a[i][p]=='E') x=i,y=p;
line[i][p]=line[i][p-1]+(a[i][p]=='o');
list[i][p]=list[i-1][p]+(a[i][p]=='o');
}
}
int pre=0,nxt=1,ans=0;
for(int i=0;i<=n-x;i++,pre^=1,nxt^=1){
memset(f[nxt],0,sizeof(f[nxt]));
for(int p=0;p<x;p++){
for(int j=0;j<=m-y;j++){
for(int k=0;k<y;k++){
int w=f[pre][p][j][k],W;
if(x+i+1<=n-p) W=max(0,line[x+i+1][min(m-k,y+j)]-line[x+i+1][max(j,y-k-1)]);
else W=0;
upd(f[nxt][p][j][k],w+W);
if(x-p-1>i) W=max(0,line[x-p-1][min(m-k,y+j)]-line[x-p-1][max(j,y-k-1)]);
else W=0;
upd(f[pre][p+1][j][k],w+W);
if(y+j+1<=m-k) W=max(0,list[min(n-p,x+i)][y+j+1]-list[max(i,x-p-1)][y+j+1]);
else W=0;
upd(f[pre][p][j+1][k],w+W);
if(y-k-1>j) W=max(0,list[min(n-p,x+i)][y-k-1]-list[max(i,x-p-1)][y-k-1]);
else W=0;
upd(f[pre][p][j][k+1],w+W);
ans=max(ans,f[pre][p][j][k]);
}
}
}
}
printf("%d",ans);
return 0;
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
#define maxn 102
int n, m, XX, YY, ans;
int a[maxn][maxn], b[maxn][maxn];
short f[maxn][maxn][maxn][maxn];
void Getmax(int &x, int y) { if(y > x) x = y; }
bool Mid(int K, int l, int r) { return (K >= l) && (K <= r); }
int main()
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i ++)
for(int j = 1; j <= m; j ++)
{
char c; cin >> c; int tem = 0;
if(c == 'o') tem = 1;
else if(c == 'E') XX = i, YY = j;
a[i][j] = a[i][j - 1] + tem;
b[j][i] = b[j][i - 1] + tem;
}
for(int u = 0; u < XX; u ++)
for(int d = 0; d <= n - XX; d ++)
for(int l = 0; l < YY; l ++)
for(int r = 0; r <= m - YY; r ++)
{
int ret = 0, tem1 = XX - u, tem2 = XX + d;
int tem3 = YY - l, tem4 = YY + r;
int p1 = min(YY + r, m - l), p2 = max(YY - l, r + 1); // line
int q1 = min(XX + d, n - u), q2 = max(XX - u, d + 1); // row
if(u) Getmax(ret, f[u - 1][d][l][r] + ((Mid(tem1, q2, q1)) ? max(a[tem1][p1] - a[tem1][p2 - 1], 0) : 0));
if(d) Getmax(ret, f[u][d - 1][l][r] + ((Mid(tem2, q2, q1)) ? max(a[tem2][p1] - a[tem2][p2 - 1], 0) : 0));
if(l) Getmax(ret, f[u][d][l - 1][r] + ((Mid(tem3, p2, p1)) ? max(b[tem3][q1] - b[tem3][q2 - 1], 0) : 0));
if(r) Getmax(ret, f[u][d][l][r - 1] + ((Mid(tem4, p2, p1)) ? max(b[tem4][q1] - b[tem4][q2 - 1], 0) : 0));
f[u][d][l][r] = ret;
ans = max(ans, (int) f[u][d][l][r]);
}
printf("%d\n", ans);
return 0;
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<iostream>
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<stack>
#define gc getchar()
#define N 103
#define sint short
#define upd(x,y) (x=max(x,(short)(y)))
#define debug(x) cerr<<#x<<"="<<x
#define sp <<" "
#define ln <<endl
using namespace std;
inline int inn()
{
int x,ch;while((ch=gc)<'0'||ch>'9');
x=ch^'0';while((ch=gc)>='0'&&ch<='9')
x=(x<<1)+(x<<3)+(ch^'0');return x;
}
sint s[N][N],dp[N][N][N][N];char str[N][N];
inline sint S(int a,int b,int c,int d)
{
if(a>c) swap(a,c);if(b>d) swap(b,d);
return (short)(s[c][d]+s[a-1][b-1]-s[a-1][d]-s[c][b-1]);
}
int main()
{
int n=inn(),m=inn(),x=0,y=0;sint ans=0;
for(int i=1;i<=n;i++) scanf("%s",str[i]+1);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(str[i][j]=='E') x=i,y=j;
else s[i][j]=(str[i][j]=='o');
for(int i=1;i<=n;i++) for(int j=1;j<=m;j++)
s[i][j]=(short)(s[i][j]+s[i-1][j]+s[i][j-1]-s[i-1][j-1]);
for(int a=x;a;a--) for(int b=y;b;b--)
for(int c=x;c<=n;c++) for(int d=y;d<=m;d++)
{
ans=max(ans,dp[a][b][c][d]);int L=d-y+1,R=m-y+b,U=c-x+1,D=n-x+a;
if(a>U) upd(dp[a-1][b][c][d],dp[a][b][c][d]+S(a-1,max(b,L),a-1,min(d,R)));
if(b>L) upd(dp[a][b-1][c][d],dp[a][b][c][d]+S(max(a,U),b-1,min(c,D),b-1));
if(c<D) upd(dp[a][b][c+1][d],dp[a][b][c][d]+S(c+1,max(b,L),c+1,min(d,R)));
if(d<R) upd(dp[a][b][c][d+1],dp[a][b][c][d]+S(max(a,U),d+1,min(c,D),d+1));
}
return !printf("%d\n",(int)ans);
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<bits/stdc++.h>
using namespace std;
const int N = 105;
int n, m, ex, ey;
char s[N][N], a[N][N];
short ans, dp[N][N][N][N], sum[N][N];
int Sum(int U, int L, int D, int R) {
if (U > D) swap(U, D);
if (L > R) swap(L, R);
return sum[D][R] + sum[U - 1][L - 1] - sum[D][L - 1] - sum[U - 1][R];
}
void Upd(short &x, short y) {
x = max(x, y);
}
int main() {
cin >> n >> m;
for (int i = 1; i <= n; ++i) {
scanf("%s", s[i] + 1);
for (int j = 1; j <= m; ++j) {
if (s[i][j] == 'E') {
ex = i;
ey = j;
}
if (s[i][j] == 'o') sum[i][j] = 1;
sum[i][j] -= sum[i - 1][j - 1];
sum[i][j] += sum[i][j - 1] + sum[i - 1][j];
}
}
for (short a = ex; a >= 1; --a) {
for (short c = ey; c >= 1; --c) {
for (short b = ex; b <= n; ++b) {
for (short d = ey; d <= m; ++d) {
short U = b - ex + 1, D = n - ex + a;
short L = d - ey + 1, R = m - ey + c;
short now = dp[a][b][c][d];
Upd(ans, now);
if (a > U)
Upd(dp[a - 1][b][c][d], now + Sum(a - 1, max(L, c), a - 1, min(R, d)));
if (b < D)
Upd(dp[a][b + 1][c][d], now + Sum(b + 1, max(L, c), b + 1, min(R, d)));
if (c > L)
Upd(dp[a][b][c - 1][d], now + Sum(max(U, a), c - 1, min(D, b), c - 1));
if (d < R)
Upd(dp[a][b][c][d + 1], now + Sum(max(U, a), d + 1, min(D, b), d + 1));
}
}
}
}
cout << ans << endl;
return 0;
}
| CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstdlib>
#include<cstring>
using namespace std;
const int maxn=1e2+5;
int n,m,dp[2][maxn][maxn][maxn],ex,ey,sum[maxn][maxn],l,r,u,d,ans;
char s[maxn][maxn];
inline int max_(int a,int b){
return a>=b?a:b;
}
inline int min_(int a,int b){
return a<=b?a:b;
}
inline void chkmax(int&a,int b){
if(a<b)a=b;
}
inline int calc(int a,int b,int c,int d){
return sum[c][d]-sum[c][b-1]-sum[a-1][d]+sum[a-1][b-1];
}
int main(){
cin>>n>>m;
for(int i=1;i<=n;++i)
cin>>(s[i]+1);
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
if(s[i][j]=='o')
sum[i][j]=1;
else if(s[i][j]=='E')
ex=i,ey=j;
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j)
sum[i][j]=sum[i][j]+sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
u=ex-1;d=n-ex;l=ey-1;r=m-ey;
for(int i=ex;i;--i){
memset(dp[i&1^1],0,sizeof(dp[i&1^1]));
for(int j=ex;j<=n;++j)
for(int x=ey;x;--x)
for(int y=ey;y<=m;++y){
if(i>1&&j-i<u)
chkmax(dp[i&1^1][j][x][y],dp[i&1][j][x][y]+calc(i-1,max_(x,y-ey+1),i-1,min_(y,m-ey+x)));
if(j<n&&j-i<d)
chkmax(dp[i&1][j+1][x][y],dp[i&1][j][x][y]+calc(j+1,max_(x,y-ey+1),j+1,min_(y,m-ey+x)));
if(x>1&&y-x<l)
chkmax(dp[i&1][j][x-1][y],dp[i&1][j][x][y]+calc(max_(i,j-ex+1),x-1,min_(j,n-ex+i),x-1));
if(y<m&&y-x<r)
chkmax(dp[i&1][j][x][y+1],dp[i&1][j][x][y]+calc(max_(i,j-ex+1),y+1,min_(j,n-ex+i),y+1));
chkmax(ans,dp[i&1][j][x][y]);
}
}
printf("%d\n",ans);
return 0;
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 100
using namespace std;
char Map[MAXN+5][MAXN+5];
int n,m,ex,ey;
int sumr[MAXN+5][MAXN+5],sumc[MAXN+5][MAXN+5];
int dp[MAXN+5][MAXN+5][MAXN+5];
void Init()
{
memset(dp,-1,sizeof(dp));
}
int main()
{
Init();
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%s",Map[i]+1);
for(int j=1;j<=m;j++)
{
sumr[i][j]=sumr[i][j-1];
sumc[i][j]=sumc[i-1][j];
if(Map[i][j]=='E')
ex=i,ey=j;
else if(Map[i][j]=='o')
{
sumr[i][j]++;
sumc[i][j]++;
}
}
}
int L,R,U,D,ans=0;
L=ey-1;
R=m-ey;
U=ex-1;
D=n-ex;
dp[0][0][0]=0;
for(int l=0;l<=L;l++)
for(int r=0;r<=R;r++)
for(int u=0;u<=U;u++)
for(int d=0;d<=D;d++)
{
if(dp[r][u][d]==-1)
continue;
ans=max(dp[r][u][d],ans);
int up=max(ex-u,d+1);
int down=min(ex+d,n-u);
int left=max(ey-l,r+1);
int right=min(ey+r,m-l);
if(up>down||left>right)
continue;
int add;
if(u<U-d)
{
add=sumr[ex-u-1][right]-sumr[ex-u-1][left-1];
dp[r][u+1][d]=max(dp[r][u+1][d],dp[r][u][d]+add);
}
if(d<D-u)
{
add=sumr[ex+d+1][right]-sumr[ex+d+1][left-1];
dp[r][u][d+1]=max(dp[r][u][d+1],dp[r][u][d]+add);
}
if(r<R-l)
{
add=sumc[down][ey+r+1]-sumc[up-1][ey+r+1];
dp[r+1][u][d]=max(dp[r+1][u][d],dp[r][u][d]+add);
}
if(l<L-r)
{
add=sumc[down][ey-l-1]-sumc[up-1][ey-l-1];
dp[r][u][d]=max(dp[r][u][d],dp[r][u][d]+add);
}
}
printf("%d\n",ans);
return 0;
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
#include<vector>
using namespace std;
#define LL long long
#define DB double
#define MAXN 100
#define MOD 1000000007
#define Pr pair<int,int>
#define X first
#define Y second
#define INF 1000000000000000000
#define mem(x,v) memset(x,v,sizeof(x))
LL read(){
LL x=0,F=1;char c=getchar();
while(c<'0'||c>'9'){if(c=='-')F=-1;c=getchar();}
while(c>='0'&&c<='9'){x=x*10+c-'0';c=getchar();}
return x*F;
}
int add(int a,int b){return (a+b>=MOD)?a+b-MOD:a+b;}
int dec(int a,int b){return (a-b<0)?a-b+MOD:a-b;}
int mul(int a,int b){return (1LL*a*b)%MOD;}
int fst_pow(int a,int b){
int res=1;
while(b){
if(b&1)res=mul(res,a);
a=mul(a,a),b>>=1;
}
return res;
}
int n,m;
int sx,sy;
char s[MAXN+5][MAXN+5];
short dp[MAXN+5][MAXN+5][MAXN+5][MAXN+5],a[MAXN+5][MAXN+5],b[MAXN+5][MAXN+5];
void upd(short &x,short y){x=max(x,y);}
int main(){
n=read(),m=read();
for(int i=1;i<=n;i++){
scanf("%s",s[i]+1);
for(int j=1;j<=m;j++){
if(s[i][j]=='E')sx=i,sy=j;
a[i][j]=a[i][j-1]+(s[i][j]=='o');
b[i][j]=b[i-1][j]+(s[i][j]=='o');
}
}
int L=sy-1,U=sx-1,R=m-sy,D=n-sx;
for(int u=0;u<=U;u++)
for(int d=0;d<=D;d++)
for(int l=0;l<=L;l++)
for(int r=0;r<=R;r++){
int pl=max(sy-l,r+1),pr=min(sy+r,m-l);
upd(dp[u+1][d][l][r],dp[u][d][l][r]+((sx-u-1>=d+1&&pl<=pr)?a[sx-u-1][pr]-a[sx-u-1][pl-1]:0));
upd(dp[u][d+1][l][r],dp[u][d][l][r]+((sx+d+1<=n-u&&pl<=pr)?a[sx+d+1][pr]-a[sx+d+1][pl-1]:0));
pl=max(sx-u,d+1),pr=min(sx+d,n-u);
upd(dp[u][d][l+1][r],dp[u][d][l][r]+((sy-l-1>=r+1&&pl<=pr)?b[pr][sy-l-1]-b[pl-1][sy-l-1]:0));
upd(dp[u][d][l][r+1],dp[u][d][l][r]+((sy+r+1<=m-l&&pl<=pr)?b[pr][sy+r+1]-b[pl-1][sy+r+1]:0));
}
printf("%d\n",dp[U][D][L][R]);
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
char s[111][111];
int dp[2][111][111][111];
int sum1[111][111],sum2[111][111];
int query1(int i,int l,int r)
{
return sum1[i][r]-sum1[i][l-1];
}
int query2(int i,int l,int r)
{
return sum2[r][i]-sum2[l-1][i];
}
int main()
{
int n,m,sx,sy;
int ans=0;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%s",s[i]+1);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++)
if(s[i][j]=='E')
{
sx=i;
sy=j;
}
for(int i=1;i<=n;i++)
{
sum1[i][0]=0;
for(int j=1;j<=m;j++)
sum1[i][j]=sum1[i][j-1]+(s[i][j]=='o');
}
for(int j=1;j<=m;j++)
{
sum2[0][j]=0;
for(int i=1;i<=n;i++)
sum2[i][j]=sum2[i-1][j]+(s[i][j]=='o');
}
int now=0,to=1;
memset(dp,0,sizeof(dp));
for(int l=0;l<sy;l++)
{
for(int r=0;r<=m-sy;r++)
for(int u=0;u<sx;u++)
for(int d=0;d<=n-sx;d++)
{
ans=max(ans,dp[now][r][u][d]);
if(sy-l-1>r)
dp[to][r][u][d]=max(dp[to][r][u][d],dp[now][r][u][d]+query2(sy-l-1,max(sx-u,d+1),min(sx+d,n-u)));
if(sy+r+1<=m-l)
dp[now][r+1][u][d]=max(dp[now][r+1][u][d],dp[now][r][u][d]+query2(sy+r+1,max(sx-u,d+1),min(sx+d,n-u)));
if(sx-u-1>d)
dp[now][r][u+1][d]=max(dp[now][r][u+1][d],dp[now][r][u][d]+query1(sx-u-1,max(sy-l,r+1),min(sy+r,m-l)));
if(sx+d+1<=n-u)
dp[now][r][u][d+1]=max(dp[now][r][u][d+1],dp[now][r][u][d]+query1(sx+d+1,max(sy-l,r+1),min(sy+r,m-l)));
dp[now][r][u][d]=0;
}
swap(now,to);
}
printf("%d",ans);
return 0;
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
typedef long long ll;
const int N=105;
char s[N][N];
int sumr[N][N];
int sumc[N][N];
int n,m,ex,ey;
short dp[N][N][N][N];
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%s",s[i]+1);
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++){
sumr[i][j]=sumr[i][j-1]+(s[i][j]=='o');
sumc[i][j]=sumc[i-1][j]+(s[i][j]=='o');
if(s[i][j]=='E')
ex=i,ey=j;
}
int L=ey-1,R=m-ey,U=ex-1,D=n-ex;
int ans=0;
memset(dp,-1,sizeof(dp));
dp[0][0][0][0]=0;
for(int l=0;l<=L;l++)
for(int r=0;r<=R;r++)
for(int u=0;u<=U;u++)
for(int d=0;d<=D;d++)
{
int tmp=dp[l][r][u][d];
if(tmp==-1) continue;
ans=max(tmp,ans);
int up=max(ex-u,d+1),dn=min(ex+d,n-u),le=max(ey-l,r+1),ri=min(ey+r,m-l);
if(up>dn||le>ri) continue;
if(u<U-d) dp[l][r][u+1][d]=max((int)dp[l][r][u+1][d],tmp+sumr[ex-u-1][ri]-sumr[ex-u-1][le-1]);
if(d<D-u) dp[l][r][u][d+1]=max((int)dp[l][r][u][d+1],tmp+sumr[ex+d+1][ri]-sumr[ex+d+1][le-1]);
if(r<R-l) dp[l][r+1][u][d]=max((int)dp[l][r+1][u][d],tmp+sumc[dn][ey+r+1]-sumc[up-1][ey+r+1]);
if(l<L-r) dp[l+1][r][u][d]=max((int)dp[l+1][r][u][d],tmp+sumc[dn][ey-l-1]-sumc[up-1][ey-l-1]);
}
printf("%d\n",ans);
}
| CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<stdio.h>
#include<vector>
#include<algorithm>
using namespace std;
#define get(A,B,C,D) (rui[B][D]-rui[A-1][D]-rui[B][C-1]+rui[A-1][C-1])
short dp[102][102][102][102];
int map[100][100];
short rui[101][101];
int main()
{
int mx,my;
scanf("%d%d",&mx,&my);
int sx,sy;
for(int i=0;i<mx;i++)
{
for(int j=0;j<my;j++)
{
char z;
scanf(" %c",&z);
if(z=='o')map[i][j]=1;
if(z=='E')sx=i,sy=j;
}
}
for(int i=0;i<mx;i++)for(int j=0;j<my;j++)rui[i+1][j+1]=rui[i+1][j]+rui[i][j+1]-rui[i][j]+map[i][j];
short maxi=0;
for(int i=1;i<=mx;i++)
{
for(int j=mx;j>=i-1;j--)
{
for(int k=1;k<=my;k++)
{
for(int l=my;l>=k-1;l--)
{
short t=dp[i][j][k][l];
maxi=max(maxi,t);
if(i>j||k>l)continue;
int si=1+sx-(mx-j);
int sj=1+sx+(i-1);
int sk=1+sy-(my-l);
int sl=1+sy+(k-1);
dp[i+1][j][k][l]=max(dp[i+1][j][k][l],t);
dp[i][j-1][k][l]=max(dp[i][j-1][k][l],t);
dp[i][j][k+1][l]=max(dp[i][j][k+1][l],t);
dp[i][j][k][l-1]=max(dp[i][j][k][l-1],t);
if(si<=i)dp[i+1][j][k][l]=max(dp[i+1][j][k][l],short(t+get(i,i,max(sk,k),min(sl,l))));
if(sj>=j)dp[i][j-1][k][l]=max(dp[i][j-1][k][l],short(t+get(j,j,max(sk,k),min(sl,l))));
if(sk<=k)dp[i][j][k+1][l]=max(dp[i][j][k+1][l],short(t+get(max(si,i),min(sj,j),k,k)));
if(sl>=l)dp[i][j][k][l-1]=max(dp[i][j][k][l-1],short(t+get(max(si,i),min(sj,j),l,l)));
}
}
}
}
printf("%d\n",maxi);
}
| CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
const int mxN=100;
int h, w, ei, ej, a1[mxN][mxN+1], a2[mxN+1][mxN];
short dp[mxN][mxN][mxN][mxN];
string s[mxN];
void ud(short &x, short y) {
x=max(x, y);
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cin >> h >> w;
for(int i=0; i<h; ++i) {
cin >> s[i];
for(int j=0; j<w; ++j) {
if(s[i][j]=='E')
ei=i, ej=j;
a1[i][j+1]=a1[i][j]+(s[i][j]=='o');
a2[i+1][j]=a2[i][j]+(s[i][j]=='o');
}
}
memset(dp, 0xc0, sizeof(dp));
dp[ei][ei][ej][ej]=0;
for(int i1=ei; ~i1; --i1) {
for(int i2=ei; i2<h; ++i2) {
for(int j1=ej; ~j1; --j1) {
for(int j2=ej; j2<w; ++j2) {
int i3=max(i2-ei, i1), i4=min(h-1+i1-ei, i2)+1, j3=max(j2-ej, j1), j4=min(w-1+j1-ej, j2)+1;
if(i1)
ud(dp[i1-1][i2][j1][j2], dp[i1][i2][j1][j2]+(i1>i2-ei&&j3<j4?a1[i1-1][j4]-a1[i1-1][j3]:0));
if(i2<h-1)
ud(dp[i1][i2+1][j1][j2], dp[i1][i2][j1][j2]+(i2<h-1+i1-ei&&j3<j4?a1[i2+1][j4]-a1[i2+1][j3]:0));
if(j1)
ud(dp[i1][i2][j1-1][j2], dp[i1][i2][j1][j2]+(j1>j2-ej&&i3<i4?a2[i4][j1-1]-a2[i3][j1-1]:0));
if(j2<w-1)
ud(dp[i1][i2][j1][j2+1], dp[i1][i2][j1][j2]+(j2<w-1+j1-ej&&i3<i4?a2[i4][j2+1]-a2[i3][j2+1]:0));
}
}
}
}
cout << dp[0][h-1][0][w-1];
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<bits/stdc++.h>
#define N 110
using namespace std;
char s[N][N];
int sum[N][N],h,w,eh,ew;
int dp[N*N/4][N*N/4];
inline int rg(int l,int r,int d,int u){
if(l>r||d>u) return 0;
return sum[r][u]-sum[l-1][u]-sum[r][d-1]+sum[l-1][d-1];
}
int solve(int ch,int cw){
if(!ch&&!cw) return 0;
if(dp[ch][cw]>=0) return dp[ch][cw];
int l=ch%eh,r=ch/eh,d=cw%ew,u=cw/ew;
if(l){
dp[ch][cw]=max(dp[ch][cw],solve(ch-1,cw)+ (eh-l>r?rg(eh-l,eh-l,max(1+u,ew-d),min(w-d,ew+u)):0) );
}
if(r){
dp[ch][cw]=max(dp[ch][cw],solve(ch-eh,cw)+ (eh+r<=h-l?rg(eh+r,eh+r,max(1+u,ew-d),min(w-d,ew+u)):0) );
}
if(d){
dp[ch][cw]=max(dp[ch][cw],solve(ch,cw-1)+ (ew-d>u?rg(max(1+r,eh-l),min(h-l,eh+r),ew-d,ew-d):0) );
}
if(u){
dp[ch][cw]=max(dp[ch][cw],solve(ch,cw-ew)+ (ew+u<=w-d?rg(max(1+r,eh-l),min(h-l,eh+r),ew+u,ew+u):0) );
}
return dp[ch][cw];
}
int main(){
scanf("%d%d",&h,&w);
for(int i=1;i<=h;i++){
scanf("%s",s[i]+1);
for(int j=1;j<=w;j++){
sum[i][j]=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
if(s[i][j]=='o') sum[i][j]++;
else if(s[i][j]=='E') eh=i,ew=j;
}
}
memset(dp,-1,sizeof(dp));
printf("%d\n",solve(eh*(h-eh)+eh-1,ew*(w-ew)+ew-1));
return 0;
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<cstdio>
#include<iostream>
#include<cstring>
#define RG register
#define si short int
using namespace std;
const int N=101;
si n,m,x,y,ans,dp[N][N][N][N],sum[N][N];
char g[N][N];
template<typename I> inline void read(I &ot){
I ch=getchar(), x=0, f=1;
while(ch<'0' || ch>'9'){if(ch=='-') f=-1; ch=getchar(); }
while(ch>='0' && ch<='9'){x=x*10+ch-'0'; ch=getchar(); }
ot=x*f;}
template<typename I, typename... U> inline void read(I &x,U&... y){read(x); read(y...);}
template<typename I>inline I mx(const I&a,const I&b){return a>b ? a : b;}
template<typename I>inline I mi(const I&a,const I&b){return a<b ? a : b;}
inline void cmx(si &a,si b){if(b>a) a=b;}
template<typename I>inline void swp(I&a,I&b){I t=a; a=b; b=t;}
inline si s(int a,int b,int c,int d){
if(a>c) swp(a,c); if(b>d) swp(b,d);
return sum[c][d]-sum[c][b-1]-sum[a-1][d]+sum[a-1][b-1];
}
int main()
{
// freopen("Salvage Robots.in","r",stdin);
//freopen(".out","w",stdout);
read(n,m);
for(int i=1;i<=n;i++)
{
scanf("%s",g[i]+1);
for(int j=1;j<=m;j++)
{
if(g[i][j]=='o') sum[i][j]=1;
else if(g[i][j]=='E') x=i, y=j;
sum[i][j]+=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
}
}
for(int i=x;i;i--)
for(int j=y;j;j--)
for(int k=x;k<=n;k++)
for(int l=y;l<=m;l++)
{
ans=mx(ans,dp[i][j][k][l]);
// int ri=m-l+y, li=y-j+1, us=x-i+1, ds=n-k+x;
int li=l-y+1, ri=m-y+j, us=k-x+1, ds=n-x+i;
if(i-1>=us) cmx(dp[i-1][j][k][l],dp[i][j][k][l]+s(i-1,mx(li,j),i-1,mi(ri,l)));
if(j-1>=li) cmx(dp[i][j-1][k][l],dp[i][j][k][l]+s(mx(i,us),j-1,mi(k,ds),j-1));
if(k+1<=ds) cmx(dp[i][j][k+1][l],dp[i][j][k][l]+s(k+1,mx(li,j),k+1,mi(ri,l)));
if(l+1<=ri) cmx(dp[i][j][k][l+1],dp[i][j][k][l]+s(mx(i,us),l+1,mi(k,ds),l+1));
}
// printf("%d\n",ans);
cout<<ans<<endl;
//fclose(stdin); fclose(stdout);
return 0;
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include <cstdio>
#include <algorithm>
int N, M, X, Y, a[100][100], sx[100][100], sy[100][100], f[100][100][100][100];
inline int SY(int x, int l, int r)
{
int L = std::max(l, r - Y), R = std::min(r, M - 1 - Y + l);
return L > R ? 0 : sy[x][R] - (L ? sy[x][L - 1] : 0);
}
inline int SX(int y, int l, int r)
{
int L = std::max(l, r - X), R = std::min(r, N - 1 - X + l);
return L > R ? 0 : sx[R][y] - (L ? sx[L - 1][y] : 0);
}
int main()
{
scanf("%d%d", &N, &M);
for (int i = 0; i < N; i++)
{
static char _i[101];
scanf("%s", _i);
for (int j = 0; j < M; j++)
if (_i[j] == 'E')
X = i, Y = j;
else if (_i[j] == 'o')
a[i][j] = 1;
}
for (int i = 0; i < N; i++)
std::copy(a[i], a[i] + M, sx[i]);
for (int i = 1; i < N; i++)
for (int j = 0; j < M; j++)
sx[i][j] += sx[i - 1][j];
for (int i = 0; i < N; i++)
std::copy(a[i], a[i] + M, sy[i]);
for (int i = 0; i < N; i++)
for (int j = 1; j < M; j++)
sy[i][j] += sy[i][j - 1];
for (int i = 0; i <= X; i++)
for (int j = N - 1; j >= X; j--)
for (int k = 0; k <= Y; k++)
for (int l = M - 1; l >= Y; l--)
{
if (i > j - X)
f[i][j][k][l] = std::max(f[i][j][k][l], f[i - 1][j][k][l] + SY(i - 1, k, l));
if (j < N - 1 - X + i)
f[i][j][k][l] = std::max(f[i][j][k][l], f[i][j + 1][k][l] + SY(j + 1, k, l));
if (k > l - Y)
f[i][j][k][l] = std::max(f[i][j][k][l], f[i][j][k - 1][l] + SX(k - 1, i, j));
if (l < M - 1 - Y + k)
f[i][j][k][l] = std::max(f[i][j][k][l], f[i][j][k][l + 1] + SX(l + 1, i, j));
}
printf("%d\n", f[X][X][Y][Y]);
return 0;
}
| CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
const int maxn = 105;
short dp[maxn][maxn][maxn][maxn];
short row[maxn][maxn], col[maxn][maxn];
char mp[maxn][maxn];
void cmax(short &a, short b){
if(a < b) a = b;
}
void __main(){
int n, m, x, y;
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++){
scanf("%s", mp[i] + 1);
for(int j = 1; j <= m; j++){
if(mp[i][j] == 'E'){
x = i;
y = j;
}
}
}
for(int i = 1; i <= n; i++){
for(int j = 1; j <= m; j++){
row[i][j] = row[i][j - 1] + (mp[i][j] == 'o');
col[i][j] = col[i - 1][j] + (mp[i][j] == 'o');
}
}
int L = y - 1, R = m - y, U = x - 1, D = n - x;
memset(dp, -1, sizeof dp);
dp[0][0][0][0] = 0;
short ans = 0;
for(int l = 0; l <= L; l++){
for(int u = 0; u <= U; u++){
for(int r = 0; r <= R; r++){
for(int d = 0; d <= D; d++) if(dp[l][u][r][d] != -1){
cmax(ans, dp[l][u][r][d]);
int tx1 = min(u, U - d), tx2 = min(d, D - u);
int ty1 = min(l, L - r), ty2 = min(r, R - l);
if(l + 1 <= L && l + r < L){
cmax(dp[l + 1][u][r][d], dp[l][u][r][d] + col[x + tx2][y - l - 1] - col[x - tx1 - 1][y - l - 1]);
}
if(u + 1 <= U && u + d < U){
cmax(dp[l][u + 1][r][d], dp[l][u][r][d] + row[x - u - 1][y + ty2] - row[x - u - 1][y - ty1 - 1]);
}
if(r + 1 <= R && l + r < R){
cmax(dp[l][u][r + 1][d], dp[l][u][r][d] + col[x + tx2][y + r + 1] - col[x - tx1 - 1][y + r + 1]);
}
if(d + 1 <= D && u + d < D){
cmax(dp[l][u][r][d + 1], dp[l][u][r][d] + row[x + d + 1][y + ty2] - row[x + d + 1][y - ty1 - 1]);
}
}
}
}
}
printf("%d\n", (int)ans);
}
int main(){
__main();
return 0;
}
/*
3 4
...o
...o
Eooo
3 3
o.o
.Eo
ooo
2 2
E.
..
3 4
o...
o...
oooE
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
*/
| CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
#define N 100
int n, m, ex, ey, sum1[N + 5][N + 5], sum2[N + 5][N + 5], ans;
char s[N + 5][N + 5];
vector<vector<vector<vector<int> > > > f;
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%s", s[i] + 1);
for (int j = 1; j <= m; ++j)
if (s[i][j] == 'E')
ex = i, ey = j;
}
f.resize(ex);
for (int a = 0; a < ex; ++a) {
f[a].resize(n - ex + 1);
for (int b = 0; b <= n - ex; ++b) {
f[a][b].resize(ey);
for (int c = 0; c < ey; ++c)
f[a][b][c].resize(m - ey + 1);
}
}
for (int i = 1; i <= n; ++i)
for (int j = 1; j <= m; ++j) {
sum1[i][j] = sum1[i][j - 1] + (s[i][j] == 'o');
sum2[i][j] = sum2[i - 1][j] + (s[i][j] == 'o');
}
for (int a = 0; a < ex; a++)
for (int b = 0; b <= n - ex; b++)
for (int c = 0; c < ey; c++)
for (int d = 0; d <= m - ey; d++) {
int U = b + 1, D = n - a, L = d + 1, R = m - c;
ans = max(ans, f[a][b][c][d]);
if (U < ex - a)
f[a + 1][b][c][d] = max(f[a + 1][b][c][d], f[a][b][c][d] + sum1[ex - a - 1][min(R, ey + d)] - sum1[ex - a - 1][max(L, ey - c) - 1]);
if (D > ex + b)
f[a][b + 1][c][d] = max(f[a][b + 1][c][d], f[a][b][c][d] + sum1[ex + b + 1][min(R, ey + d)] - sum1[ex + b + 1][max(L, ey - c) - 1]);
if (L < ey - c)
f[a][b][c + 1][d] = max(f[a][b][c + 1][d], f[a][b][c][d] + sum2[min(D, ex + b)][ey - c - 1] - sum2[max(U, ex - a) - 1][ey - c - 1]);
if (R > ey + d)
f[a][b][c][d + 1] = max(f[a][b][c][d + 1], f[a][b][c][d] + sum2[min(D, ex + b)][ey + d + 1] - sum2[max(U, ex - a) - 1][ey + d + 1]);
}
printf("%d\n", ans);
return 0;
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<cstdio>
#include<algorithm>
using namespace std;
#define MN 100
char s[MN+5][MN+5];
short R[MN+5][MN+5],C[MN+5][MN+5],f[MN+5][MN+5][MN+5][MN+5],ans;
void rw(short&a,short b){if(b>a)a=b;}
int qr(int x,int l,int r){return l<=r?R[x][r]-R[x][l-1]:0;}
int qc(int x,int l,int r){return l<=r?C[r][x]-C[l-1][x]:0;}
int main()
{
int n,m,i,j,x,y,a,b,c,d;
scanf("%d%d",&n,&m);
for(i=1;i<=n;++i)
{
scanf("%s",s[i]+1);
for(j=1;j<=m;++j)
{
if(s[i][j]=='E')x=i,y=j;
R[i][j]=R[i][j-1]+(s[i][j]=='o');
C[i][j]=C[i-1][j]+(s[i][j]=='o');
}
}
for(a=0;x-a;++a)for(b=0;x+b<=n;++b)for(c=0;y-c;++c)for(d=0;y+d<=m;++d)
{
if(x-a-1)rw(f[a+1][b][c][d],f[a][b][c][d]+(x-a-1>b)*qr(x-a-1,max(y-c,d+1),min(y+d,m-c)));
if(x+b+1<=n)rw(f[a][b+1][c][d],f[a][b][c][d]+(x+b+1<n-a+1)*qr(x+b+1,max(y-c,d+1),min(y+d,m-c)));
if(y-c-1)rw(f[a][b][c+1][d],f[a][b][c][d]+(y-c-1>d)*qc(y-c-1,max(x-a,b+1),min(x+b,n-a)));
if(y+d+1<=m)rw(f[a][b][c][d+1],f[a][b][c][d]+(y+d+1<m-c+1)*qc(y+d+1,max(x-a,b+1),min(x+b,n-a)));
rw(ans,f[a][b][c][d]);
}
printf("%d",ans);
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<cstdio>
#include<cstring>
#include<algorithm>
#define N 110
using namespace std;
int n, m, f[2][N][N][N], s1[N][N], s2[N][N], x, y, now, now1, l1, r1, l2, r2;
char S[N], map[N][N];
int main(){
scanf("%d%d", &n, &m);
for(int i=1; i<=n; i++){
scanf("%s", S+1);
for(int j=1; j<=m; j++)map[i][j]=S[j];
}
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++)if(map[i][j]=='E'){x=i; y=j;}
memset(s1, 0, sizeof(s1)); memset(s2, 0, sizeof(s2));
for(int i=1; i<=n; i++)
for(int j=1; j<=m; j++){s1[i][j]=s1[i][j-1]+(map[i][j]=='o'?1:0); s2[i][j]=s2[i-1][j]+(map[i][j]=='o'?1:0);}
memset(f, 0, sizeof(f)); now=0;
for(int i=1; i<=n; i++){
now1=now; now^=1;
memset(f[now], 0, sizeof(f[now]));
for(int j=i; j; j--){
l1=max(x-(n-i), j); r1=min(x+(j-1), i);
for(int i1=1; i1<=m; i1++)
for(int j1=i1; j1; j1--){
l2=max(y-(m-i1), j1); r2=min(y+(j1-1), i1);
if(l1==j)f[now][j][j1][i1]=max(f[now][j][j1][i1], f[now][j+1][j1][i1]+s1[j][r2]-s1[j][l2-1]);
else f[now][j][j1][i1]=max(f[now][j][j1][i1], f[now][j+1][j1][i1]);
if(r1==i)f[now][j][j1][i1]=max(f[now][j][j1][i1], f[now1][j][j1][i1]+s1[i][r2]-s1[i][l2-1]);
else f[now][j][j1][i1]=max(f[now][j][j1][i1], f[now1][j][j1][i1]);
if(l2==j1)f[now][j][j1][i1]=max(f[now][j][j1][i1], f[now][j][j1+1][i1]+s2[r1][j1]-s2[l1-1][j1]);
else f[now][j][j1][i1]=max(f[now][j][j1][i1], f[now][j][j1+1][i1]);
if(r2==i1)f[now][j][j1][i1]=max(f[now][j][j1][i1], f[now][j][j1][i1-1]+s2[r1][i1]-s2[l1-1][i1]);
else f[now][j][j1][i1]=max(f[now][j][j1][i1], f[now][j][j1][i1-1]);
}
}
}
printf("%d", f[now][1][1][m]);
return 0;
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<bits/stdc++.h>
using namespace std;
const int N=102;
int gi() {
int x=0,o=1;char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
if(ch=='-') o=-1,ch=getchar();
while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
return x*o;
}
int n,m,x,y,ans=0,sx[N][N],sy[N][N];
short f[N][N][N][N];
int main() {
cin>>n>>m;
for(int i=1;i<=n;i++)
for(int j=1;j<=m;j++) {
char ch=getchar();
while(ch!='.'&&ch!='o'&&ch!='E') ch=getchar();
sx[i][j]=sy[i][j]=(ch=='o');
sx[i][j]+=sx[i][j-1];sy[i][j]+=sy[i-1][j];
if(ch=='E') x=i,y=j;
}
for(int i=x;i;i--)
for(int j=y;j;j--)
for(int k=x;k<=n;k++)
for(int l=y;l<=m;l++) {
ans=max(ans,(int)f[i][j][k][l]);
if(i>1&&k+1<x+i) f[i-1][j][k][l]=max((int)f[i-1][j][k][l],f[i][j][k][l]+max(0,sx[i-1][min(l,m-y+j)]-sx[i-1][max(j-1,l-y)]));
if(k<n&&x+k<n+i) f[i][j][k+1][l]=max((int)f[i][j][k+1][l],f[i][j][k][l]+max(0,sx[k+1][min(l,m-y+j)]-sx[k+1][max(j-1,l-y)]));
if(j>1&&l+1<y+j) f[i][j-1][k][l]=max((int)f[i][j-1][k][l],f[i][j][k][l]+max(0,sy[min(k,n-x+i)][j-1]-sy[max(i-1,k-x)][j-1]));
if(l<m&&y+l<m+j) f[i][j][k][l+1]=max((int)f[i][j][k][l+1],f[i][j][k][l]+max(0,sy[min(k,n-x+i)][l+1]-sy[max(i-1,k-x)][l+1]));
}
printf("%d\n",ans);
return 0;
}
//orzgzy
//鸡贼明年进队超稳
| CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #pragma GCC optimize("Ofast")
#pragma GCC optimize("inline")
#include <bits/stdc++.h>
#define y1 __zzd001
using namespace std;
const int N=105;
int n,m,ax,ay,bx,by;
char s[N][N];
short g[N][N],dp[N][N][N][N];
short Get(int x1,int y1,int x2,int y2){
return g[x2][y2]-g[x2][y1-1]-g[x1-1][y2]+g[x1-1][y1-1];
}
short max(short a,short b){
return a>b?a:b;
}
short DP(int x1,int y1,int x2,int y2){
// printf("DP %d %d %d %d\n",(int)x1,(int)y1,(int)x2,(int)y2);
short &v=dp[x1][y1][x2][y2];
if (~v)
return v;
if (x1>x2||y1>y2)
return v=0;
short xL=max(x2-bx+1,x1),xR=min(x1+ax-1,x2);
short yL=max(y2-by+1,y1),yR=min(y1+ay-1,y2);
return v=max(
max(DP(x1,y1,x2,y2-1)+((yR==y2)?Get(xL,y2,xR,y2):0),
DP(x1,y1+1,x2,y2)+((yL==y1)?Get(xL,y1,xR,y1):0)),
max(DP(x1,y1,x2-1,y2)+((xR==x2)?Get(x2,yL,x2,yR):0),
DP(x1+1,y1,x2,y2)+((xL==x1)?Get(x1,yL,x1,yR):0)));
}
int main(){
memset(g,0,sizeof g);
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
scanf("%s",s[i]+1);
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
if (s[i][j]=='o')
g[i][j]++;
else if (s[i][j]=='E')
ax=i,bx=n-i+1,ay=j,by=m-j+1;
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
g[i][j]=g[i][j]+g[i][j-1]+g[i-1][j]-g[i-1][j-1];
memset(dp,-1,sizeof dp);
printf("%d",(int)DP(1,1,n,m));
return 0;
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<queue>
#include<cstdio>
#include<algorithm>
using namespace std;
#define N 102
#define rep(i,j,k) for(i=j;i<=k;++i)
struct node{
short a,b,c,d;
}t,nt;
short temp,ans,n,m,i,j,k,p,A,B,C,D,vt,ex,ey;
short sum[N][N],mp[N][N];
short f[N][N][N][N];
queue<node> q;
short getsum(short x_1,short x_2,short y_1,short y_2){
if(x_1>x_2) return 0;
if(y_1>y_2) return 0;
return sum[x_2][y_2]+sum[x_1-1][y_1-1]-sum[x_1-1][y_2]-sum[x_2][y_1-1];
}
void chkmax(short &a,short b){
if(a==-1) q.push(nt);
if(a<b) a=b;
}
void work(){
rep(i,1,n){
temp=0;
rep(j,1,m){
temp+=mp[i][j];
sum[i][j]=sum[i-1][j]+temp;
}
}
rep(i,1,n)
rep(j,i,n)
rep(k,1,m)
rep(p,k,m)
f[i][j][k][p]=-1;
f[ex][ex][ey][ey]=0;
t=(node){ex,ex,ey,ey};
q.push(t);
while(!q.empty()){
t=q.front(); q.pop(); vt=f[t.a][t.b][t.c][t.d];
A=t.b-ex+1; B=n-(ex-t.a); C=t.d-ey+1; D=m-(ey-t.c);
nt=t;
if(t.b+1<=B){
nt.b++;
chkmax(f[nt.a][nt.b][nt.c][nt.d],vt+getsum(nt.b,nt.b,max(nt.c,C),min(nt.d,D)));
nt.b--;
}
if(t.d+1<=D){
nt.d++;
chkmax(f[nt.a][nt.b][nt.c][nt.d],vt+getsum(max(nt.a,A),min(nt.b,B),nt.d,nt.d));
nt.d--;
}
if(t.a-1>=A){
nt.a--;
chkmax(f[nt.a][nt.b][nt.c][nt.d],vt+getsum(nt.a,nt.a,max(nt.c,C),min(nt.d,D)));
nt.a++;
}
if(t.c-1>=C){
nt.c--;
chkmax(f[nt.a][nt.b][nt.c][nt.d],vt+getsum(max(nt.a,A),min(nt.b,B),nt.c,nt.c));
nt.c++;
}
chkmax(ans,vt);
}
}
void read(short &p){
p=0; char x=getchar();
while(x<'0' || x>'9') x=getchar();
while(x>='0'&&x<='9'){
p=p*10+x-'0';
x=getchar();
}
}
int main(){
read(n); read(m);
rep(i,1,n){
j=0;
char x=getchar();
while((x!='.')&&(x!='E')&&(x!='o')) x=getchar();
while((x=='.')||(x=='E')||(x=='o')){
j++;
if(x=='E') ex=i,ey=j;
else if(x=='o') mp[i][j]=1;
x=getchar();
}
}
work();
printf("%d\n",(int)ans);
return 0;
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
const int N=101;
char s[N];
short g[N][N],f[N][N],v[N][N][N][N];
inline void upd(short &x,short y) {x=max(x,y);}
int main() {
int n,m,x,y,ans=0;
cin>>n>>m;
for(int i=1;i<=n;i++) {
scanf("%s",s+1);
for(int j=1;j<=m;j++) {
if(s[j]=='E') x=i,y=j;
int t=(s[j]=='o');
f[i][j]=f[i][j-1]+t,g[i][j]=g[i-1][j]+t;
}
}
memset(v,-1,sizeof(v));
v[0][0][0][0]=0;
for(int i=0;i<x;i++)
for(int j=0;x+j<=n;j++)
for(int p=0;p<y;p++)
for(int q=0;y+q<=m;q++) {
int d=v[i][j][p][q];
if(d==-1) continue;
ans=max(ans,d);
if(j+1<x-i) upd(v[i+1][j][p][q],d+f[x-i-1][min(y+q,m-p)]-f[x-i-1][max(y-p,q+1)-1]);
if(x+j<n-i) upd(v[i][j+1][p][q],d+f[x+j+1][min(y+q,m-p)]-f[x+j+1][max(y-p,q+1)-1]);
if(q+1<y-p) upd(v[i][j][p+1][q],d+g[min(x+j,n-i)][y-p-1]-g[max(x-i,j+1)-1][y-p-1]);
if(y+q<m-p) upd(v[i][j][p][q+1],d+g[min(x+j,n-i)][y+q+1]-g[max(x-i,j+1)-1][y+q+1]);
}
cout<<ans;
return 0;
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#define rep(i,a,b) for(i=a;i<=b;i++)
using namespace std;
const int N=110;
int dp[N][N];int ans;
int s1[2][N][N],s2[N][N];int a[N][N];char s[N];
int px,py;int n,m;
void init(){
int i,j;
rep(i,1,n) rep(j,1,m) s1[0][i][j]=s1[0][i][j-1]+a[i][j];
rep(i,1,n) rep(j,1,m) s1[1][i][j]=s1[1][i-1][j]+a[i][j];
rep(i,1,n) rep(j,1,m) s2[i][j]=s2[i-1][j]+s1[0][i][j];
}
void calc(int x,int y){
int i,j,tx,ty,lx,ly;
memset(dp,0,sizeof(dp));
tx=px+x-(n-px);ty=py+y-(m-py);
dp[tx][ty]=s2[px+x][py+y]-s2[px+x][ty-1]-s2[tx-1][py+y]+s2[tx-1][ty-1];
lx=n-px;ly=m-py;
for(i=tx;i>=x+1;i--){
for(j=ty;j>=y+1;j--){
if(i<tx) dp[i][j]=max(dp[i][j],dp[i+1][j]+s1[0][i][j+ly]-s1[0][i][j-1]);
if(j<ty) dp[i][j]=max(dp[i][j],dp[i][j+1]+s1[1][i+lx][j]-s1[1][i-1][j]);
//if(x==1&&y==0) cerr<<i<<' '<<j<<' '<<dp[i][j]<<endl;
}
}
//cerr<<x<<' '<<y<<' '<<dp[x+1][y+1]<<endl;
ans=max(ans,dp[x+1][y+1]);
}
void work(){
int i,j;
rep(i,0,px-1) rep(j,0,py-1){
if(px+i>n||py+j>m) continue;
calc(i,j);
}
}
int main(){
//freopen("a.in","r",stdin);
//freopen("a.out","w",stdout);
int i,j;
scanf("%d%d",&n,&m);
rep(i,1,n){
scanf("%s",s+1);
rep(j,1,m){
if(s[j]=='.') a[i][j]=0;
else if(s[j]=='o') a[i][j]=1;
else px=i,py=j;
}
}
if(px-1<n-px){
rep(i,1,n) rep(j,1,m) if(i<n-i+1) swap(a[i][j],a[n-i+1][j]);
px=n-px+1;
}
if(py-1<m-py){
rep(i,1,n) rep(j,1,m) if(j<m-j+1) swap(a[i][j],a[i][m-j+1]);
py=m-py+1;
}
/*cerr<<px<<' '<<py<<endl;
rep(i,1,n){
rep(j,1,m) cerr<<a[i][j];
cerr<<endl;
}*/
init();work();
printf("%d\n",ans);
}
| CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
int n,m,x,y,s1[105][105],s2[105][105],f[105][105][105],ans;
char a[105][105];
void upd(int &x,int y){if (x<y) x=y;}
void work()
{
scanf("%d %d\n",&n,&m);
for (int i=1; i<=n; i++)
{
scanf("%s\n",a[i]+1);
for (int j=1; j<=m; j++)
if (a[i][j]=='E')
x=i,y=j;
}
for (int i=1; i<=n; i++)
for (int j=1; j<=m; j++)
if (a[i][j]=='o')
s1[i][j]=s1[i][j-1]+1,s2[i][j]=s2[i-1][j]+1;
else s1[i][j]=s1[i][j-1],s2[i][j]=s2[i-1][j];
for (int l=0; l<=y; l++)
for (int u=0; u<=x; u++)
for (int r=0; r<=m-y; r++)
for (int d=0; d<=n-x; d++)
{
upd(ans,f[u][r][d]);
if (d<n-x)
{
int L=max(r+1,y-l),R=min(m-l,y+r);
if (x+d+1<=n-u)
upd(f[u][r][d+1],f[u][r][d]+s1[x+d+1][R]-s1[x+d+1][L-1]);
else upd(f[u][r][d+1],f[u][r][d]);
}
if (r<m-y)
{
int L=max(d+1,x-u),R=min(n-u,x+d);
if (y+r+1<=m-l)
upd(f[u][r+1][d],f[u][r][d]+s2[R][y+r+1]-s2[L-1][y+r+1]);
else upd(f[u][r+1][d],f[u][r][d]);
}
if (u<x)
{
int L=max(r+1,y-l),R=min(m-l,y+r);
if (x-u-1>d)
upd(f[u+1][r][d],f[u][r][d]+s1[x-u-1][R]-s1[x-u-1][L-1]);
else upd(f[u+1][r][d],f[u][r][d]);
}
if (l<y)
{
int L=max(d+1,x-u),R=min(n-u,x+d);
if (y-l-1>r) f[u][r][d]+=s2[R][y-l-1]-s2[L-1][y-l-1];
}
}
printf("%d",ans);
}
int main()
{
work();
return 0;
}
| CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<cstring>
#include<cstdlib>
#include<cstdio>
#include<cmath>
#include<iostream>
#define N 101
using namespace std;
short n,m,f[N][N][N][N],sx,sy,h[N][N],L[N][N];
char s[N][N];
void upd(short &x,short y)
{
if(x<y) x=y;
}
int main()
{
cin>>n>>m;
for(short i=1;i<=n;i++)
{
scanf("%s",s[i]+1);
for(short j=1;j<=m;j++) if(s[i][j]=='E') sx=i,sy=j;
}
for(short i=1;i<=n;i++)
for(short j=1;j<=m;j++)
{
L[i][j]=L[i-1][j];
h[i][j]=h[i][j-1];
if(s[i][j]=='o') h[i][j]++,L[i][j]++;
}
short ln=m-sy,rn=sy-1,un=n-sx,dn=sx-1;
for(short i=0;i<=ln;i++)
for(short j=0;j<=rn;j++)
for(short x=0;x<=un;x++)
for(short y=0;y<=dn;y++)
{
short t,now,l,r;
if(i<ln)
{
now=sy+i+1;t=0;
if(now+j<=m)
{
l=max(sx-y,x+1);r=min(sx+x,n-y);
if(l<=r) t=L[r][now]-L[l-1][now];
}
upd(f[i+1][j][x][y],f[i][j][x][y]+t);
}
if(j<rn)
{
now=sy-j-1;t=0;
if(now-i>=1)
{
l=max(sx-y,x+1);r=min(sx+x,n-y);
if(l<=r) t=L[r][now]-L[l-1][now];
}
upd(f[i][j+1][x][y],f[i][j][x][y]+t);
}
if(i==1 && j==0 && x==0 && y==1)
{short oo=1;}
if(x<un)
{
now=sx+x+1;t=0;
if(now+y<=n)
{
l=max(sy-j,i+1);r=min(sy+i,m-j);
if(l<=r) t=h[now][r]-h[now][l-1];
}
upd(f[i][j][x+1][y],f[i][j][x][y]+t);
}
if(y<dn)
{
now=sx-y-1;t=0;
if(now-x>=1)
{
l=max(sy-j,i+1);r=min(sy+i,m-j);
if(l<=r) t=h[now][r]-h[now][l-1];
}
upd(f[i][j][x][y+1],f[i][j][x][y]+t);
}
if(f[1][0][1][1]==4)
{short oo=1;}
}
cout<<f[ln][rn][un][dn]<<endl;
return 0;
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<bits/stdc++.h>
using namespace std;
using Int = signed;
template<typename T1,typename T2> inline void chmin(T1 &a,T2 b){if(a>b) a=b;}
template<typename T1,typename T2> inline void chmax(T1 &a,T2 b){if(a<b) a=b;}
//INSERT ABOVE HERE
const Int MAX = 102;
Int h,w,x,y;
Int b[MAX*3][MAX*3];
// [l, r] * [u, d]
Int sum(Int l,Int r,Int u,Int d){
if(l>r||u>d) return 0;
l+=MAX-1;r+=MAX;u+=MAX-1;d+=MAX;
return b[d][r]+b[u][l]-b[u][r]-b[d][l];
}
short dp[MAX][MAX][MAX][MAX];
short calcL(Int l,Int r,Int u,Int d){
if(x-l<r) return 0;
return sum(x-l,x-l,max(y-u,d),min(y+d,h-(u+1)));
}
short calcR(Int l,Int r,Int u,Int d){
if(w-(l+1)<x+r) return 0;
return sum(x+r,x+r,max(y-u,d),min(y+d,h-(u+1)));
}
short calcU(Int l,Int r,Int u,Int d){
if(y-u<d) return 0;
return sum(max(x-l,r),min(x+r,w-(l+1)),y-u,y-u);
}
short calcD(Int l,Int r,Int u,Int d){
if(h-(u+1)<y+d) return 0;
return sum(max(x-l,r),min(x+r,w-(l+1)),y+d,y+d);
}
signed main(){
cin>>h>>w;
vector<string> s(h);
for(Int i=0;i<h;i++) cin>>s[i];
memset(b,0,sizeof(b));
for(Int i=0;i<h;i++)
for(Int j=0;j<w;j++)
if(s[i][j]=='o') b[MAX+i][MAX+j]++;
for(Int i=0;i<h;i++)
for(Int j=0;j<w;j++)
if(s[i][j]=='E') y=i,x=j;
for(Int i=0;i<MAX*3;i++)
for(Int j=1;j<MAX*3;j++)
b[i][j]+=b[i][j-1];
for(Int i=1;i<MAX*3;i++)
for(Int j=0;j<MAX*3;j++)
b[i][j]+=b[i-1][j];
memset(dp,0,sizeof(dp));
for(int l=0;l<=w;l++){
for(int r=0;r<=w;r++){
for(int u=0;u<=h;u++){
for(int d=0;d<=h;d++){
//cout<<l<<" "<<r<<" "<<u<<" "<<d<<":"<<dp[l][r][u][d]<<endl;
chmax(dp[l+1][r][u][d],dp[l][r][u][d]+calcL(l+1,r,u,d));
chmax(dp[l][r+1][u][d],dp[l][r][u][d]+calcR(l,r+1,u,d));
chmax(dp[l][r][u+1][d],dp[l][r][u][d]+calcU(l,r,u+1,d));
chmax(dp[l][r][u][d+1],dp[l][r][u][d]+calcD(l,r,u,d+1));
}
}
}
}
cout<<dp[w][w][h][h]<<endl;
return 0;
}
| CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include <stdio.h>
#include <iostream>
#include <algorithm>
#include <memory.h>
using namespace std;
typedef long long LL;
const int maxn = 105;
short ans,col[maxn][maxn],row[maxn][maxn];
short dp[maxn][maxn][maxn][maxn];
int n,m,xe,ye;char g[maxn][maxn];
void upd(short &x,short y) {y>x?x=y:0;}
short calcrow(int x,int y1,int y2) {
return y1>y2?0:row[x][y2]-row[x][y1-1];
}
short calccol(int y,int x1,int x2) {
return x1>x2?0:col[x2][y]-col[x1-1][y];
}
int main()
{
#ifdef Amberframe
freopen("agc004e.in","r",stdin);
freopen("agc004e.out","w",stdout);
#endif
scanf("%d %d",&n,&m);
for (int i=1;i<=n;i++) scanf("%s",g[i]+1);
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
if (g[i][j]=='E') xe=i,ye=j;
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
row[i][j]=row[i][j-1]+(g[i][j]=='o');
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++)
col[i][j]=col[i-1][j]+(g[i][j]=='o');
for (int xl=xe;xl>=1;xl--)
for (int yl=ye;yl>=1;yl--)
for (int xr=xe;xr<=n;xr++)
for (int yr=ye;yr<=m;yr++)
{
short cur=dp[xl][yl][xr][yr];ans=max(ans,cur);
if (xl>1) upd(dp[xl-1][yl][xr][yr],cur+(xl-1>=xr-xe+1?calcrow(xl-1,max(yl,yr-ye+1),min(yr,m-ye+yl)):0));
if (xr<n) upd(dp[xl][yl][xr+1][yr],cur+(xr+1<=n-xe+xl?calcrow(xr+1,max(yl,yr-ye+1),min(yr,m-ye+yl)):0));
if (yl>1) upd(dp[xl][yl-1][xr][yr],cur+(yl-1>=yr-ye+1?calccol(yl-1,max(xl,xr-xe+1),min(xr,n-xe+xl)):0));
if (yr<m) upd(dp[xl][yl][xr][yr+1],cur+(yr+1<=m-ye+yl?calccol(yr+1,max(xl,xr-xe+1),min(xr,n-xe+xl)):0));
}
printf("%d",(int)ans);
return 0;
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<bits/stdc++.h>
using namespace std;
#define LL long long
#define ULL unsigned long long
#define mp make_pair
#define pb push_back
#define pii pair<int,int>
#define pll pair<LL,LL>
#define x first
#define y second
#define pi acos(-1)
#define sqr(x) ((x)*(x))
#define pdd pair<double,double>
#define MEMS(x) memset(x,-1,sizeof(x))
#define MEM(x) memset(x,0,sizeof(x))
#define less Less
#define EPS 1e-4
#define arg ARG
#define cpdd const pdd
#define rank Rank
#define KK 500
#define N 100005
#define MXN 200005
short dp[105][105][105][105];
int sum[105][105];
pii p;
int h,w;
int get(int a,int b,int c,int d){
// printf("%d %d %d %d\n",a,b,c,d);
if(a>c||b>d)return 0;
return sum[c][d]+sum[a-1][b-1]-sum[a-1][d]-sum[c][b-1];
}
int DP(int a,int b,int c,int d){
if(dp[a][b][c][d]!=-1)return dp[a][b][c][d];
if(p.x<a||p.x>c||p.y<b||p.y>d)return 0;
// printf("%d %d %d %d\n",a,b,c,d);
dp[a][b][c][d]=max((int)dp[a][b][c][d],DP(a+1,b,c,d)+get(max(a,c-p.x+1),max(b,d-p.y+1),min(a,h-(p.x-a-1)),min(d,w-(p.y-b))));
dp[a][b][c][d]=max((int)dp[a][b][c][d],DP(a,b,c-1,d)+get(max(c,c-1-p.x+1),max(b,d-p.y+1),min(c,h-(p.x-a)),min(d,w-(p.y-b))));
dp[a][b][c][d]=max((int)dp[a][b][c][d],DP(a,b+1,c,d)+get(max(a,c-p.x+1),max(b,d-p.y+1),min(c,h-(p.x-a)),min(b,w-(p.y-b-1))));
dp[a][b][c][d]=max((int)dp[a][b][c][d],DP(a,b,c,d-1)+get(max(a,c-p.x+1),max(d,d-1-p.y+1),min(c,h-(p.x-a)),min(d,w-(p.y-b))));
return dp[a][b][c][d];
}
int main(){
// int h,w;
scanf("%d %d",&h,&w);
char c[105][105];
MEMS(dp);
for(int i=1;i<=h;i++){
scanf("%s",c[i]+1);
for(int j=1;j<=w;j++){
if(c[i][j]=='o')sum[i][j]++;
if(c[i][j]=='E')dp[i][j][i][j]=0,p=mp(i,j);
sum[i][j]+=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
}
}
printf("%d\n",DP(1,1,h,w));
}
| CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include <cstdio>
#include <iostream>
using namespace std;
char map[105][105];
int n, m, x, y, ans = 0, a[105][105], b[105][105];
short f[105][105][105][105];
int max(int x, int y) { return x > y ? x : y; }
int min(int x, int y) { return x < y ? x : y; }
int main() {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; i++) {
scanf("%s", map[i] + 1);
for (int j = 1; j <= m; j++) {
a[i][j] = a[i][j - 1] + (map[i][j] == 'o');
b[i][j] = b[i - 1][j] + (map[i][j] == 'o');
if (map[i][j] == 'E') x = i, y = j;
}
}
for (int i = x; i >= 1; i--)
for (int j = y; j >= 1; j--) {
for (int k = x; k <= n; k++)
for (int l = y; l <= m; l++) {
if (i > 1 && i - 1 > k - x)
ans = max(ans, f[i - 1][j][k][l] =
max(f[i - 1][j][k][l],
f[i][j][k][l] + a[i - 1][min(l, m - y + j)] -
a[i - 1][max(j - 1, l - y)]));
if (k < n && n - k > x - i)
ans = max(ans, f[i][j][k + 1][l] =
max(f[i][j][k + 1][l],
f[i][j][k][l] + a[k + 1][min(l, m - y + j)] -
a[k + 1][max(j - 1, l - y)]));
if (j > 1 && j - 1 > l - y)
ans = max(ans, f[i][j - 1][k][l] =
max(f[i][j - 1][k][l],
f[i][j][k][l] + b[min(k, n - x + i)][j - 1] -
b[max(i - 1, k - x)][j - 1]));
if (l < m && m - l > y - j)
ans = max(ans, f[i][j][k][l + 1] =
max(f[i][j][k][l + 1],
f[i][j][k][l] + b[min(k, n - x + i)][l + 1] -
b[max(i - 1, k - x)][l + 1]));
}
}
printf("%d", ans);
}
| CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include <bits/stdc++.h>
#define N 101
unsigned short F[N][N][N][N],mp[N][N],sum[N][N];
using namespace std;
inline int rd() { int r; scanf("%d",&r); return r; }
int n,m,sx,sy;
char s[N];
inline void ut(unsigned short &x,unsigned short y) { x = max(x, y); }
void init() {
n = rd(), m = rd();
for (int i=1;i<=n;i++) {
scanf("%s",s+1);
for (int j=1;j<=m;j++) {
s[j] == 'E' ? sx=i, sy=j :0;
s[j] == 'o' ? mp[i][j]=1 :0;
sum[i][j] = sum[i-1][j] + sum[i][j-1] - sum[i-1][j-1] + mp[i][j];
}
}
}
inline short get(int x1, int y1, int x2, int y2) {
if (x1 > x2 || y1 > y2) return 0;
return sum[x2][y2] - sum[x1][y2] - sum[x2][y1] + sum[x1][y1];
}
void dp(int i, int j, int x, int y) {
unsigned short v = 0;
v = i+x+1 <= sx-1 ?
get(sx-i-2, max(sy-j-1, y), sx-i-1, min(sy+y, m-j)) :0;
ut( F[i+1][j][x][y], F[i][j][x][y] + v );
v = i+x+1 <= n-sx ?
get(sx+x, max(sy-j-1, y), sx+x+1, min(sy+y, m-j)) :0;
ut( F[i][j][x+1][y], F[i][j][x][y] + v );
v = j+y+1 <= sy-1 ?
get(max(sx-i-1, x), sy-j-2, min(sx+x, n-i), sy-j-1) :0;
ut( F[i][j+1][x][y], F[i][j][x][y] + v );
v = j+y+1 <= m-sy ?
get(max(sx-i-1, x), sy+y, min(sx+x, n-i), sy+y+1) :0;
ut( F[i][j][x][y+1], F[i][j][x][y] + v );
return ;
}
void solve() {
unsigned short ans = 0;
for (int i=0;i<=sx-1;i++)
for (int j=0;j<=sy-1;j++)
for (int _i=0;_i<=n-sx;_i++)
for (int _j=0;_j<=m-sy;_j++) {
dp(i,j,_i,_j), ans = max(ans, F[i][j][_i][_j]);
// if (F[i][j][_i][_j] == 1) printf("::1:: %d %d %d %d\n",i,j,_i,_j);
// if (F[i][j][_i][_j] == 2) printf("::2:: %d %d %d %d\n",i,j,_i,_j);
// if (F[i][j][_i][_j] == 3) printf("::3:: %d %d %d %d\n",i,j,_i,_j);
}
printf("%d\n",(int)ans);
}
int main() {
init();
solve();
return 0;
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<bits/stdc++.h>
#define N 105
using namespace std;
int ans;
int cnt[N][N];
#define mmax(x,y) (x=max(x,y),ans=max(ans,y))
#define get(x1,y1,x2,y2) (cnt[x2][y2]-cnt[x2][y1-1]-cnt[x1-1][y2]+cnt[x1-1][y1-1])
char s[N][N];
int n,m,Ex,Ey;
int dp[2][N][N][N];
int main(){
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++) scanf("%s",s[i]+1);
for (int i=1;i<=n;i++)
for (int j=1;j<=m;j++){
cnt[i][j]=cnt[i-1][j]+cnt[i][j-1]-cnt[i-1][j-1]+(s[i][j]=='o');
if(s[i][j]=='E') Ex=i,Ey=j;
}
int now=0;
for (int i=0;i<=Ex-1;i++){
memset(dp[now^1],0,sizeof(dp[0]));
for (int j=0;j<=n-Ex;j++)
for (int k=0;k<=Ey-1;k++)
for (int L=0;L<=m-Ey;L++){
int v=dp[now][j][k][L];
int xl=max(Ex-i,j+1),yl=max(Ey-k,L+1),xr=min(Ex+j,n-i),yr=min(Ey+L,m-k);
if (Ex-i-1>j) mmax(dp[now^1][j][k][L],v+get(Ex-i-1,yl,Ex-i-1,yr));
if (Ex+j<n-i) mmax(dp[now][j+1][k][L],v+get(Ex+j+1,yl,Ex+j+1,yr));
if (Ey-k-1>L) mmax(dp[now][j][k+1][L],v+get(xl,Ey-k-1,xr,Ey-k-1));
if (Ey+L<m-k) mmax(dp[now][j][k][L+1],v+get(xl,Ey+L+1,xr,Ey+L+1));
}
now^=1;
}
printf("%d\n",ans);
return 0;
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;
typedef vector<int> vi;
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,n) rep2(i,0,n)
#define rep2(i,m,n) for(int i=m;i<(n);i++)
#define ALL(c) (c).begin(),(c).end()
const int M = 101;
int H, W;
int sx, sy;
short ac[M][M];
string s[M];
short dp[M][M][M][M];
short get(int a, int b, int c, int d) { //[a,c],[b,d]
if (a > c || b > d) return 0;
return ac[c+1][d+1] - ac[c+1][b] - ac[a][d+1] + ac[a][b];
}
short calc(int p, int q, int r, int s) {//dead : bottom p, top r, right r, left s
if (dp[p][q][r][s] != -1) {
return dp[p][q][r][s];
}
short &res = dp[p][q][r][s];
res = 0;
if (p + r < H) {
if (sx - p - 1 >= r) {
res = max<short>(res, calc(p + 1, q, r, s) + get(sx - p - 1, max(sy - q, s), sx - p - 1, min(sy + s, W - 1 - q)));
}
if (sx + r + 1 < H - p) {
res = max<short>(res, calc(p, q, r + 1, s) + get(sx + r + 1, max(sy - q, s), sx + r + 1, min(sy + s, W - 1 - q)));
}
}
if (q + s < W) {
if (sy - q - 1 >= s) {
res = max<short>(res, calc(p, q + 1, r, s) + get(max(sx - p, r), sy - q - 1, min(sx + r, H - 1 - p), sy - q - 1));
}
if (sy + s + 1 < W - q) {
res = max<short>(res, calc(p, q, r, s + 1) + get(max(sx - p, r), sy + s + 1, min(sx + r, H - 1 - p), sy + s + 1));
}
}
return res;
}
int main() {
cin >> H >> W;
rep(i, H) cin >> s[i];
rep(i, H) {
rep(j, W) {
ac[i+1][j+1] = ac[i+1][j] + ac[i][j+1] - ac[i][j] + (s[i][j] == 'o');
if (s[i][j] == 'E') {
sx = i, sy = j;
}
}
}
memset(dp, -1, sizeof(dp));
cout << calc(0, 0, 0, 0) << endl;
return 0;
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<cstdio>
#include<cstring>
#include<algorithm>
#define MAXN 100
using namespace std;
char Map[MAXN+5][MAXN+5];
int n,m,ex,ey;
int sumr[MAXN+5][MAXN+5],sumc[MAXN+5][MAXN+5];
int dp[MAXN+5][MAXN+5][MAXN+5];
void Init()
{
memset(dp,-1,sizeof(dp));
}
int main()
{
// freopen("robot.in","r",stdin);
// freopen("robot.out","w",stdout);
Init();
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%s",Map[i]+1);
for(int j=1;j<=m;j++)
{
sumr[i][j]=sumr[i][j-1];
sumc[i][j]=sumc[i-1][j];
if(Map[i][j]=='E')
ex=i,ey=j;
else if(Map[i][j]=='o')
{
sumr[i][j]++;
sumc[i][j]++;
}
}
}
int L,R,U,D,ans=0;
L=ey-1;
R=m-ey;
U=ex-1;
D=n-ex;
dp[0][0][0]=0;
for(int l=0;l<=L;l++)
for(int r=0;r<=R;r++)
for(int u=0;u<=U;u++)
for(int d=0;d<=D;d++)
{
if(dp[r][u][d]==-1)
continue;
ans=max(dp[r][u][d],ans);
int up=max(ex-u,d+1);
int down=min(ex+d,n-u);
int left=max(ey-l,r+1);
int right=min(ey+r,m-l);
if(up>down||left>right)
continue;
int add;
if(u<U-d)
{
add=sumr[ex-u-1][right]-sumr[ex-u-1][left-1];
dp[r][u+1][d]=max(dp[r][u+1][d],dp[r][u][d]+add);
}
if(d<D-u)
{
add=sumr[ex+d+1][right]-sumr[ex+d+1][left-1];
dp[r][u][d+1]=max(dp[r][u][d+1],dp[r][u][d]+add);
}
if(r<R-l)
{
add=sumc[down][ey+r+1]-sumc[up-1][ey+r+1];
dp[r+1][u][d]=max(dp[r+1][u][d],dp[r][u][d]+add);
}
if(l<L-r)
{
add=sumc[down][ey-l-1]-sumc[up-1][ey-l-1];
dp[r][u][d]=max(dp[r][u][d],dp[r][u][d]+add);
}
}
printf("%d\n",ans);
return 0;
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define ull unsigned long long
#define poly vector<ll>
#define pb push_back
#define pii pair<int,int>
#define fi first
#define se second
#define mp make_pair
#define For(i,l,r) for(int i=(int)(l);i<=(int)(r);i++)
#define Rep(i,r,l) for(int i=(int)(r);i>=(int)(l);i--)
inline ll read(){
ll x=0;char ch=getchar();bool d=1;
for(;!isdigit(ch);ch=getchar()) if(ch=='-') d=0;
for(;isdigit(ch);ch=getchar()) x=x*10+ch-'0';
return d?x:-x;
}
inline void write(ll x){
if(x<0) x=-x,putchar('-');
if(x>=10) write(x/10);
putchar('0'+x%10);
}
inline void writeln(ll x){write(x);puts("");}
inline void writep(ll x){write(x);putchar(' ');}
inline ull rnd(){
return ((ull)rand()<<30^rand())<<4|rand()%4;
}
const int N=105;
int a[N][N][2];
short f[N][N][N][N];
int main(){
int n=read(),m=read(),x,y;
For(i,1,n){
char s[N];
scanf("%s",s+1);
For(j,1,m){
if(s[j]=='E') x=i,y=j;
a[i][j][0]=a[i-1][j][0]+(s[j]=='o');
a[i][j][1]=a[i][j-1][1]+(s[j]=='o');
}
}
int ans=0;
For(l,0,y-1) For(r,0,m-y){
For(u,0,x-1) For(d,0,n-x){
ans=max(ans,(int)f[l][r][u][d]);
if(l+r<y-1) f[l+1][r][u][d]=max((int)f[l+1][r][u][d],(int)f[l][r][u][d]+a[min(x+d,n-u)][y-l-1][0]-a[max(x-u-1,d)][y-l-1][0]);
if(l+r<m-y) f[l][r+1][u][d]=max((int)f[l][r+1][u][d],(int)f[l][r][u][d]+a[min(x+d,n-u)][y+r+1][0]-a[max(x-u-1,d)][y+r+1][0]);
if(u+d<x-1) f[l][r][u+1][d]=max((int)f[l][r][u+1][d],(int)f[l][r][u][d]+a[x-u-1][min(y+r,m-l)][1]-a[x-u-1][max(y-l-1,r)][1]);
if(u+d<n-x) f[l][r][u][d+1]=max((int)f[l][r][u][d+1],(int)f[l][r][u][d]+a[x+d+1][min(y+r,m-l)][1]-a[x+d+1][max(y-l-1,r)][1]);
}
}
cout<<ans;
}
| CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=100;
int n,m,op_x,op_y,ans;
char S[maxn+8];
int f[maxn+8][maxn+8][maxn+8][maxn+8],v[maxn+8][maxn+8][2];
int read()
{
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=x*10+ch-'0';
return x*f;
}
int main()
{
n=read(),m=read();
for (int i=1;i<=n;i++)
{
scanf("%s",S+1);
for (int j=1;j<=m;j++)
{
v[i][j][0]=v[i][j][1]=S[j]=='o';
v[i][j][0]+=v[i][j-1][0];
v[i][j][1]+=v[i-1][j][1];
if (S[j]=='E') op_x=i,op_y=j;
}
}
for (int l=0;l<=op_y-1;l++)
for (int r=0;r<=m-op_y;r++)
for (int u=0;u<=op_x-1;u++)
for (int d=0;d<=n-op_x;d++)
{
ans=max(ans,f[l][r][u][d]);
if (l+r<op_y-1) f[l+1][r][u][d]=max(f[l+1][r][u][d],f[l][r][u][d]+v[min(n-u,op_x+d)][op_y-l-1][1]-v[max(d,op_x-u-1)][op_y-l-1][1]);
if (l+r<m-op_y) f[l][r+1][u][d]=max(f[l][r+1][u][d],f[l][r][u][d]+v[min(n-u,op_x+d)][op_y+r+1][1]-v[max(d,op_x-u-1)][op_y+r+1][1]);
if (u+d<op_x-1) f[l][r][u+1][d]=max(f[l][r][u+1][d],f[l][r][u][d]+v[op_x-u-1][min(m-l,op_y+r)][0]-v[op_x-u-1][max(r,op_y-l-1)][0]);
if (u+d<n-op_x) f[l][r][u][d+1]=max(f[l][r][u][d+1],f[l][r][u][d]+v[op_x+d+1][min(m-l,op_y+r)][0]-v[op_x+d+1][max(r,op_y-l-1)][0]);
}
printf("%d\n",ans);
return 0;
}
| CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<cstdio>
#include<cstring>
#include<algorithm>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dwn(i,a,b) for(int i=a;i>=b;i--)
using namespace std;
const int maxn=105;
const int maxm=7000010;
int f[maxm];
int n,m,x,y,S1[maxn][maxn],S2[maxn][maxn];
int id(int i,int j,int k,int l) {return ((i*(n-x+1)+j)*y+k)*(m-y+1)+l+1;}
int sumx(int k,int l,int r) {return S2[k][r]-S2[k][l-1];}
int sumy(int k,int l,int r) {return S1[r][k]-S1[l-1][k];}
void relax(int& x,int y) {if(x<y) x=y;}
char M[maxn][maxn];
int main() {
scanf("%d%d",&n,&m);
rep(i,1,n) scanf("%s",M[i]+1);
rep(i,1,n) rep(j,1,m) {
if(M[i][j]=='E') x=i,y=j;
else if(M[i][j]=='o') S1[i][j]=S2[i][j]=1;
}
rep(i,1,n) rep(j,1,m) S1[i][j]+=S1[i-1][j],S2[i][j]+=S2[i][j-1];
int ans=0;
rep(u,0,x-1) rep(d,0,n-x) rep(l,0,y-1) rep(r,0,m-y) {
int res=f[id(u,d,l,r)];ans=max(ans,res);
if(u!=x-1) relax(f[id(u+1,d,l,r)],res+(x-u-1>d?sumx(x-u-1,max(y-l,r+1),min(y+r,m-l)):0));
if(d!=n-x) relax(f[id(u,d+1,l,r)],res+(x+d+1<=n-u?sumx(x+d+1,max(y-l,r+1),min(y+r,m-l)):0));
if(l!=y-1) relax(f[id(u,d,l+1,r)],res+(y-l-1>r?sumy(y-l-1,max(x-u,d+1),min(x+d,n-u)):0));
if(r!=m-y) relax(f[id(u,d,l,r+1)],res+(y+r+1<=m-l?sumy(y+r+1,max(x-u,d+1),min(x+d,n-u)):0));
}
printf("%d\n",ans);
return 0;
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
#define fi first
#define se second
typedef pair<int,int> PII;
const int N = 111;
int n, m, ans, Ex, Ey;
int sr[N][N], sc[N][N], dp[N][N][N][N];
char ch[N][N];
int main() {
cin >> n >> m;
for (int i = 1; i <= n; i++)
for (int j = 1; j <= m; j++) {
cin >> ch[i][j];
sr[i][j] = sr[i][j-1] + (ch[i][j] == 'o');
sc[i][j] = sc[i-1][j] + (ch[i][j] == 'o');
if (ch[i][j] == 'E') Ex = i, Ey = j;
}
for (int u = 0; u < Ex; u++) {
for (int d = 0; d <= n - Ex; d++) {
for (int l = 0; l < Ey; l++) {
for (int r = 0; r <= m - Ey; r++) {
int ret = 0;
int top = max(Ex - u, d + 1);
int bot = min(Ex + d, n - u);
int lft = max(Ey - l, r + 1);
int rgt = min(Ey + r, m - l);
if (u > 0 && Ex - u - d > 0) ret = max(ret, dp[u-1][d][l][r] + (sr[Ex-u][rgt] - sr[Ex-u][lft-1]));
if (d > 0 && Ex + d + u <= n) ret = max(ret, dp[u][d-1][l][r] + (sr[Ex+d][rgt] - sr[Ex+d][lft-1]));
if (l > 0 && Ey - l - r > 0) ret = max(ret, dp[u][d][l-1][r] + (sc[bot][Ey-l] - sc[top-1][Ey-l]));
if (r > 0 && Ey + l + r <= m) ret = max(ret, dp[u][d][l][r-1] + (sc[bot][Ey+r] - sc[top-1][Ey+r]));
dp[u][d][l][r] = ret;
ans = max(ans,ret);
}
}
}
}
cout << ans;
}
| CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<bits/stdc++.h>
#define maxn 105
using namespace std;
int xe,ye;
short n,m,ans,a[maxn][maxn],p=0,np=1,dp[2][maxn][maxn][maxn];
char c[maxn][maxn];
void inline upd(short& x,short y){
if(x<y){x=y;if(y>ans)ans=y;}
}
int inline z(int i,int j,int k,int l){
return a[k][l]-a[i-1][l]-a[k][j-1]+a[i-1][j-1];
}
int inline cal1(int w,int h,int x,int l,int r){
int ret=0,_w=n-w,_h=m-h;
if(ye>1+_h)return 0;
else return z(max(l,xe-_w+l-1),x,min(r,xe+l-1),x);
}
int inline cal2(int w,int h,int y,int l,int r){
int ret=0,_w=n-w,_h=m-h;
if(xe>1+_w)return 0;
else return z(y,max(l,ye+l-_h-1),y,min(r,ye+l-1));
}
int inline cal3(int w,int h,int x,int l,int r){
int ret=0,_w=n-w,_h=m-h;
if(ye<h)return 0;
else return z(max(l,xe-_w+l-1),x,min(r,xe+l-1),x);
}
int inline cal4(int w,int h,int y,int l,int r){
int ret=0,_w=n-w,_h=m-h;
if(xe<w)return 0;
else return z(y,max(l,ye-_h+l-1),y,min(r,ye+l-1));
}
int main(){
// freopen("in.txt","r",stdin);
cin>>n>>m;
for(int i=1;i<=n;++i)
scanf("%s",c[i]+1);
for(int i=1;i<=n;++i)
for(int j=1;j<=m;++j){
a[i][j]=(c[i][j]=='o')+a[i-1][j]+a[i][j-1]-a[i-1][j-1];
if(c[i][j]=='E')xe=i,ye=j;
}
dp[np][1][1][m]=ans=1;
for(int k=n;k>=1;--k,swap(p,np))for(int l=m;l>=1;--l)
for(int i=1;i<=n-k+1;++i)for(int j=1;j<=m-l+1;++j)if(dp[np][i][j][l]){
int x=dp[np][i][j][l];
// printf("{%d,%d,%d,%d,%d}\n",i,j,k,l,x);
upd(dp[np][i][j+1][l-1],x+cal1(k,l,j,i,i+k-1));
upd(dp[p][i+1][j][l],x+cal2(k,l,i,j,j+l-1));
upd(dp[np][i][j][l-1],x+cal3(k,l,j+l-1,i,i+k-1));
upd(dp[p][i][j][l],x+cal4(k,l,i+k-1,j,j+l-1));
dp[np][i][j][l]=0;
}
printf("%d",ans-1);
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std;
const int N=105;
int n,m,x,y,L[N][N],R[N][N];
char a[N][N];
short f[N][N][N][N],ans;
inline void fix(short &x,short y){
if(x<y) x=y;
}
int main(){
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++){
scanf("%s",a[i]+1);
for(int j=1;j<=m;j++){
if(a[i][j]=='E') x=i,y=j;
L[i][j]=L[i][j-1]+(a[i][j]=='o');
R[i][j]=R[i-1][j]+(a[i][j]=='o');
}
}
memset(f,-0x7f,sizeof(f)); f[0][0][0][0]=0;
for(int u=0;x-u>0;u++)
for(int d=0;x+d<=n;d++)
for(int l=0;y-l>0;l++)
for(int r=0;y+r<=m;r++){
if(f[u][d][l][r]<0) continue;
short cur=f[u][d][l][r]; ans=max(ans,cur);
if(x-d>u+1 && x-u-1>0)
fix(f[u+1][d][l][r],cur+L[x-u-1][min(y+r,m-l)]-L[x-u-1][max(y-l-1,r)]);
if(x+u+d<n && x+d+1<=n)
fix(f[u][d+1][l][r],cur+L[x+d+1][min(y+r,m-l)]-L[x+d+1][max(y-l-1,r)]);
if(y+l+r<m && y+r+1<=m)
fix(f[u][d][l][r+1],cur+R[min(x+d,n-u)][y+r+1]-R[max(x-u-1,d)][y+r+1]);
if(y-r>l+1 && y-l-1>0)
fix(f[u][d][l+1][r],cur+R[min(x+d,n-u)][y-l-1]-R[max(x-u-1,d)][y-l-1]);
}
printf("%d\n",(int)ans);
return 0;
}
| CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include <iostream>
#include <cstdio>
#include <cstring>
#include <cassert>
#include <cctype>
#include <algorithm>
using namespace std;
typedef long long lint;
#define cout cerr
#define ni (next_num<int>())
template<class T>inline T next_num(){
T i=0;char c;
while(!isdigit(c=getchar())&&c!='-');
bool flag=c=='-';
flag?(c=getchar()):0;
while(i=i*10-'0'+c,isdigit(c=getchar()));
return flag?-i:i;
}
template<class T1,class T2>inline void apmax(T1 &a,const T2 &b){if(a<b)a=b;}
const int N=101;
char mat[N][N];
short f[N][N][N][N],sum[N][N];
inline int asksum(int x1,int x2,int y1,int y2){
return x1<=x2&&y1<=y2?sum[x2][y2]-sum[x1-1][y2]-sum[x2][y1-1]+sum[x1-1][y1-1]:0;
}
int main(){
int n=ni,m=ni,ex,ey;
memset(sum,0,sizeof(sum));
for(int i=1;i<=n;i++){
scanf("%s",mat[i]+1);
for(int j=1;j<=m;j++){
sum[i][j]=(mat[i][j]=='o')+sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
if(mat[i][j]=='E'){
ex=i,ey=j;
}
}
}
for(int i=ex;i>=1;i--){
for(int j=ex;j<=n;j++){
for(int k=ey;k>=1;k--){
for(int l=ey;l<=m;l++){
short &F=f[i][j][k][l]=0;
int xs=max(1+(j-ex),i),xt=min(n-(ex-i),j);
int ys=max(1+(l-ey),k),yt=min(m-(ey-k),l);
if(i<ex)apmax(F,f[i+1][j][k][l]+asksum(xs,i,ys,yt));
if(j>ex)apmax(F,f[i][j-1][k][l]+asksum(j,xt,ys,yt));
if(k<ey)apmax(F,f[i][j][k+1][l]+asksum(xs,xt,ys,k));
if(l>ey)apmax(F,f[i][j][k][l-1]+asksum(xs,xt,l,yt));
}
}
}
}
printf("%d\n",(int)f[1][n][1][m]);
return 0;
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<cstdio>
#include<iostream>
#include<cstring>
#define RG register
#define si short int
using namespace std;
const int N=101;
si n,m,x,y,ans,dp[N][N][N][N],sum[N][N];
char g[N][N];
template<typename I> inline void read(I &ot){
I ch=getchar(), x=0, f=1;
while(ch<'0' || ch>'9'){if(ch=='-') f=-1; ch=getchar(); }
while(ch>='0' && ch<='9'){x=x*10+ch-'0'; ch=getchar(); }
ot=x*f;}
template<typename I, typename... U> inline void read(I &x,U&... y){read(x); read(y...);}
template<typename I>inline I mx(const I&a,const I&b){return a>b ? a : b;}
template<typename I>inline I mi(const I&a,const I&b){return a<b ? a : b;}
template<typename I>inline void swp(I&a,I&b){I t=a; a=b; b=t;}
inline void cmx(si &a,si b){if(b>a) a=b;}
inline si s(int a,int b,int c,int d){
if(a>c) swp(a,c); if(b>d) swp(b,d);
return sum[c][d]-sum[c][b-1]-sum[a-1][d]+sum[a-1][b-1];
}
int main()
{
// freopen("Salvage Robots.in","r",stdin);
read(n,m);
for(RG int i=1;i<=n;i++)
{
scanf("%s",g[i]+1);
for(RG int j=1;j<=m;j++)
{
if(g[i][j]=='o') sum[i][j]=1;
else if(g[i][j]=='E') x=i, y=j;
sum[i][j]+=sum[i-1][j]+sum[i][j-1]-sum[i-1][j-1];
}
}
for(int i=x;i;i--)
for(int j=y;j;j--)
for(int k=x;k<=n;k++)
for(int l=y;l<=m;l++)
{
ans=mx(ans,dp[i][j][k][l]);
int li=l-y+1, ri=m-y+j, us=k-x+1, ds=n-x+i;
if(i-1>=us) cmx(dp[i-1][j][k][l],dp[i][j][k][l]+s(i-1,mx(li,j),i-1,mi(ri,l)));
if(j-1>=li) cmx(dp[i][j-1][k][l],dp[i][j][k][l]+s(mx(i,us),j-1,mi(k,ds),j-1));
if(k+1<=ds) cmx(dp[i][j][k+1][l],dp[i][j][k][l]+s(k+1,mx(li,j),k+1,mi(ri,l)));
if(l+1<=ri) cmx(dp[i][j][k][l+1],dp[i][j][k][l]+s(mx(i,us),l+1,mi(k,ds),l+1));
}
cout<<ans<<endl;
return 0;
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include <cstdio>
#include <iostream>
#define N 101
using namespace std;
int n,m,X,Y,ans=0,a[N][N],b[N][N];
short f[N][N][N][N];
char map[N][N];
int max(int x,int y){ return x>y?x:y; }
int min(int x,int y){ return x<y?x:y; }
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
{
scanf("%s",map[i]+1);
for (int j=1;j<=m;j++)
{
a[i][j]=a[i][j-1]+(map[i][j]=='o'), b[i][j]=b[i-1][j]+(map[i][j]=='o');
if (map[i][j]=='E') X=i,Y=j;
}
}
for (int i=X;i>=1;i--) for (int j=Y;j>=1;j--) for (int p=X;p<=n;p++) for (int q=Y;q<=m;q++)
{
if (i>1&&i-1>p-X) ans=max(ans,f[i-1][j][p][q]=max(f[i-1][j][p][q],f[i][j][p][q]+a[i-1][min(q,m-Y+j)]-a[i-1][max(j-1,q-Y)]));
if (p<n&&n-p>X-i) ans=max(ans,f[i][j][p+1][q]=max(f[i][j][p+1][q],f[i][j][p][q]+a[p+1][min(q,m-Y+j)]-a[p+1][max(j-1,q-Y)]));
if (j>1&&j-1>q-Y) ans=max(ans,f[i][j-1][p][q]=max(f[i][j-1][p][q],f[i][j][p][q]+b[min(p,n-X+i)][j-1]-b[max(i-1,p-X)][j-1]));
if (q<m&&m-q>Y-j) ans=max(ans,f[i][j][p][q+1]=max(f[i][j][p][q+1],f[i][j][p][q]+b[min(p,n-X+i)][q+1]-b[max(i-1,p-X)][q+1]));
}
printf("%d",ans);
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<bits/stdc++.h>
#define N 105
using namespace std;
short line[N][N],li[N][N],ans[N][N][N][N],n,m,op,sx,sy;
char s[N][N];
int main(){
cin>>n>>m;
for (short i=1;i<=n;i++) scanf("%s",s[i]+1);
for (short i=1;i<=n;i++) for (short j=1;j<=m;j++) { line[i][j]=line[i][j-1]+(s[i][j]=='o'); li[i][j]=li[i-1][j]+(s[i][j]=='o'); if (s[i][j]=='E') sx=i,sy=j; }
for (short i=sx;i;i--) for (short j=sy;j;j--) for (short k=sx;k<=n;k++) for (short p=sy;p<=m;p++) {
if (1<i && k+1<sx+i) op=max(op,ans[i-1][j][k][p]=max((int)ans[i-1][j][k][p],ans[i][j][k][p]+line[i-1][min((int)p,m-sy+j)]-line[i-1][max(j-1,p-sy)]));
if (k<n && sx+k<n+i) op=max(op,ans[i][j][k+1][p]=max((int)ans[i][j][k+1][p],ans[i][j][k][p]+line[k+1][min((int)p,m-sy+j)]-line[k+1][max(j-1,p-sy)]));
if (1<j && p+1<sy+j) op=max(op,ans[i][j-1][k][p]=max((int)ans[i][j-1][k][p],ans[i][j][k][p]+li[min((int)k,n-sx+i)][j-1]-li[max(i-1,k-sx)][j-1]));
if (p<m && sy+p<m+j) op=max(op,ans[i][j][k][p+1]=max((int)ans[i][j][k][p+1],ans[i][j][k][p]+li[min((int)k,n-sx+i)][p+1]-li[max(i-1,k-sx)][p+1]));
}
cout<<op;
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | // package atcoder.agc.agc004;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class Main {
public static void main(String[] args) {
InputReader in = new InputReader(System.in);
PrintWriter out = new PrintWriter(System.out);
int h = in.nextInt();
int w = in.nextInt();
int ey = -1;
int ex = -1;
char[][] cmap = new char[h][];
for (int i = 0; i < h ; i++) {
cmap[i] = in.nextToken().toCharArray();
for (int j = 0; j < w ; j++) {
if (cmap[i][j] == 'E') {
ey = i;
ex = j;
}
}
}
imos = new int[h+1][w+1];
for (int i = 0; i < h ; i++) {
for (int j = 0; j < w ; j++) {
imos[i+1][j+1] = imos[i+1][j] + imos[i][j+1] - imos[i][j] + ((cmap[i][j] == 'o') ? 1 : 0);
}
}
int U = ey;
int D = h - U - 1;
int L = ex;
int R = w - L - 1;
int[][][][] dp = new int[U+1][D+1][L+1][R+1];
for (int u = 0; u <= U ; u++) {
for (int d = 0; d <= D; d++) {
for (int l = 0; l <= L; l++) {
Arrays.fill(dp[u][d][l], -1);
}
}
}
dp[0][0][0][0] = 0;
for (int u = 0; u <= U ; u++) {
for (int d = 0; d <= D ; d++) {
for (int l = 0; l <= L ; l++) {
for (int r = 0; r <= R ; r++) {
int base = dp[u][d][l][r];
if (base < 0) {
continue;
}
int tu = Math.max(ey-u, d);
int td = Math.min(ey+d, h-u-1);
int tl = Math.max(ex-l, r);
int tr = Math.min(ex+r, w-l-1);
if (u < U && d <= ey-u-1) {
dp[u+1][d][l][r] = Math.max(dp[u+1][d][l][r], base + inclusiveSum(ey-u-1, ey-u-1, tl, tr));
}
if (d < D && u <= (h-1)-(ey+d+1)) {
dp[u][d+1][l][r] = Math.max(dp[u][d+1][l][r], base + inclusiveSum(ey+d+1, ey+d+1, tl, tr));
}
if (l < L && r <= ex-l-1) {
dp[u][d][l+1][r] = Math.max(dp[u][d][l+1][r], base + inclusiveSum(tu, td, ex-l-1, ex-l-1));
}
if (r < R && l <= (w-1)-(ex+r+1)) {
dp[u][d][l][r+1] = Math.max(dp[u][d][l][r+1], base + inclusiveSum(tu, td, ex+r+1, ex+r+1));
}
}
}
}
}
int max = 0;
for (int u = 0; u <= U ; u++) {
for (int d = 0; d <= D; d++) {
for (int l = 0; l <= L; l++) {
for (int r = 0; r <= R; r++) {
max = Math.max(max, dp[u][d][l][r]);
}
}
}
}
out.println(max);
out.flush();
}
static int[][] imos;
static int inclusiveSum(int u, int d, int l, int r) {
if (u > d) {
return 0;
}
if (l > r) {
return 0;
}
return imos[d+1][r+1] - imos[d+1][l] - imos[u][r+1] + imos[u][l];
}
static class InputReader {
private InputStream stream;
private byte[] buf = new byte[1024];
private int curChar;
private int numChars;
public InputReader(InputStream stream) {
this.stream = stream;
}
private int[] nextInts(int n) {
int[] ret = new int[n];
for (int i = 0; i < n; i++) {
ret[i] = nextInt();
}
return ret;
}
private int[][] nextIntTable(int n, int m) {
int[][] ret = new int[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ret[i][j] = nextInt();
}
}
return ret;
}
private long[] nextLongs(int n) {
long[] ret = new long[n];
for (int i = 0; i < n; i++) {
ret[i] = nextLong();
}
return ret;
}
private long[][] nextLongTable(int n, int m) {
long[][] ret = new long[n][m];
for (int i = 0; i < n; i++) {
for (int j = 0; j < m; j++) {
ret[i][j] = nextLong();
}
}
return ret;
}
private double[] nextDoubles(int n) {
double[] ret = new double[n];
for (int i = 0; i < n; i++) {
ret[i] = nextDouble();
}
return ret;
}
private int next() {
if (numChars == -1)
throw new InputMismatchException();
if (curChar >= numChars) {
curChar = 0;
try {
numChars = stream.read(buf);
} catch (IOException e) {
throw new InputMismatchException();
}
if (numChars <= 0)
return -1;
}
return buf[curChar++];
}
public char nextChar() {
int c = next();
while (isSpaceChar(c))
c = next();
if ('a' <= c && c <= 'z') {
return (char) c;
}
if ('A' <= c && c <= 'Z') {
return (char) c;
}
throw new InputMismatchException();
}
public String nextToken() {
int c = next();
while (isSpaceChar(c))
c = next();
StringBuilder res = new StringBuilder();
do {
res.append((char) c);
c = next();
} while (!isSpaceChar(c));
return res.toString();
}
public int nextInt() {
int c = next();
while (isSpaceChar(c))
c = next();
int sgn = 1;
if (c == '-') {
sgn = -1;
c = next();
}
int res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c-'0';
c = next();
} while (!isSpaceChar(c));
return res*sgn;
}
public long nextLong() {
int c = next();
while (isSpaceChar(c))
c = next();
long sgn = 1;
if (c == '-') {
sgn = -1;
c = next();
}
long res = 0;
do {
if (c < '0' || c > '9')
throw new InputMismatchException();
res *= 10;
res += c-'0';
c = next();
} while (!isSpaceChar(c));
return res*sgn;
}
public double nextDouble() {
return Double.valueOf(nextToken());
}
public boolean isSpaceChar(int c) {
return c == ' ' || c == '\n' || c == '\r' || c == '\t' || c == -1;
}
public interface SpaceCharFilter {
public boolean isSpaceChar(int ch);
}
}
static void debug(Object... o) {
System.err.println(Arrays.deepToString(o));
}
}
| JAVA |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include <bits/stdc++.h>
using namespace std;
const int N = 105;
char s[N][N];
int r[N][N], c[N][N];
int f[N * N >> 2][N * N >> 2];
int h, w, X, Y;
int encode(int l, int r, int M) {return r * M + l;} // 编码
void chkmax(int& x, int v) {
x = max(x, v);
}
int dfs(int lX, int rX, int lY, int rY) {
int cX = encode(lX, rX, X), cY = encode(lY, rY, Y);
if (f[cX][cY] >= 0) return f[cX][cY];
f[cX][cY] = 0;
if (lX < X - 1)
chkmax(f[cX][cY], dfs(lX + 1, rX, lY, rY) + (rX + 1 <= X - lX - 1 ? max(0, r[X - lX - 1][min(Y + rY, w - lY)] - r[X - lX - 1][max(Y - lY, rY + 1) - 1]) : 0));
if (rX < h - X)
chkmax(f[cX][cY], dfs(lX, rX + 1, lY, rY) + (X + rX + 1 <= h - lX ? max(0, r[X + rX + 1][min(Y + rY, w - lY)] - r[X + rX + 1][max(Y - lY, rY + 1) - 1]) : 0));
if (lY < Y - 1)
chkmax(f[cX][cY], dfs(lX, rX, lY + 1, rY) + (rY + 1 <= Y - lY - 1 ? max(0, c[min(X + rX, h - lX)][Y - lY - 1] - c[max(X - lX, rX + 1) - 1][Y - lY - 1]) : 0));
if (rY < w - Y)
chkmax(f[cX][cY], dfs(lX, rX, lY, rY + 1) + (Y + rY + 1 <= w - lY ? max(0, c[min(X + rX, h - lX)][Y + rY + 1] - c[max(X - lX, rX + 1) - 1][Y + rY + 1]) : 0));
return f[cX][cY];
}
int main() {
scanf("%d%d", &h, &w);
for (int i = 1; i <= h; i++) scanf("%s", s[i] + 1);
for (int i = 1; i <= h; i++)
for (int j = 1; j <= w; j++)
if (s[i][j] == 'E') X = i, Y = j;
for (int i = 1; i <= h; i++)
for (int j = 1; j <= w; j++) {
r[i][j] = r[i][j - 1] + (s[i][j] == 'o');
c[i][j] = c[i - 1][j] + (s[i][j] == 'o');
}
memset(f, -1, sizeof(f));
printf("%d\n", dfs(0, 0, 0, 0));
return 0;
}
| CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include <cstring>
#include <cctype>
#include <cstdio>
inline int read(int f = 1, int x = 0, char ch = ' ')
{
while(!isdigit(ch = getchar())) if(ch == '-') f = -1;
while(isdigit(ch)) x = x*10+ch-'0', ch = getchar();
return f*x;
}
const int N = 1e2+5;
int max(int a, int b) { return a>b?a:b; }
int min(int a, int b) { return a<b?a:b; }
short f[N][N][N][N], g[N][N], h[N][N];
char s[N][N];
int n, m, ans, px, py;
int main()
{
n = read(), m = read(); memset(f, -1, sizeof(f));
for(int i = 1; i <= n; ++i) scanf("%s", s[i]+1);
for(int i = 1; i <= n; ++i)
for(int j = 1; j <= m; ++j)
{
if(s[i][j] == 'E') px = i, py = j;
g[i][j] = g[i][j-1]+(s[i][j] == 'o');
h[i][j] = h[i-1][j]+(s[i][j] == 'o');
}
f[0][0][0][0] = 0;
int pl = py-1, pr = m-py, pd = px-1, pu = n-px, p;
for(int l = 0; l <= pl; ++l)
for(int r = 0; r <= pr; ++r)
for(int d = 0; d <= pd; ++d)
for(int u = 0; u <= pu; ++u)
{
if(!(~f[l][r][d][u])) continue;
int cl = max(r+1, py-l), cr = min(m-l, py+r);
int cd = max(u+1, px-d), cu = min(n-d, px+u);
if((p = py+r+1) <= m-l) f[l][r+1][d][u] = max(f[l][r+1][d][u], f[l][r][d][u]+h[cu][p]-h[cd-1][p]);
if((p = py-l-1) >= r+1) f[l+1][r][d][u] = max(f[l+1][r][d][u], f[l][r][d][u]+h[cu][p]-h[cd-1][p]);
if((p = px+u+1) <= n-d) f[l][r][d][u+1] = max(f[l][r][d][u+1], f[l][r][d][u]+g[p][cr]-g[p][cl-1]);
if((p = px-d-1) >= u+1) f[l][r][d+1][u] = max(f[l][r][d+1][u], f[l][r][d][u]+g[p][cr]-g[p][cl-1]);
ans = max(ans, f[l][r][d][u]);
}
printf("%d\n", ans);
return 0;
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<iostream>
#include<cstdio>
#define N (100+5)
using namespace std;
short n,m,line[N][N],list[N][N],f[N][N][N][N],sx,sy,ans;
char a[N][N];
int main()
{
cin>>n>>m;
for (short i=1; i<=n; ++i)
scanf("%s",a[i]+1);
for (short i=1; i<=n; ++i)
for (short j=1; j<=m; ++j)
{
line[i][j]=line[i][j-1]+(a[i][j]=='o');
list[i][j]=list[i-1][j]+(a[i][j]=='o');
if (a[i][j]=='E') sx=i,sy=j;
}
for (short i=sx; i>=1; --i)
for (short j=sy; j>=1; --j)
for (short k=sx; k<=n; ++k)
for (short l=sy; l<=m; ++l)
{
if (i>1 && k-sx<i-1)
f[i-1][j][k][l]=max((int)f[i-1][j][k][l],f[i][j][k][l]+line[i-1][min((int)l,m-sy+j)]-line[i-1][max(j-1,l-sy)]);
if (k<n && sx+k<n+i)
f[i][j][k+1][l]=max((int)f[i][j][k+1][l],f[i][j][k][l]+line[k+1][min((int)l,m-sy+j)]-line[k+1][max(j-1,l-sy)]);
if (j>1 && l-sy<j-1)
f[i][j-1][k][l]=max((int)f[i][j-1][k][l],f[i][j][k][l]+list[min((int)k,n-sx+i)][j-1]-list[max(i-1,k-sx)][j-1]);
if (l<m && sy+l<m+j)
f[i][j][k][l+1]=max((int)f[i][j][k][l+1],f[i][j][k][l]+list[min((int)k,n-sx+i)][l+1]-list[max(i-1,k-sx)][l+1]);
short now=max(max(f[i-1][j][k][l],f[i][j][k+1][l]),max(f[i][j-1][k][l],f[i][j][k][l+1]));
ans=max(ans,now);
}
cout<<ans;
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | #include<bits/stdc++.h>
#define N 105
using namespace std;
#define X(x,u,v) (b[x][v]-b[x][(u)-1])
#define Y(x,u,v) (a[v][x]-a[(u)-1][x])
int n,m,u,v;short dp[N][N][N][N],a[N][N],b[N][N],Ans;
char s[N];
void upd(short &x,short y)
{
x=max(x,y);
}
int main()
{
scanf("%d%d",&n,&m);
for (int i=1;i<=n;i++)
{
scanf("%s",s+1);
for (int j=1;j<=m;j++)
{
if (s[j]=='E') u=i,v=j;
if (s[j]=='o') a[i][j]=b[i][j]=1;
a[i][j]+=a[i-1][j];
b[i][j]+=b[i][j-1];
}
}
/*
dp[x1][x2][y1][y2] ->
*/
for (int i=0;i<n;i++)
for (int j=0;j<m;j++)
{
for (int x1=1;x1+i<=n;x1++)
for (int y1=1;y1+j<=m;y1++)
{
int x2=x1+i,y2=y1+j;
short tmp=dp[x1][x2][y1][y2];
/*if (x1+(n-u)<x2||x2-(u-1)>x1||y1+(m-v)<y2||y2-(v-1)>y1)continue;
cout<<x1<<' '<<x2<<' '<<y1<<' '<<y2<<endl;*/
upd(Ans,dp[x1][x2][y1][y2]);
int _x1=max(1,x2-(u-1)),_x2=min(n,x1+(n-u)),_y1=max(1,y2-(v-1)),_y2=min(m,y1+(m-v));
//_x1=min()
if (_x1>_x2||_y1>_y2)continue;
if (x1>1&&_x1<x1) upd(dp[x1-1][x2][y1][y2],tmp+X(x1-1,max(y1,_y1),min(y2,_y2)));
if (x2<n&&_x2>x2) upd(dp[x1][x2+1][y1][y2],tmp+X(x2+1,max(y1,_y1),min(y2,_y2)));
if (y1>1&&_y1<y1) upd(dp[x1][x2][y1-1][y2],tmp+Y(y1-1,max(x1,_x1),min(x2,_x2)));
if (y2<m&&_y2>y2) upd(dp[x1][x2][y1][y2+1],tmp+Y(y2+1,max(x1,_x1),min(x2,_x2)));
}
}
printf("%d\n",(int)Ans);
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | //khodaya khodet komak kon
#include <bits/stdc++.h>
#define F first
#define S second
#define pb push_back
#define all(x) x.begin(), x.end()
#pragma GCC optimize("Ofast,no-stack-protector,unroll-loops,fast-math")
#define int long long
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
const int N = 105 + 10;
const ll MOD = 1000000000 + 7;
const ll INF = 1000000010;
const ll LOG = 25;
int n, m, px, py, A[N][N], B[N][N], C[N][N];
char s[N][N];
int f[N][N][N];
int32_t main(){
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i) {
scanf("%s", s[i] + 1);
for (int j = 1; j <= m; ++j) {
A[i][j] = s[i][j] == 'o';
B[i][j] = B[i][j - 1] + A[i][j];
C[i][j] = C[i - 1][j] + A[i][j];
if (s[i][j] == 'E') px = i, py = j;
}
}
for (int u = 0; u <= px - 1; ++u) {
for (int d = 0; d <= n - px; ++d) {
for (int l = 0; l <= py - 1; ++l) {
for (int r = 0; r <= m - py; ++r) {
int tu = max(px - u, 1 + d);
int td = min(px + d, n - u);
int tl = max(py - l, 1 + r);
int tr = min(py + r, m - l);
if (u && px - u == tu && tl <= tr) f[d][l][r] += B[px - u][tr] - B[px - u][tl - 1];
if (d) f[d][l][r] = max(f[d][l][r], f[d - 1][l][r] + (px + d == td && tl <= tr ? B[px + d][tr] - B[px + d][tl - 1] : 0));
if (l) f[d][l][r] = max(f[d][l][r], f[d][l - 1][r] + (py - l == tl && tu <= td ? C[td][py - l] - C[tu - 1][py - l] : 0));
if (r) f[d][l][r] = max(f[d][l][r], f[d][l][r - 1] + (py + r == tr && tu <= td ? C[td][py + r] - C[tu - 1][py + r] : 0));
}
}
}
}
printf("%d\n", f[n - px][py - 1][m - py]);
return 0;
} | CPP |
p04009 AtCoder Grand Contest 004 - Salvage Robots | We have a grid with H rows and W columns. The state of the cell at the i-th (1≤i≤H) row and j-th (1≤j≤W) column is represented by a letter a_{ij}, as follows:
* `.` : This cell is empty.
* `o` : This cell contains a robot.
* `E` : This cell contains the exit. `E` occurs exactly once in the whole grid.
Snuke is trying to salvage as many robots as possible, by performing the following operation some number of times:
* Select one of the following directions: up, down, left, right. All remaining robots will move one cell in the selected direction, except when a robot would step outside the grid, in which case the robot will explode and immediately disappear from the grid. If a robot moves to the cell that contains the exit, the robot will be salvaged and immediately removed from the grid.
Find the maximum number of robots that can be salvaged.
Constraints
* 2≤H,W≤100
* a_{ij} is `.`, `o` or `E`.
* `E` occurs exactly once in the whole grid.
Input
The input is given from Standard Input in the following format:
H W
a_{11}...a_{1W}
:
a_{H1}...a_{HW}
Output
Print the maximum number of robots that can be salvaged.
Examples
Input
3 3
o.o
.Eo
ooo
Output
3
Input
2 2
E.
..
Output
0
Input
3 4
o...
o...
oooE
Output
5
Input
5 11
ooo.ooo.ooo
o.o.o...o..
ooo.oE..o..
o.o.o.o.o..
o.o.ooo.ooo
Output
12 | 6 | 0 | import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.InputMismatchException;
public class Main {
static InputStream is;
static PrintWriter out;
static String INPUT = "";
static void solve()
{
int n = ni(), m = ni();
char[][] map = nm(n,m);
int er = -1, ec = -1;
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
if(map[i][j] == 'E'){
er = i; ec = j;
}
}
}
int[][] cum = new int[n+1][m+1];
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
cum[i+1][j+1] = cum[i+1][j] + cum[i][j+1] - cum[i][j] + (map[i][j] == 'o' ? 1 : 0);
}
}
int[][][] pre = new int[n][m][m];
int[][][] cur = new int[n][m][m];
for(int h = 1;h <= n;h++){
for(int i = 0;i < n;i++){
for(int j = 0;j < m;j++){
Arrays.fill(cur[i][j], -99999999);
}
}
for(int w = 1;w <= m;w++){
for(int i = 0;i+h-1 < n;i++){
for(int j = 0;j+w-1 < m;j++){
if(i <= er && er < i+h && j <= ec && ec < j+w){
int k = i+h-1, l = j+w-1;
if(h == 1 && w == 1){
cur[i][j][l] = 0;
continue;
}
// (r1,c1,r2,c2)
// ((r2-er),(c2-ec), n-1-(er-r1), m-1-(ec-c1))
if(i+1 <= k){
cur[i][j][l] = Math.max(cur[i][j][l], pre[i+1][j][l]
+ get(Math.max(i, k-er), Math.max(j, l-ec), Math.min(i, n-1-(er-(i+1))), Math.min(l, m-1-(ec-j)), cum));
}
if(i <= k-1){
cur[i][j][l] = Math.max(cur[i][j][l], pre[i][j][l]
+ get(Math.max(k, k-1-er), Math.max(j, l-ec), Math.min(k, n-1-(er-i)), Math.min(l, m-1-(ec-j)), cum));
}
if(j+1 <= l){
cur[i][j][l] = Math.max(cur[i][j][l], cur[i][j+1][l]
+ get(Math.max(i, k-er), Math.max(j, l-ec), Math.min(k, n-1-(er-i)), Math.min(j, m-1-(ec-(j+1))), cum));
}
if(j <= l-1){
cur[i][j][l] = Math.max(cur[i][j][l], cur[i][j][l-1]
+ get(Math.max(i, k-er), Math.max(l, (l-1)-ec), Math.min(k, n-1-(er-i)), Math.min(l, m-1-(ec-j)), cum));
}
}
}
}
}
int[][][] dum = pre; pre = cur; cur = dum;
}
out.println(pre[0][0][m-1]);
}
static int get(int r1, int c1, int r2, int c2, int[][] cum)
{
if(r1 > r2 || c1 > c2)return 0;
return cum[r2+1][c2+1] - cum[r1][c2+1] - cum[r2+1][c1] + cum[r1][c1];
}
public static void main(String[] args) throws Exception
{
long S = System.currentTimeMillis();
is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes());
out = new PrintWriter(System.out);
solve();
out.flush();
long G = System.currentTimeMillis();
tr(G-S+"ms");
}
private static boolean eof()
{
if(lenbuf == -1)return true;
int lptr = ptrbuf;
while(lptr < lenbuf)if(!isSpaceChar(inbuf[lptr++]))return false;
try {
is.mark(1000);
while(true){
int b = is.read();
if(b == -1){
is.reset();
return true;
}else if(!isSpaceChar(b)){
is.reset();
return false;
}
}
} catch (IOException e) {
return true;
}
}
private static byte[] inbuf = new byte[1024];
static int lenbuf = 0, ptrbuf = 0;
private static int readByte()
{
if(lenbuf == -1)throw new InputMismatchException();
if(ptrbuf >= lenbuf){
ptrbuf = 0;
try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); }
if(lenbuf <= 0)return -1;
}
return inbuf[ptrbuf++];
}
private static boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); }
// private static boolean isSpaceChar(int c) { return !(c >= 32 && c <= 126); }
private static int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; }
private static double nd() { return Double.parseDouble(ns()); }
private static char nc() { return (char)skip(); }
private static String ns()
{
int b = skip();
StringBuilder sb = new StringBuilder();
while(!(isSpaceChar(b))){
sb.appendCodePoint(b);
b = readByte();
}
return sb.toString();
}
private static char[] ns(int n)
{
char[] buf = new char[n];
int b = skip(), p = 0;
while(p < n && !(isSpaceChar(b))){
buf[p++] = (char)b;
b = readByte();
}
return n == p ? buf : Arrays.copyOf(buf, p);
}
private static char[][] nm(int n, int m)
{
char[][] map = new char[n][];
for(int i = 0;i < n;i++)map[i] = ns(m);
return map;
}
private static int[] na(int n)
{
int[] a = new int[n];
for(int i = 0;i < n;i++)a[i] = ni();
return a;
}
private static int ni()
{
int num = 0, b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static long nl()
{
long num = 0;
int b;
boolean minus = false;
while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-'));
if(b == '-'){
minus = true;
b = readByte();
}
while(true){
if(b >= '0' && b <= '9'){
num = num * 10 + (b - '0');
}else{
return minus ? -num : num;
}
b = readByte();
}
}
private static void tr(Object... o) { if(INPUT.length() != 0)System.out.println(Arrays.deepToString(o)); }
}
| JAVA |
Subsets and Splits
No saved queries yet
Save your SQL queries to embed, download, and access them later. Queries will appear here once saved.