10.15 上午 考试
今天的题也很水啊,然而我并没有AK,中间还停了电,唠了会磕
T1
直接模拟
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#define ll long long
#define dis(x,y) (dep[x]+dep[y]-2*dep[LCA(x,y)])
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
inline int read()
{
char q=getchar();int ans=0;
while(q<'0'||q>'9')q=getchar();
while(q>='0'&&q<='9'){ans=ans*10+q-'0';q=getchar();}
return ans;
}
const int LEN=106;
int n;
char s[LEN];
int len;
void read_line()
{
len=0;
char tt=getchar();
while(tt!='\n')
{
if(tt>='0'&&tt<='9')
{
s[++len]='0';
while(tt>='0'&&tt<='9')
tt=getchar();
continue;
}
s[++len]=tt;
tt=getchar();
}
}
int pr[156],nx[150];
inline void del(int x)
{
pr[nx[x]]=pr[x];
nx[pr[x]]=nx[x];
}
int is_fu(char x)
{
if(x=='+'||x=='-'||x=='*'||x=='/')
return 1;
return 0;
}
int check()
{
/*printf("\n");
for(int i=1;i<=len;++i)
printf("%c ",s[i]);
printf("\n");*/
for(int i=1;i<=len;++i)
pr[i]=i-1,nx[i]=i+1;
nx[0]=1;pr[len+1]=len;
int ff=1;
while(ff)
{
ff=0;
for(int i=nx[0];i<=len;i=nx[i])
if(s[i]=='0')
{
while(pr[i]>0&&s[pr[i]]==' ')
ff=1,del(pr[i]);
while(nx[i]<=len&&s[nx[i]]==' ')
ff=1,del(nx[i]);
while(pr[i]>0&&s[pr[i]]=='('&&nx[i]<=len&&s[nx[i]]==')')
ff=1,del(pr[i]),del(nx[i]);
while(nx[i]<=len&&is_fu(s[nx[i]])&&nx[nx[i]]<=len&&s[nx[nx[i]]]=='0')
ff=1,del(nx[nx[i]]),del(nx[i]);
}
}
int tt=0,all=0;
//printf("\n");
for(int i=nx[0];i<=len;i=nx[i])
{
++all;
//printf("%c ",s[i]);
if(s[i]=='0')
++tt;
}
//printf("tt=%d\n",tt);
if(all==1&&tt==1)
return 1;
return 0;
}
int main(){
//freopen("T1.in","r",stdin);
n=read();
for(int i=1;i<=n;++i)
{
read_line();
if(check())
printf("Yes\n");
else
printf("No\n");
}
}
T2
hash表+拓扑序
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#define ull unsigned long long
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int N=100006;
const int LEN=76;
const int P1=76543;
const int P2=151651;
const int MOD=1000003;
const int mod=1e9+7;
struct son
{
int v,next;
}a1[N*5];
int first[MOD],e;
void addbian(int u,int v)
{
a1[e].v=v;
a1[e].next=first[u];
first[u]=e++;
}
struct son1
{
int next,order;
ull ha1,ha2;
};
struct HASH
{
son1 a3[N*5];
int first[MOD],e,temp;
void clear()
{
mem(a3,0);
mem(first,-1);
e=0;
}
int checkifin(ull x1,ull x2)
{
temp=x1%MOD;
for(int i=first[temp];i!=-1;i=a3[i].next)
if(a3[i].ha1==x1&&a3[i].ha2==x2)
return a3[i].order;
return -1;
}
void add(ull x1,ull x2,int order)
{
temp=x1%MOD;
a3[e].order=order;
a3[e].ha1=x1;
a3[e].ha2=x2;
a3[e].next=first[temp];
first[temp]=e++;
}
}H;
int m,con;
char s1[LEN],s2[LEN];
int ind[N];
int len1,len2;
int get1()
{
int t1,ans;
ull x1=0,x2=0;
len1=strlen(s1);
for(int i=0;i<len1;++i)
{
x1=(x1+s1[i])*P1;
x2=(x2+s1[i])*P2;
}
t1=H.checkifin(x1,x2);
if(t1==-1)
{
ans=++con;
H.add(x1,x2,con);
}
else
ans=t1;
return ans;
}
int get2()
{
int t1,ans;
ull x1=0,x2=0;
len2=strlen(s2);
for(int i=0;i<len2;++i)
{
x1=(x1+s2[i])*P1;
x2=(x2+s2[i])*P2;
}
t1=H.checkifin(x1,x2);
if(t1==-1)
{
ans=++con;
H.add(x1,x2,con);
}
else
ans=t1;
return ans;
}
int dui[N*10],he,en;
ll num[N];
ll work()
{
ll ans=0;
mem(num,0);
he=1;en=0;
for(int i=1;i<=con;++i)
if(!ind[i])
{
num[i]=1;
dui[++en]=i;
}
int now,temp;
while(en>=he)
{
now=dui[he++];
for(int i=first[now];i!=-1;i=a1[i].next)
{
temp=a1[i].v;
--ind[temp];
num[temp]+=num[now];num[temp]%=mod;
if(!ind[temp])
dui[++en]=temp;
}
if(first[now]==-1)
ans+=num[now],ans%=mod;
}
return ans%mod;
}
int main(){
//freopen("T2.in","r",stdin);
H.clear();
mem(first,-1);
scanf("%d",&m);
int t1,t2;
for(int i=1;i<=m;++i)
{
scanf("%s%s",s1,s2);
t1=get1();t2=get2();
//printf("i=%d t1=%d t2=%d\n",i,t1,t2);
addbian(t1,t2);
++ind[t2];
}
cout<<work();
}
T3
$ f_i $ 为从(0,0)到第i个被淹的路口且不经过前面被淹的路口的方案数
$$ f_i=C_{x_i+y_i}^{x_i} - \sum_{x_ j<=x_i,y_ j<=y_i}C_{x_ i-x_ j+y_i-y_ j}^{x_i-x_ j}f_ j $$
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
#include <cmath>
#define ull unsigned long long
#define ll long long
#define mem(a,b) memset(a,b,sizeof(a))
using namespace std;
const int N=100006;
const int LEN=76;
const int P1=76543;
const int P2=151651;
const int MOD=1000003;
const int mod=1e9+7;
struct son
{
int v,next;
}a1[N*5];
int first[MOD],e;
void addbian(int u,int v)
{
a1[e].v=v;
a1[e].next=first[u];
first[u]=e++;
}
struct son1
{
int next,order;
ull ha1,ha2;
};
struct HASH
{
son1 a3[N*5];
int first[MOD],e,temp;
void clear()
{
mem(a3,0);
mem(first,-1);
e=0;
}
int checkifin(ull x1,ull x2)
{
temp=x1%MOD;
for(int i=first[temp];i!=-1;i=a3[i].next)
if(a3[i].ha1==x1&&a3[i].ha2==x2)
return a3[i].order;
return -1;
}
void add(ull x1,ull x2,int order)
{
temp=x1%MOD;
a3[e].order=order;
a3[e].ha1=x1;
a3[e].ha2=x2;
a3[e].next=first[temp];
first[temp]=e++;
}
}H;
int m,con;
char s1[LEN],s2[LEN];
int ind[N];
int len1,len2;
int get1()
{
int t1,ans;
ull x1=0,x2=0;
len1=strlen(s1);
for(int i=0;i<len1;++i)
{
x1=(x1+s1[i])*P1;
x2=(x2+s1[i])*P2;
}
t1=H.checkifin(x1,x2);
if(t1==-1)
{
ans=++con;
H.add(x1,x2,con);
}
else
ans=t1;
return ans;
}
int get2()
{
int t1,ans;
ull x1=0,x2=0;
len2=strlen(s2);
for(int i=0;i<len2;++i)
{
x1=(x1+s2[i])*P1;
x2=(x2+s2[i])*P2;
}
t1=H.checkifin(x1,x2);
if(t1==-1)
{
ans=++con;
H.add(x1,x2,con);
}
else
ans=t1;
return ans;
}
int dui[N*10],he,en;
ll num[N];
ll work()
{
ll ans=0;
mem(num,0);
he=1;en=0;
for(int i=1;i<=con;++i)
if(!ind[i])
{
num[i]=1;
dui[++en]=i;
}
int now,temp;
while(en>=he)
{
now=dui[he++];
for(int i=first[now];i!=-1;i=a1[i].next)
{
temp=a1[i].v;
--ind[temp];
num[temp]+=num[now];num[temp]%=mod;
if(!ind[temp])
dui[++en]=temp;
}
if(first[now]==-1)
ans+=num[now],ans%=mod;
}
return ans%mod;
}
int main(){
//freopen("T2.in","r",stdin);
H.clear();
mem(first,-1);
scanf("%d",&m);
int t1,t2;
for(int i=1;i<=m;++i)
{
scanf("%s%s",s1,s2);
t1=get1();t2=get2();
//printf("i=%d t1=%d t2=%d\n",i,t1,t2);
addbian(t1,t2);
++ind[t2];
}
cout<<work();
}