【补题】第十七届同济大学友谊赛
先放代码,题解日后再更。
A.张老师和菜哭武的游戏
Code
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
#define RG register int
#define LL long long
LL GCD(LL a,LL b){return b==0?a:GCD(b,a%b);}
int T;
LL N,A,B;
int main(){
cin>>T;
while(T--){
cin>>N>>A>>B;
if((N/GCD(A,B))%2==1) cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}
B.伤害计算
Code
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
#define RG register int
#define LL long long
string S;
double ndx(int n,int x){
return (double)n*(double)((1+x)*x)/2.0/(double)x;
}
int main(){
cin>>S;
double Ans=0;
int a=0,b=0;bool d=false;
for(RG i=0;i<S.size();++i){
if(S[i]=='+'){
if(!d) Ans+=a;
else Ans+=ndx(a,b);
d=false;a=b=0;
}
else if(S[i]=='d') d=true;
else{
if(!d) a=a*10+S[i]-'0';
else b=b*10+S[i]-'0';
}
}
if(!d) Ans+=a;
else Ans+=ndx(a,b);
int temp=Ans;
if(Ans!=temp) printf("%.1f\n",Ans);
else cout<<temp<<endl;
return 0;
}
C.张老师的旅行
Code
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
#define RG register int
#define LL long long
template<typename elemType>
inline void Read(elemType &T){
elemType X=0,w=0; char ch=0;
while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
T=(w?-X:X);
}
LL dp[1005][1005][2];
LL p[1005],t[1005];
int N,st;
int main(){
Read(N);
for(RG i=1;i<=N;++i)
Read(p[i]);
for(RG i=1;i<=N;++i){
Read(t[i]);
if(t[i]==0) st=i;
}
memset(dp,0x3f,sizeof(dp));
dp[st][st][0]=dp[st][st][1]=0;
for(RG Len=2;Len<=N;++Len){
for(RG L=1;L+Len-1<=N;++L){
int R=L+Len-1;
if(dp[L+1][R][0]+p[L+1]-p[L]<=t[L])
dp[L][R][0]=min(dp[L][R][0],dp[L+1][R][0]+p[L+1]-p[L]);
if(dp[L+1][R][1]+p[R]-p[L]<=t[L])
dp[L][R][0]=min(dp[L][R][0],dp[L+1][R][1]+p[R]-p[L]);
if(dp[L][R-1][1]+p[R]-p[R-1]<=t[R])
dp[L][R][1]=min(dp[L][R][1],dp[L][R-1][1]+p[R]-p[R-1]);
if(dp[L][R-1][0]+p[R]-p[L]<=t[R])
dp[L][R][1]=min(dp[L][R][1],dp[L][R-1][0]+p[R]-p[L]);
}
}
LL Ans=min(dp[1][N][0],dp[1][N][1]);
if(Ans>1000000000000LL) printf("-1\n");
else printf("%lld\n",Ans);
return 0;
}
D.车辆调度
Code
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
#define RG register int
#define LL long long
int Car[5][2];
char G[15][15],G2[15][15];
int N,M,K,num=0;
int dx[4]={-1,1,0,0},dy[4]={0,0,-1,1};
bool Ans=false;
int Maintain(int x,int y,int d){
int limit=max(N,M);
int i=1;
for(i=1;i<limit;++i){
int nx=x+dx[d]*i;
int ny=y+dy[d]*i;
if(nx<1||nx>N||ny<1||ny>M) break;
if(G[nx][ny]=='R'||G[nx][ny]=='X') break;
}
return i-1;
}
void DFS(int Step){
if(Ans) return;
if(Step==K){
for(RG i=1;i<=num;++i)
if(G2[Car[i][0]][Car[i][1]]=='D'){Ans=true;break;}
return;
}
for(int i=1;i<=num;++i){
if(Ans) return;
for(int dir=0;dir<4;++dir){
int dis=Maintain(Car[i][0],Car[i][1],dir);
int nx=Car[i][0]+dis*dx[dir];
int ny=Car[i][1]+dis*dy[dir];
int x=Car[i][0],y=Car[i][1];
if(dis==0) continue;
if(G[nx][ny]=='D'){Ans=true;return;}
char temp=G[nx][ny];
G[x][y]=G2[x][y];
G[nx][ny]='R';
Car[i][0]=nx;Car[i][1]=ny;
DFS(Step+1);
G[nx][ny]=temp;
G[x][y]='R';
Car[i][0]=x;Car[i][1]=y;
}
}
return;
}
int main(){
ios::sync_with_stdio(false);
cin>>M>>N>>K;
for(RG i=1;i<=N;++i)
for(RG j=1;j<=M;++j){
cin>>G[i][j];
G2[i][j]=G[i][j];
if(G[i][j]=='R'){G2[i][j]='.';++num;Car[num][0]=i;Car[num][1]=j;}
}
DFS(0);
if(Ans) cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return 0;
}
E.弦
Code
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
using namespace std;
#define RG register int
#define LL long long
LL Inv[20000010];
const LL MOD=1000000007LL;
void Init(){
Inv[1]=1;
for(register int i=2;i<=20000005;++i)
Inv[i]=((-(MOD/i)*Inv[MOD%i])%MOD+MOD)%MOD;
return;
}
LL F(LL n){
LL now=1,last=0;
for(RG i=2;i<=n;i++){
last=now;
now=last*(LL)(4*i-2)%MOD*Inv[i+1]%MOD;
}
return last;
}
LL C2(LL n){
return (2LL*Inv[n]%MOD)*Inv[n-1]%MOD;
}
int main(){
Init();
int n;LL c=1;cin>>n;
LL fact=1LL;
for(RG i=0;i<n;++i){
c=(c*C2(2*n-i*2))%MOD;
fact=(fact*(LL)(i+1))%MOD;
}
LL Ans=(F(n+1)*fact)%MOD*c%MOD;
printf("%lld\n",Ans);
return 0;
}
F.排列计算
Code
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
#define RG register int
#define LL long long
template<typename elemType>
inline void Read(elemType &T){
elemType X=0,w=0; char ch=0;
while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
T=(w?-X:X);
}
struct SegTreeNode{LL Value,Lazytag;};
SegTreeNode SegTree[800010];
int N,M;
inline void Push_Down(int Root,int L,int R){
if(!SegTree[Root].Lazytag) return;
LL Add=SegTree[Root].Lazytag;
SegTree[Root].Lazytag=0;
int mid=(L+R)>>1;
SegTree[Root<<1].Value+=(LL)(mid-L+1)*Add;
SegTree[Root<<1|1].Value+=(LL)(R-mid)*Add;
SegTree[Root<<1].Lazytag+=Add;
SegTree[Root<<1|1].Lazytag+=Add;
return;
}
void Update(int Root,int L,int R,int QL,int QR,LL Add){
if(R<QL||QR<L) return;
if(QL<=L && R<=QR){
SegTree[Root].Value+=(LL)(R-L+1)*Add;
SegTree[Root].Lazytag+=Add;
return;
}
int mid=(L+R)>>1;
Push_Down(Root,L,R);
Update(Root<<1,L,mid,QL,QR,Add);
Update(Root<<1|1,mid+1,R,QL,QR,Add);
SegTree[Root].Value=SegTree[Root<<1].Value+SegTree[Root<<1|1].Value;
return;
}
LL Query(int Root,int L,int R,int QL,int QR){
if(R<QL||QR<L) return 0;
if(QL<=L && R<=QR) return SegTree[Root].Value;
int mid=(L+R)>>1;
Push_Down(Root,L,R);
return Query(Root<<1,L,mid,QL,QR)+Query(Root<<1|1,mid+1,R,QL,QR);
}
vector<int> Ans;
int main(){
Read(N);Read(M);
while(M--){
int L,R;LL Add;
Read(L);Read(R);
Update(1,1,N,L,R,1);
}
for(RG i=1;i<=N;++i)
Ans.push_back(Query(1,1,N,i,i));
sort(Ans.begin(),Ans.end());
LL Res=0;
for(RG i=1;i<=N;++i)
Res+=(LL)Ans[i-1]*(LL)i;
cout<<Res<<endl;
return 0;
}
H.时空栈
Code
#include <iostream>
#include <algorithm>
#include <cstring>
#include <cstdio>
#include <vector>
using namespace std;
#define RG register int
#define LL long long
const int INF=2147483647;
template<typename elemType>
inline void Read(elemType &T){
elemType X=0,w=0; char ch=0;
while(!isdigit(ch)) {w|=ch=='-';ch=getchar();}
while(isdigit(ch)) X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
T=(w?-X:X);
}
int SegTree[800010],val[200010];
int N,Index;
void Insert_Node(int Root,int L,int R,int Pos,int Add){
if(L==R){SegTree[Root]+=Add;return;}
int mid=(L+R)>>1;
if(Pos<=mid) Insert_Node(Root<<1,L,mid,Pos,Add);
else Insert_Node(Root<<1|1,mid+1,R,Pos,Add);
SegTree[Root]=SegTree[Root<<1]+SegTree[Root<<1|1];
return;
}
int Query(int Root,int L,int R,int QL,int QR){
if(R<QL||QR<L) return 0;
if(QL<=L && R<=QR) return SegTree[Root];
int mid=(L+R)>>1;
return Query(Root<<1,L,mid,QL,QR)+Query(Root<<1|1,mid+1,R,QL,QR);
}
int Ask(int Root,int L,int R,int Ed,int Sum){
if(L==R){
if(Sum==1) return L;
return -1;
}
int mid=(L+R)>>1;
if(mid<Ed){
int temp=Ask(Root<<1|1,mid+1,R,Ed,Sum-SegTree[Root<<1]);
if(temp!=-1) return temp;
}
return Ask(Root<<1,L,mid,Ed,Sum);
}
int main(){
Read(N);
for(RG i=1;i<=N;++i){
int opt,Time,Value;
Read(opt);Read(Time);
if(opt==0){Read(Value);val[Time]=Value;Insert_Node(1,1,N,Time,1);}
else if(opt==1) Insert_Node(1,1,N,Time,-1);
else printf("%d\n",val[Ask(1,1,N,Time,Query(1,1,N,1,Time))]);
}
return 0;
}