Concurso individual de primavera SDUT 2021 (para 20) - 2 补 题

Inserte la descripción de la imagen aquí
De acuerdo con el significado de la pregunta, el segmento de autobús incluye el segmento de línea vertical y el segmento de línea horizontal y la línea diagonal. El método de juicio del segmento de línea vertical y el segmento de línea horizontal es el mismo. El método de juicio de línea diagonal es el siguiente siempre que la línea diagonal rectangular que cumpla con los requisitos del segmento de línea vertical y el segmento de línea horizontal cumpla con los requisitos. El método de juicio vertical y horizontal es ver cuántos segmentos de línea con longitudes iguales se pueden dividir en una línea.

#include<bits/stdc++.h>
using namespace std;
int main()
{
    
    
    long long n,m,ln=0,lm=0,tmp,ans=0;
    cin>>n>>m;
    for(int i=2;i<=n;i+=2)
    {
    
    
    	ln+=(n-i+1);
	}
	for(int i=2;i<=m;i+=2)
	{
    
    
		lm+=(m-i+1);
	}
    ans=ln*(m+1)+lm*(n+1);
    ans+=2*lm*ln;
    cout<<ans<<endl;
    return 0;
}

Inserte la descripción de la imagen aquí
Debido a que solo se requiere un número para cumplir con las condiciones, y cada dígito de cada número solo se compone de 0 o 1, puede elegir el enfoque de enumeración violenta + bfs y simplemente encontrar un número que cumpla con las condiciones.

#include<iostream>
#include<algorithm>
#include<queue>
using namespace std;
long long n;
void bfs(long long x) {
    
    
    queue<long long > q;
    q.push(1);
    while (!q.empty()) {
    
    
        long long  tmp = q.front();
        q.pop();
        if (tmp % (long long)x == 0) {
    
    
            cout<<tmp<<endl;
            return;
        }
        q.push(tmp * 10);
        q.push(tmp * 10 + 1);
    }
}

int main() {
    
    
    while (cin>>n) {
    
    
        if (n == 0) break;
        bfs(n);
    }
    return 0;
}

Inserte la descripción de la imagen aquí

Debido a que los datos en la pregunta son números primos de 4 dígitos, primero puede usar un tamiz de números primos para filtrar los números primos y luego usar el método de enumeración de fuerza bruta + bfs para resolver el problema

#include<bits/stdc++.h> 
using namespace std;
typedef pair<int, int> P;
const int mod=1e9+7;
const int maxn=1e5+10;

bool isprime[maxn]; 
int vis[maxn];
void sieve(){
    
    
    for(int i=1;i<=maxn;i++) isprime[i]=true;
    
	isprime[0]=isprime[1]=false;
    
	for(int i=2; i<=maxn; i++){
    
    
        if(isprime[i]){
    
    
            for(int j=2*i;j<=maxn;j+=i){
    
    
                isprime[j]=false;
            }
        }
    }
}
int A,B;
int bfs(){
    
    
	for (int i=1000; i<10000; i++) vis[i]=0;
	queue <P> que; 
	P t;
	que.push(P(A,0));
	while (que.size()){
    
    
		P p=que.front();
		que.pop();
		if (p.first==B)
			return p.second;
		for (int i=1; i<=9; i+=2){
    
    
			t.first=p.first/10*10+i;
			if (t.first!=p.first && isprime[t.first] && !vis[t.first]){
    
    
				vis[t.first]++;
				t.second=p.second+1;
				que.push(t);
			}
		}
		for (int i=0; i<=9; i++){
    
    
			t.first=p.first/100*100+i*10+p.first%10;
			if (t.first!=p.first && isprime[t.first] && !vis[t.first]){
    
    
				vis[t.first]++;
				t.second=p.second+1;
				que.push(t);
			}
		}
		for (int i=0; i<=9; i++){
    
    
			t.first=p.first/1000*1000+i*100+p.first%100;
			if (t.first!=p.first && isprime[t.first] && !vis[t.first]){
    
    
				vis[t.first]++;
				t.second=p.second+1;
				que.push(t);
			}
		}
		for (int i=1; i<=9; i++){
    
    
			t.first=i*1000+p.first%1000;
			if (t.first!=p.first && isprime[t.first] && !vis[t.first]){
    
    
				vis[t.first]++;
				t.second=p.second+1;
				que.push(t);
			}
		}
	}
	return -1;
}
int main() {
    
    
	ios::sync_with_stdio(0);
	cin.tie(0);
	cout.tie(0);	
	int T;
	cin>>T;
	sieve();
	while (T--){
    
    		
		cin>>A>>B;
		int ans=bfs();
		if (ans==-1) cout<<"Impossible"<<endl;
		else cout<<ans<<endl;
	}
	return 0;
}

Supongo que te gusta

Origin blog.csdn.net/weixin_51768569/article/details/114492452
Recomendado
Clasificación