1. 程式人生 > 其它 >AcWing 第15場周賽

AcWing 第15場周賽

青蛙跳

+a的次數=k%2+k/2
-b的次數=k/2

注意資料不要爆範圍了

#include<iostream>
#include<cstring>
#include<cstdio>

using namespace std;
int t;
int k,a,b;
int main(){
	cin>>t;
	while(t--){
		cin>>a>>b>>k;
	    cout<<(long long)(a-b)*(k/2)+(k%2)*a<<endl;
	}return 0;
}

最小正整數

設所求答案為x
\(n|x\) \(10^k|x\)
又因為x要為最小正整數

所以可以求lcm(n,10^k)

#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
using namespace std;
int n,k,t;
long long gcd(long long a,long long b){
    return b==0?a:gcd(b,a%b);
}
int main(){
	cin>>t;
	while(t--){
	    cin>>n>>k;
	    int m=pow(10,k);
	    cout<<(long long)n*m/gcd(n,m)<<endl;
	}	return 0;
} 

行走路徑

讀題時以為很難結果自己嚇自己就....

其實此題與滑雪這道題很相似(記憶化搜尋)

只需要加以修改

問的QWER經歷的最多,所以求的是最長路徑

三種情況

  1. infinity 有環
  2. none 即ans=0
  3. ans

判環的方法
1.強連通分量
2.topsort
3.dfs
4.spfa(it died)

(事先把QWER的位置換為0,1,2,3)
我們列舉每個(i,j)

以i,j為起點,找最長路,dfs判斷有沒有環

假設求出的最長路的長度為t
則QWER出現的次數
此時如果起點為\(mp[i][j]\)
為1,次數為\(\lfloor {\frac{t}{4} }\rfloor\)
為2,次數為\(\lfloor \frac{t-3}{4} \rfloor\)


為3,次數為\(\lfloor{\frac{t-2}{4} }\rfloor\)
為4,次數為\(\lfloor{ \frac{t-1}{4} }\rfloor\)

#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;
int n,m;
const int maxn=1e3+10;
int mp[maxn][maxn];
int f[maxn][maxn];
bool st[maxn][maxn];
bool huan=0;
int nxt[4]={0,0,1,-1},nyt[4]={-1,1,0,0};
int dp(int x,int y){
	if(huan) return -1;
	if(f[x][y]!=-1) return f[x][y];
	st[x][y]=1;
	f[x][y]=1;
	for(int i=0;i<4;++i){
		int nx=x+nxt[i],ny=y+nyt[i];
	    if(nx>=1 && ny>=1 && nx<=n && ny<=m && mp[nx][ny]==(mp[x][y]+1)%4){//保證Q-W-E-R
	       
	    	if(st[nx][ny]){//有環 
	    		huan=1;return -1;
	    	}
	    	f[x][y]=max(f[x][y],dp(nx,ny)+1);
	
	    }
	}
	st[x][y]=0;
	return f[x][y];
}
int main(){
	cin>>n>>m;
	for(int i=1;i<=n;++i)
		for(int j=1;j<=m;++j){
			char c;cin>>c; //用數值代替
			if(c=='Q') mp[i][j]=0;
			if(c=='W') mp[i][j]=1;
			if(c=='E') mp[i][j]=2;
			if(c=='R') mp[i][j]=3;
		}
	memset(f,-1,sizeof(f));
	int res=0;
 	for(int i=1;i<=n;++i)
 		for(int j=1;j<=m;++j){
 			int t=dp(i,j);//t為路徑長度
 			if(mp[i][j]) t-=4-mp[i][j];
 			res=max(t/4,res);
		 }
	if(huan) puts("infinity");
	else if(res==0) puts("none");
	else cout<<res<<endl; 
	return 0;
} 

賽後總結

1.不能無腦寫上去,加以分析,欲速則不達
2.學會轉化為做過的題目
3.仔細讀題(讀了個寂寞)

ZFY AK IOI