# Codeforces Round #607 (Div. 1)

## A. Cut and Paste

#### 题解

（我之前是对于每一次复制前都会暴力复制一份复制内容，但是发现会被全是1的情况卡爆，因为如果是1的话就不用复制）

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
#include<string>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long LL;
typedef pair<int,int> PII;
{
int x=0,f=1;char c=getchar();
while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){x=x*10+c-'0';c=getchar();}
return x*f;
}
const int MOD=1000000007;
int T,x;
char s[3000010],tmp[3000010];
int main()
{
while(T--)
{
scanf("%s",s);
int len=strlen(s);
int i=1;
while(len<=x && i<=x)
{
int tmp=len;
for(int j=1;j<s[i-1]-'0';j++)
for(int k=i;k<tmp;k++)s[len++]=s[k];
i++;
}
if(i==x+1){printf("%d\n",len%MOD);continue;}
int ans=len;
while(i<=x)ans=(ans+((ans-i)*(s[i-1]-'0'-1)%MOD+MOD)%MOD)%MOD,i++;
printf("%d\n",ans);
}
return 0;
}


## B. Beingawesomeism

#### 题解

#include<iostream>
#include<algorithm>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std;
#define mem(a,b) memset(a,b,sizeof(a))
typedef long long LL;
typedef pair<int,int> PII;
{
int x=0,f=1;char c=getchar();
while(!isdigit(c)){if(c=='-')f=-1;c=getchar();}
while(isdigit(c)){x=x*10+c-'0';c=getchar();}
return x*f;
}
int T;
int n,m;
char pic[100][100];
bool check1()
{
bool ok1=0;
for(int i=0;i<n;i++)if(pic[i][0]=='P')ok1=1;
if(!ok1)return 1;
bool ok2=0;
for(int i=0;i<n;i++)if(pic[i][m-1]=='P')ok2=1;
if(!ok2)return 1;
bool ok3=0;
for(int i=0;i<m;i++)if(pic[0][i]=='P')ok3=1;//printf("ok:%d\n",ok1);
if(!ok3)return 1;
bool ok4=0;
for(int i=0;i<m;i++)if(pic[n-1][i]=='P')ok4=1;//	printf("ok:%d\n",ok1);
if(!ok4)return 1;
return 0;
}
bool check2()
{
if(pic[0][0]=='A')return 1;
if(pic[0][m-1]=='A')return 1;
if(pic[n-1][0]=='A')return 1;
if(pic[n-1][m-1]=='A')return 1;
for(int i=1;i<n-1;i++)
{
bool ok=0;
for(int j=0;j<m;j++)
if(pic[i][j]=='P')ok=1;
if(!ok)return 1;
}
for(int i=1;i<m-1;i++)
{
bool ok=0;
for(int j=0;j<n;j++)
if(pic[j][i]=='P')ok=1;
if(!ok)return 1;
}

return 0;
}
bool check3()
{
bool ok1=0;
for(int i=1;i<m-1;i++)if(pic[0][i]=='A')ok1=1;
if(ok1)return 1;
bool ok2=0;
for(int i=1;i<m-1;i++)if(pic[n-1][i]=='A')ok2=1;
if(ok2)return 1;
bool ok3=0;
for(int i=1;i<n-1;i++)if(pic[i][0]=='A')ok3=1;
if(ok3)return 1;
bool ok4=0;
for(int i=1;i<n-1;i++)if(pic[i][m-1]=='A')ok4=1;
if(ok4)return 1;
return 0;
}
int main()
{
while(T--)
{
for(int i=0;i<n;i++)scanf("%s",pic[i]);
bool flag=0,flag2=0;
for(int i=0;i<n;i++)
for(int j=0;j<m;j++)
{
if(pic[i][j]=='A')flag=1;
if(pic[i][j]=='P')flag2=1;
}
if(!flag)printf("MORTAL\n");
else if(!flag2)printf("0\n");
else if(check1())printf("1\n");
else if(check2())printf("2\n");
else if(check3())printf("3\n");
else printf("4\n");
}
return 0;
}


## C. Jeremy Bearimy

#### 题解

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define ll long long
using namespace std;
{
int k=0,f=1;char c=getchar();
for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;
for(;isdigit(c);c=getchar()) k=k*10+c-'0';return k*f;
}
const int N=400055;
ll ans1,ans2;
{
to[++tot]=b;
w[tot]=c;
}
void dfs1(int u,int f)
{
size[u]=1;
if(to[i]!=f)
{
dfs1(to[i],u);size[u]+=size[to[i]];
if(size[to[i]]&1) ans1+=w[i];
ans2+=1ll*min(size[to[i]],n-size[to[i]])*w[i];
}
}
int main()
{
while(T--)
{
int a,b,c;
ans1=ans2=tot=0;
for(int i=1;i<n;i++)
{