類似題目 UVA106- Fermat vs. Pythagoras
分析
參考鏈接,點擊
代碼:
#include <bits/stdc++.h>
using namespace std;
bool gcd(int a, int b){//最大公約數
int temp;
while(b > 0){
temp = a % b;
a = b;
b = temp;
}
if(a == 1)
return true;
return false;
}
int main() {
int n;
scanf("%d", &n);
int res = 0;
int maxLen = sqrt(n);
for (int i=1; i<=maxLen; i++){
for (int j=i+1; j<=maxLen; j++){
int c = j*j + i*i;
if (c > n)
break;
int a = j*j - i*i;
int b = 2*i*j;
if (gcd(a, b) && gcd(a, c) && gcd(b, c)){
res++;
}
}
}
printf("%d\n", res);
return 0;
}
暴力AC了 其實規律也很簡單 註釋中給出規律了
#include <iostream>
using namespace std;
int main()
{
int res = 0;
int temp_A, temp_B;
cin >> temp_A >> temp_B;
if (temp_A*6+temp_B<0){
res = (temp_A*6+temp_B)*2 + temp_A*3;
}else if(temp_A*5+temp_B*2<0){
res = (temp_A*5+temp_B*2)*2 + temp_A*3;
}else if(temp_A*4+temp_B*3<0){
res = (temp_A*4+temp_B*3)*2 + temp_A*3;
}else if(temp_A*3+temp_B*4<0){
res = (temp_A*3+temp_B*4)*2 + temp_A*3;
}else if(temp_A*2+temp_B*5<0){
res = (temp_A*2+temp_B*5)*2 + temp_A*2 + temp_B;
}else if(temp_A*1+temp_B*6<0){
res = (temp_A*1+temp_B*6)*2 + temp_A + temp_B*2;
}else{
res = temp_B*17;
}
cout << res << endl;
return 0;
}
/*
A:1 B:0
(1 1 1 1 1 1 0) (1 1 1 1 1 1 0) (1 1 1)
(1 1 1 1 1 0 0) (1 1 1 1 1 0 0) (1 1 1)
(1 1 1 1 0 0 0) (1 1 1 1 0 0 0) (1 1 1)
(1 1 1 0 0 0 0) (1 1 1 0 0 0 0) (1 1 1)
(1 1 0 0 0 0 0) (1 1 0 0 0 0 0) (1 1 0)
(1 0 0 0 0 0 0) (1 0 0 0 0 0 0) (1 0 0)
(0 0 0 0 0 0 0) (0 0 0 0 0 0 0) (0 0 0)
*/