过题:3
排名:363
1001 ( Calculus )
CY
#include <bits/stdc++.h>
typedef long long ll;
const int MAXN = 1e2 + 10;
char s[MAXN], temp[MAXN], cnt;
ll vis[8];
int main()
{
int t; scanf("%d", &t);
while (t--)
{
memset(vis, 0, sizeof vis);
scanf("%s", s + 1); s[0] = '+';
int n = strlen(s);
s[n++] = '+';
int p = 1, prv = 0;
while (p < n)
{
if (s[p] == '+')
{
int i, tot = 0;
for (i = prv + 1; i < p; ++i)
{
if (s[i] >= '0' && s[i] <= '9') tot = tot * 10 + s[i] - '0';
else break;
}
memset(temp, 0, sizeof temp);
cnt = 0;
for (cnt = 0 ; i < p; ++i)
temp[cnt++] = s[i];
if (strcmp(temp, "/x") == 0) vis[1] += tot;
else if (strcmp(temp, "sinx") == 0) vis[2] += tot;
else if (strcmp(temp, "cosx") == 0) vis[3] += tot;
else if (strcmp(temp, "/sinx") == 0) vis[4] += tot;
else if (strcmp(temp, "/cosx") == 0) vis[5] += tot;
else if (strcmp(temp, "x") == 0) vis[6] += tot;
else if (strcmp(temp, "^x") == 0) vis[7] = std::max(vis[7], 1ll * tot);
else vis[0] += tot;
prv = p;
}
++p;
}
if (!vis[0] && !vis[1] && !vis[2] && !vis[3] && !vis[4] && !vis[5] && !vis[6] && !vis[7]) printf("YES\n");
else printf("NO\n");
}
return 0;
}
1002 ( Kanade Loves Maze Designing )
WYX
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn=2e3+10;
const int p=1e9+7;
const int p1=1e9+9;
ll qpow(ll a,ll n,ll p)
{
ll ans=1;
while(n)
{
if(n&1) ans=ans*a%p;
n>>=1;
a=a*a%p;
}
return ans;
}
vector<int>G[maxn];
void add(int x,int y)
{
G[x].push_back(y);
G[y].push_back(x);
}
int mp[maxn],cnt=0;
int a[maxn][maxn],b[maxn];
void dfs(int u,int fa,int id)
{
if(mp[b[u]]==0) cnt++;
mp[b[u]]++;
a[id][u]=cnt;
for(auto v:G[u])
{
if(v==fa) continue;
dfs(v,u,id);
}
mp[b[u]]--;
if(mp[b[u]]==0) cnt--;
}
ll a1[maxn],a2[maxn];
int main()
{
// ios_base::sync_with_stdio(false);
// cin.tie(nullptr);
// cout.tie(nullptr);
for(int i=0;i<=2000;i++)
a1[i]=qpow(19560929,i,p)%p,a2[i]=qpow(19560929,i,p1)%p1;
int t;scanf("%d",&t);
while(t--)
{
int n;scanf("%d",&n);
for(int i=1;i<=n;i++)
G[i].clear();
for(int i=2;i<=n;i++)
{
int x;scanf("%d",&x);
add(i,x);
}
for(int i=1;i<=n;i++)
scanf("%d",&b[i]);
for(int i=1;i<=n;i++)
{
cnt=0;
dfs(i,i,i);
}
for(int i=1;i<=n;i++)
{
ll ans=0;
for(int j=1;j<=n;j++)
ans=(ans+a[i][j]*a1[j-1])%p;
printf("%lld ",ans);
ans=0;
for(int j=1;j<=n;j++)
ans=(ans+a[i][j]*a2[j-1])%p1;
printf("%lld\n",ans);
}
}
return 0;
}
1009 ( License Plate Recognition )
CY
#include <bits/stdc++.h>
typedef long long ll;
typedef std::pair < int, int > pii;
const int MAXN = 1e2 + 10;
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
char s[MAXN][MAXN];
bool vis[MAXN][MAXN];
std::vector < pii > ans;
pii dfs(int x, int y)
{
vis[x][y] = 1;
pii ans = {y, y};
for (int i = 0; i < 4; ++i)
{
int xx = x + dx[i], yy = y + dy[i];
if (xx > 0 && xx <= 30 && yy > 0 && yy <= 100 && s[xx][yy] == '#' && !vis[xx][yy])
{
auto u = dfs(xx, yy);
ans.first = std::min(ans.first, u.first);
ans.second = std::max(ans.second, u.second);
}
}
return ans;
}
int main()
{
int t; scanf("%d", &t);
for (int tt = 1; tt <= t; ++tt)
{
memset(vis, 0, sizeof vis);
ans.clear();
for (int i = 1; i <= 30; ++i)
scanf("%s", s[i] + 1);
for (int i = 1; i <= 30; ++i)
for (int j = 1; j <= 100; ++j)
if (!vis[i][j] && s[i][j] == '#') ans.push_back(dfs(i, j));
std::sort(ans.begin(), ans.end());
std::reverse(ans.begin(), ans.end());
while (ans.size() > 7)
{
auto u = ans.back(); ans.pop_back();
auto v = ans.back(); ans.pop_back();
ans.push_back({std::min(u.first, v.first), std::max(u.second, v.second)});
}
std::reverse(ans.begin(), ans.end());
printf("Case #%d:\n", tt);
for (auto &i : ans)
printf("%d %d\n", i.first, i.second);
}
return 0;
}