2014 GCJ Round 1C

题目链接:GCJ|Round1C

在浦东图书馆里面做的比赛,今年的GCJ果然还是打酱油。

三道题目, 做了第一题跟第二题的小数据。

A:一道稍带点YY的题目, 题意是, 输入X/Y的形式,(X,Y可能不是互质), 初始的值全部都是0/1,1/1的形式。 然后z = (x  + y) / 2构造数据, 比如现在是0/1,1/1经过一次构造可以构造出(0/1 + 1/1/)/2 = 1/2,经过两次可以构造出1/4的数据。 现在给出一个数, 然后由当前的数往前推, 问最少推多少次可以推出一个1/1.

分析:首先分母必须要是2^k,而且要求最少的次数, 可以每次都乘以2, 大于等于1就行。 

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define lowbit(x) (x & (-x))
typedef long long LL;

LL gcd(LL a, LL b){
	return b == 0 ? a : gcd(b , a % b);
}

int main(){
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	int n, cases = 1;
	scanf("%d", &n);
	LL x, y;
	char op;
	while(n--){
		scanf("%lld%c%lld", &x, &op, &y);
		LL z = gcd(x, y);
		x /= z, y /= z;
		int ans = 0;
		while(x < y){
			x *= 2;
			z = gcd(x, y);
			x /= z, y /= z;
			if(y != lowbit(y)){
			    ans = 1e9;
			    break;
			}
			++ans;
			if(ans > 40) break;
		}
		if(ans > 40)	printf("Case #%d: impossible\n", cases++ );
		else printf("Case #%d: %d\n", cases++, ans);
	//	printf("%lld %lld\n", x, y);
	}
	return 0;
}

B:题意:给出一些字符串均有小写字母构成, 现在要求将所有的字符串相连构成一串, 问有多少种合法的方式, 一个合法的结果是: 所有相同的字母必须要连在一起。

分析:只会小数据, 小数据的字符串个数 <= 10,那么我dfs枚举所有的连接情况然后判断一下,也才10!种, 完全可以接受。 大数据的解法会及时更新。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<string>
#include<algorithm>
using namespace std;
#define lowbit(x) (x & (-x))
typedef long long LL;
int a[10];
bool vis[11];
int n, ans;
int cases = 1;
char c[10000];
string A, B, s[11];
bool tt[26];

void gao(){
	int i = 0;
	B = "";
	for(int i = 0; i < n; ++i){
   		B += s[a[i]];
	}
	memset(tt, 0, sizeof(tt));
	int len = B.size();

	for(int i = 0; i < len;){
		int j = i;
		if(tt[B[i] - 'a'] == 1) return ;
		tt[B[i] - 'a'] = 1;
		while(B[i] == B[j]){
			++i;
		}
		if(i == len) break;
	}
	++ans;
}

void dfs(int k){
	if(k == n){
		gao();
		return ;
	}

	for(int i = 0; i < n; ++i){
		if(!vis[i]){
			a[k] = i;
			vis[i] = 1;
			dfs(k + 1);
			vis[i] = 0;
		}
	}
}

int main(){
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	int T;
	scanf("%d", &T);
	while(T--){
	//	string s;
		scanf("%d", &n);
		int tmp = n, j = 0, t = 0;
		while(tmp--){
			cin >> s[t];
			++t;
		}
		ans = 0;
		memset(vis, 0, sizeof(vis));
		dfs(0);
		printf("Case #%d: %d\n", cases++, ans);
	}
	return 0;
}

C:待补。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值