Archive for the ‘The easier ones’ Category

UVA – 12406 – Help Dexter   Leave a comment


import java.util.Scanner;

public class HelpDexter {

	public static void main(String[] args) {
		Scanner in = new Scanner(System.in);
		int tc = in.nextInt();
		for (int c = 1; c <= tc; c++) {
			int n = in.nextInt();
			int m = in.nextInt();
			long r1 = Long.MAX_VALUE;
			long r2 = Long.MIN_VALUE;
			for (int i = 0; i < 1 << n; i++) {
				long res = 0;
				for (int j = n - 1; j >= 0; j--)
					if ((1 << j & i) != 0)
						res = res * 10 + 2;
					else
						res = res * 10 + 1;
				if (res % (1 << m) == 0) {
					r1 = Math.min(r1, res);
					r2 = Math.max(r2, res);
				}
			}
			if (r1 != Long.MAX_VALUE && r2 != Long.MIN_VALUE && r1 != r2)
				System.out.printf("Case %d: %d %d\n", c, r1, r2);
			else if (r1 == r2)
				System.out.printf("Case %d: %d\n", c, r2);
			else
				System.out.printf("Case %d: impossible\n", c);
		}
	}
}

Advertisements

Posted April 2, 2012 by epicrado in Mathematics, The easier ones

UVA – 10499 – The Land of Justice   Leave a comment


#include <stdio.h>
int main()
{
	long long int n;
	scanf("%lld",&n);
	while(n>=0)
	{
		
		printf("%lld",(n>1)?n*25:0);
		putchar('%');
		putchar('\n');
		scanf("%lld",&n);
	}
	return 0;
}

Posted October 14, 2011 by epicrado in ad-hoc, Mathematics, The easier ones

UVA – 11185 – Ternary   3 comments




#include <stdio.h>
void toTernary(int n)
{
	if(n)
	{
		toTernary(n/3);
		putchar(n%3+'0');
	}
}
int main()
{
	int n;
	scanf("%d",&n);
	while(n>=0)
	{
		if(!n)
			putchar('0');
		else
			toTernary(n);
		putchar('\n');
		scanf("%d",&n);
	}
	return 0;
}

Posted October 6, 2011 by epicrado in The easier ones

UVA – 729 – The Hamming Distance Problem   4 comments


#include <stdio.h>
char out[] = {'1','0'};
int countBits(int n)
{
	int count = 0;
	while(n>0)
	{
		count+=(n&1);
		n>>=1;
	}
	return count;
}
int main()
{
	int tc,nBits,H,max,i,j;
	scanf("%d",&tc);
	while(tc--)
	{
		scanf("%d %d",&nBits,&H);
		max=1<<nBits;
		for(i=0;i<max;i++)
			if(countBits(i)==H)
			{
				for(j=nBits-1;j>=0;j--)
					putchar(out[!((1<<j)&i)]);
				putchar('\n');
			}
		if(tc)
			putchar('\n');
	}
	return 0;
}

Posted October 2, 2011 by epicrado in The easier ones

UVA – 195 – Anagram   1 comment




#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char inp[1000];
char out[1000];
char v[1000];
char mark[128][1000];
int value[128];
void permute(int i,int len)
{
	if(i==len)
	{
		out[i]=0;
		puts(out);
	}
	else
	{
		int c;
		for(c = 0;c<len;c++)
			if(!v[c]&&!mark[inp[c]][i])
			{
				mark[inp[c]][i]=1;
				v[c]=1;
				out[i]=inp[c];
				permute(i+1,len);
				v[c]=0;
			}
		for(c=0;c<26;c++)
			mark[c+'a'][i]=mark[c+'A'][i]=0;

	}
}
int cmp(const void*a,const void*b)
{
	return value[*(char*)a]-value[*(char*)b];
}
int main()
{
	int n,k,val;
	for(n=0;n<1000;n++)
		v[n]=0;
	for(n=0;n<128;n++)
		for(k=0;k<1000;k++)
			mark[n][k]=0;
	val = 0;
	for(n=0;n<26;n++)
	{
		value[n+'A']=val;
		value[n+'a']=val+1;
		val+=2;
	}
	scanf("%d",&n);
	getchar();
	while(n--)
	{
		gets(inp);
		qsort(inp,strlen(inp),1,&cmp);
		permute(0,strlen(inp));
	}
	return 0;
}

Posted September 7, 2011 by epicrado in Recursive Backtracking, The easier ones

UVA – 12068 – Harmonic Mean   Leave a comment


#include <stdio.h>
int cc = 1;
typedef struct rational
{
	unsigned long long int n,d;
}fraction;
int gcd(unsigned long long int a,unsigned long long int b)
{
	unsigned long long int t;
	while(b)
	{
		t = b;
		b = a%b;
		a = t;
	}
	return a;
}
void simplify(fraction* f)
{
	int g = gcd(f->d,f->n);
	f->d/=g;
	f->n/=g;
}
fraction add(fraction*a,fraction*b)
{
	fraction res;
	res.n = a->n * b->d + a->d * b->n;
	res.d = a->d * b->d;
	simplify(&res);
	return res;
}
fraction divide(fraction*a,fraction*b)
{
	fraction res;
	res.n = a->n * b->d;
	res.d = a->d * b->n;
	simplify(&res);
	return res;
}
void print(fraction* f)
{
	printf("Case %d: %llu/%llu\n",cc++,f->n,f->d);
}
int main()
{
	int c,i;
	scanf("%d",&c);
	while(c--)
	{
		fraction o;
		scanf("%llu",&o.n);
		o.d = 1;
		fraction sum;
		sum.n = 0;
		sum.d = 1;
		for(i=0;i<o.n;i++)
		{
			fraction c;
			c.n = 1;
			scanf("%llu",&c.d);
			sum = add(&sum,&c);
		}
		fraction p = divide(&o,&sum);
		print(&p);
	}
	return 0;
}

Posted September 3, 2011 by epicrado in GCD/LCM, Mathematics, The easier ones

UVA -12239 – Bingo!   Leave a comment


Have a top submission ;)
#include <stdio.h>
#include <stdlib.h>
int balls[100];
char mark[100];
int main()
{
	int N,B;
	register int absolute,i,j,count;
	char c;
	N = B = 0;
	while((c=getchar())!=' ')
		N = N*10 +c-'0';
	while((c=getchar())!='\n')
		B = B*10 + c-'0';
	while(N||B)
	{
		for(i=0;i<=N;i++)mark[i]=balls[i]=0;
		j = 0;
		while((c=getchar())!='\n')
			if(c!=' ')
				balls[j]=balls[j]*10 + c-'0';
			else
				j++;
		count = 0;
		for(i=0;i<B;i++)
			for(j=i;j<B;j++)
			{
				absolute = abs(balls[i]-balls[j]);
				count+=!mark[absolute];
				mark[absolute] = 1;
			}

		if(count==N+1)
			putchar('Y');
		else
			putchar('N');
		putchar('\n');
		N = B = 0;
		while((c=getchar())!=' ')
			N = N*10 + c-'0';
		i++;
		while((c=getchar())!='\n')
			B = B*10 + c-'0';
	}
	return 0;
}

Posted September 2, 2011 by epicrado in Iterative, The easier ones