#include <bits/stdc++.h>
using namespace std;
#define int long long int
signed main(){
int a,b;
while(cin>>a>>b){
vector<bool> steps;
while(b!=1){
if(a>b){
a-=b;
steps.push_back(true);
}
else{
swap(a,b);
steps.push_back(false);
}
}
int ans = pow(2,a-1);
for(int i = steps.size()-1 ; i>=0 ; i--){
if(steps[i]){
ans*=2;
}
else{
ans+=1;
}
}
cout<<ans<<endl;
}
return 0;
}
#include
using namespace std;
#define int long long int
signed main(){
int a,b;
while(cin>>a>>b){
vector steps;
while(b!=1){
if(a>b){
a-=b;
steps.push_back(true);
}
else{
swap(a,b);
steps.push_back(false);
}
}
int ans = pow(2,a-1);
for(int i = steps.size()-1 ; i>=0 ; i--){
if(steps[i]){
ans*=2;
}
else{
ans+=1;
}
}
cout< }
return 0;
}
我記得pow的回傳值是double型態
#include
using namespace std;
#define int long long int
signed main(){
int a,b;
while(cin>>a>>b){
vector steps;
while(b!=1){
if(a>b){
a-=b;
steps.push_back(true);
}
else{
swap(a,b);
steps.push_back(false);
}
}
int ans = pow(2,a-1);
for(int i = steps.size()-1 ; i>=0 ; i--){
if(steps[i]){
ans*=2;
}
else{
ans+=1;
}
}
cout< }
return 0;
}我記得pow的回傳值是double型態
以及ans會大到需要用long long
#include
using namespace std;
#define int long long int
signed main(){
int a,b;
while(cin>>a>>b){
vector steps;
while(b!=1){
if(a>b){
a-=b;
steps.push_back(true);
}
else{
swap(a,b);
steps.push_back(false);
}
}
int ans = pow(2,a-1);
for(int i = steps.size()-1 ; i>=0 ; i--){
if(steps[i]){
ans*=2;
}
else{
ans+=1;
}
}
cout< }
return 0;
}
大大,我在參考你的程式碼後,加上a,b約分的環節,就AC (3ms, 332KB)了,在此感謝您
以下我的程式碼:
#include <bits/stdc++.h>
using namespace std;
int main(void)
{
int a,b;
while(cin>>a>>b){
int A=max(a,b);
int B=min(a,b);
while(A%B!=0){
A%=B;
swap(A,B);
}
if(B!=1){
a/=B;
b/=B;
}
vector<bool> steps;
while(b!=1){
if(a>b){
a-=b;
steps.push_back(true);
}
else{
swap(a,b);
steps.push_back(false);
}
}
unsigned long long ans = pow(2,a-1);
for(int i = steps.size()-1 ; i>=0 ; i--){
if(steps[i]){
ans*=2;
}
else{
ans+=1;
}
}
steps.clear();
cout<<ans<<endl;
}
return 0;
}