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

在这里插入图片描述
按照题意,总线段包括垂直线段水平线段和斜线,垂直线段和水平线段的判断方式一样,斜线判断方式是只要是满足垂直线段和水平线段都符合要求的长方形的斜线都满足要求。垂直水平的判断方式是看一条线上可以分出多少条长度为偶数的线段

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

在这里插入图片描述
因为只要求求得一个数符合条件,况且每个数字的每一位数只有0或1组成,因此可以选择暴力枚举+bfs的做法,随便找到一个符合条件的数字即可

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

在这里插入图片描述

因为题中数据全部都是4位数的素数,所以可以先采用素数筛把素数筛出来,然后使用暴力枚举+bfs 的方法求解

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

猜你喜欢

转载自blog.csdn.net/weixin_51768569/article/details/114492452