GoogleJam_Rational Number Tree

大数据代码

#include<iostream>
#include<fstream>
#include<vector>
using namespace std;

struct node{
	unsigned long long p;
	unsigned long long q;
	node(unsigned long long a,unsigned long long b){
		p = a;
		q = b;
	}
};

unsigned long long getIndex(unsigned long long p,unsigned long long q)
{
	vector<unsigned long long> vec;
	while( p!= 1 || q != 1)
	{
		if(p > q){   //r
			vec.push_back(1);
			p -= q;			
		}else{
			vec.push_back(0);
			q -= p;
		}
	}
	unsigned long long ret = 1;
	for(int i = vec.size()-1;i>=0;i--)
	{
		ret *= 2;
		ret += vec[i];
	}
	return ret;
}

node getPQ(unsigned long long index)
{
	if(index == 1){
		return node(1,1);
	}
	node tmp = getPQ(index/2);
	if(index%2 == 0){
		tmp.q += tmp.p;
	}else{
		tmp.p += tmp.q;
	}
	return tmp;
}

int main()
{
	ifstream in("B-large-practice.in");
	ofstream out("bs.out");

	int command;
	unsigned long long n,p,q,index;
	in >> n;

	for(unsigned long long i = 0;i< n;i++)
	{
		in>> command;
		out<<"Case #"<<i+1<<": ";
		if(command == 1){
			in>>index;
			node tmp = getPQ(index);
			out<<tmp.p<<" "<<tmp.q<<endl;
		}else if(command == 2){
			in>>p>>q;
			out<<getIndex(p,q)<<endl;
		}
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值