SDUT 2021 Spring Individual Contest(for 20) - 2补题

Insert picture description here
According to the meaning of the question, the bus segment includes the vertical line segment and the horizontal line segment and the diagonal line. The judgment method of the vertical line segment and the horizontal line segment is the same. The vertical and horizontal judgment method is to see how many line segments with even lengths can be divided on a line

#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;
}

Insert picture description here
Because only one number is required to meet the conditions, and each digit of each number is only composed of 0 or 1, so you can choose the approach of violent enumeration + bfs, and just find a number that meets the conditions.

#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;
}

Insert picture description here

Because the data in the question are all 4-digit prime numbers, you can first use a prime number sieve to filter out the prime numbers, and then use the brute force enumeration + bfs method to solve the problem

#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;
}

Guess you like

Origin blog.csdn.net/weixin_51768569/article/details/114492452