题目链接: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:待补。