20180715 考试记录
T1 方程的解
Solution
exgcd求ax+by=c
设方程\(a'x+b'y=c'\)
其中\(a'=a/(a,b)\) ; \(b'=b/(a,b)\) ; \(c'=c/(a,b)\)
我们能求出\(ax'+by'=(a,b)\)的一解
那么因为有\(x=x'*c'\)和\(y=y'*c'\)
便可以求出设方程的一解(这也是原方程ax+by=c的一解)
由于 \(a'(x+t*b')+b'(x-t*a')=c'\) 当t取整数时均成立
那么可以利用\(b'\)求出x的最小整数值,然后求出最大y值
然后用最大y值与最小y值的关系求解的个数
PS:这个题还需要一堆特判,特判不对也可致凉
Code
//By Menteur_Hxy
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cmath>
#define F(i,a,b) for(register int i=(a);i<=(b);i++)
#define R(i,a,b) for(register int i=(b);i>=(a);i--)
using namespace std;
typedef long long LL;
LL rd() {
LL x=0,f=1; char c=getchar();
while(!isdigit(c)) {if(c=='-')f=-f; c=getchar();}
while(isdigit(c)) x=(x<<1)+(x<<3)+c-48,c=getchar();
return x*f;
}
LL T,a,b,c,x,y,d;
LL exgcd(LL a,LL b,LL &x,LL &y) {
if(!b) {x=1,y=0; return a;}
LL t=exgcd(b,a%b,y,x);
y-=a/b*x;
return t;
}
void out(LL x) {
if(x<0||x>65535) puts("ZenMeZheMeDuo");
else printf("%lld\n",x);
}
int main() {
freopen("fuction.in","r",stdin);
freopen("fuction.out","w",stdout);
T=rd();
while(T--) {
a=rd(),b=rd(),c=rd();
if(a<=0&&b<=0) a=-a,b=-b,c=-c;
if(a==0&&b==0) {
if(c) out(0);
else out(-1);
continue;
}
if(a==0) {
if(c%b==0&&c/b>0) out(-1);
else out(0);
continue;
}
if(b==0) {
if(c%a==0&&c/a>0) out(-1);//1
else out(0);
continue;
}
if(a+b==c) {out(1);continue;}
d=exgcd(a,b,x,y);
// cout<<d<<endl;
if(c%d) {out(0);continue;}
if(a*b<0) {out(-1);continue;}
LL a1=a/d,b1=b/d,c1=c/d;
x*=c1,y*=c1;
x%=b1; if(x<=0) x+=b1;
// cout<<a1<<" "<<b1<<" "<<c1<<endl;
y=(c-a*x)/b;
// cout<<x<<" "<<y<<endl;
if(y<0) {out(0);continue;}
// cout<<y<<" "<<y%a1<<" "<<a1<<endl;
LL ans=y/a1-(y%a1==0)+1;
out(ans);
}
}
T2 [luogu3177 HAOI2015]树上染色
Solution
树形dp,统计每一条边对答案的贡献
注意每次计算一条边的贡献时是对于整棵树的
(之前写的是对一个子树,一直不对QAQ)
Code
//By Menteur_Hxy
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define M(a,b) memset(a,(b),sizeof(a))
#define F(i,a,b) for(register int i=(a);i<=(b);i++)
#define R(i,a,b) for(register int i=(b);i>=(a);i--)
#define E(i,u) for(register int i=head[u];i;i=nxt[i])
using namespace std;
typedef long long LL;
LL read() {
LL x=0,f=1; char c=getchar();
while(!isdigit(c)) {if(c=='-')f=-f; c=getchar();}
while(isdigit(c)) x=(x<<1)+(x<<3)+c-48,c=getchar();
return x*f;
}
const int N=2010;
int n,m,cnt;
int nxt[N<<1],to[N<<1],w[N<<1],siz[N],head[N];
LL dp[N][N];
void out(LL x) {printf("%lld ",x);}
void dfs(int u,int pre) {
siz[u]=1;
dp[u][0]=dp[u][1]=0;
E(i,u) { int v=to[i];
if(v==pre) continue;
dfs(v,u);
siz[u]+=siz[v];
}
E(i,u) { int v=to[i];
if(v==pre) continue;
R(j,0,min(siz[u],m)) F(k,0,min(j,siz[v])) if(dp[u][j-k]!=-1) {
LL t=(LL)w[i]*((m-k)*k + (n-m-siz[v]+k)*(siz[v]-k));//1
dp[u][j]=max(dp[u][j],dp[u][j-k]+dp[v][k]+t);
}
}
}
#define add(a,b,c) nxt[++cnt]=head[a],to[cnt]=b,w[cnt]=c,head[a]=cnt
#define insert(a,b,c) add(a,b,c),add(b,a,c)
int main() {
n=read(),m=read();
F(i,1,n-1) {
int a=read(),b=read(),c=read();
insert(a,b,c);
}
M(dp,-1);
dfs(1,0);
printf("%lld",dp[1][m]);
return 0;
}
T3 CF274E Mirror Room
Solution
暴力70分
正解是在实心块上每次二分找到下一次到达的地方
Code
暴力70分:
//By Menteur_Hxy
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<set>
#include<map>
#include<algorithm>
#define M(a,b) memset(a,(b),sizeof(a))
#define F(i,a,b) for(register int i=(a);i<=(b);i++)
#define R(i,a,b) for(register int i=(b);i>=(a);i--)
#define E(i,u) for(register int i=head[u];i;i=nxt[i])
using namespace std;
typedef long long LL;
LL read() {
LL x=0,f=1; char c=getchar();
while(!isdigit(c)) {if(c=='-')f=-f; c=getchar();}
while(isdigit(c)) x=(x<<1)+(x<<3)+c-48,c=getchar();
return x*f;
}
typedef pair<int,int> PII;
#define x first
#define y second
int n,m,k,ans,x,y;
set<PII> block,empty;
map<PII,set<PII> > vis;
PII s,t,dir;
char ch[5];
int main() {
// freopen("ray.in","r",stdin);
// freopen("ray.out","w",stdout);
n=read(),m=read(),k=read();
F(i,0,n+1) block.insert(PII(i,0)),block.insert(PII(i,m+1));
F(i,0,m+1) block.insert(PII(0,i)),block.insert(PII(n+1,i));
F(i,1,k) {
int a=read(),b=read();
block.insert(PII(a,b));
}
s.x=read(),s.y=read();
scanf("%s",ch+1);
dir.x=(ch[1]=='N'?-1:1); dir.y=(ch[2]=='W'?-1:1);
while(1) {
if(empty.insert(s).y) ans++;
t=PII(s.x+dir.x,s.y+dir.y);
if(!block.count(t)) {s=t;continue;}
if(!vis[t].insert(dir).y) break;
x=block.count(PII(t.x-dir.x,t.y)),y=block.count(PII(t.x,t.y-dir.y));
if(x==y) break;
else if(x) s.x+=dir.x,dir.y=-dir.y;
else s.y+=dir.y,dir.x=-dir.x;
}
printf("%d",ans);
return 0;
}
满分:留坑