1. 程式人生 > 實用技巧 >Codeforces Round #696 (Div. 2)

Codeforces Round #696 (Div. 2)

這次比賽很玄學,也可能是自己太困了吧,直接爆 0
但是第二天醒來重新做,做出來兩道。。。

A. Puzzle From the Future
題目連結:https://codeforces.com/problemset/problem/1474/A
思路:
a的第一個字元一定是 1 , 將 aa = a[i - 1] + b[i - 1] ,則 aa 有三種取值:0 , 1 , 2 ; a[i] 、b[i] 有兩種取值:0 , 1 ,列舉這幾種情況就可。

#include<iostream>
#include<cstring>
#include<cstdio>
#include<string.h> 
using namespace std;
char a[100010];
int main(){
	string b; 
	int t , n ;
	cin >> t;
	while(t --){
		scanf("%d",&n);
		cin >> b;
		a[0] = '1' ;
		for(int i = 1 ; i < n ; i ++){
			char aa = (a[i - 1] - '0') + (b[i - 1] - '0') + '0';
			if(b[i] == '1' && (aa == '0' || aa == '1')) a[i] = '1' ;
			else if(b[i] == '1' && aa == '2') a[i] = '0';
			
			if(b[i] == '0' && (aa == '0' || aa == '2')) a[i] = '1' ;
			else if(b[i] == '0' && aa == '1') a[i] = '0' ;
		}
			
		for(int i = 0 ; i < n ; i ++) printf("%c",a[i]);
		puts("");
	}
	
	return 0; 
}

B. Different Divisors
題目連結:https://codeforces.com/problemset/problem/1474/B
思路:
1.題目要求至少有四個因子並且因子的差至少為 d
2.我們要找的不是至少為 4 個因子的整數,而是恰好為 4 個因子的整數 , 且有兩個已經確定,即:1 和 該數本身 。
3.那麼在滿足只有四個因子的情況下,另外兩個因子只能是質數才能保證該數恰好為 4 個因子 。
4.第一個因子最小是 d + 1 , 第二個因子最小是 2 * d + 1 (因為得滿足相鄰因子的差 >= d)
5.第一個因子從 d + 1 開始列舉 , 如果不是質數,就把兩個因子都 + 1 ,直到遇到一個質數為止;
6.第二個因子從 2 * d + 1 加上因為第一個因子列舉時加的數 開始列舉 , 如果不是質數,就只把第二個因子 + 1 ,直到遇到一個質數為止。
7.最後將兩個因子相乘 就是 結果。

#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

int judge(int x){
	for(int i = 2 ; i <= x / i ; i ++){
		if(x % i == 0) return 0;
	}
	return 1 ;
}

int main(){
	int t , d;
	cin >> t;
	while(t --){
		scanf("%d",&d);
		int a = d + 1 ;
		int b = 2 * d + 1;
		while(judge(a) == 0){
			a ++ , b ++;
		} 
		while(judge(b) == 0){
			b ++;
		}
		int res = a * b;
		printf("%d\n",res);
	}
	return 0;
}

這次比賽讓我知道了困的時候不要勉強,否則會WA的很慘。
還是頭腦清醒的時候做題比較好哇。