UVa 10931 – Parity

#include <iostream>

using namespace std ;

int countBits(int i){
   i = i - ((i >> 1) & 0x55555555);
   i = (i & 0x33333333) + ((i >> 2) & 0x33333333);
   return (((i + (i >> 4)) & 0x0F0F0F0F) * 0x01010101) >> 24;
}

int main (){
   int n,j;
   char s[33] ;
   cin>>n;
   while (n){
       j=0;
       while (n>>j)
           s[j++] = ((n>>j)&1)+'0';


       cout<<"The parity of ";
       while (j--)
           cout.put(s[j]);

       cout<<" is "<<countBits(n)<<" (mod 2)."<<endl;
       cin>>n;
   }
   return 0;
}

Advertisements

UVa 10812 – Beat the Spread!

#include <iostream>

using namespace std;

int main(){
   int sum,dif,tc;
   cin>>tc;
   while (tc--){
       cin>>sum>>dif;

       if (dif>sum || ((sum^dif)&1)) // one odd and other even
           cout<<"impossible"<<endl;

       else {
           dif = (sum+dif)/2;
           cout<<dif<<" "<<sum-dif<<endl;
       }

   }
   return 0;
}

UVa 10773 – Back to Intermediate Math

#include <iostream>
#include <string>
#include <cmath>
#include <stdio.h>

using namespace std;

int main (){

	int n,t=0;
	double d,u,v,ans;
	cin>>n;

	std::cout.setf(std::ios::fixed);
	std::cout.precision(3);

	while (t++<n){
		cin>>d>>v>>u;
		ans = u*u - v*v ;
		if (ans<0.001||u<0.001||v<0.001)
           //cases that make the eqn of ans contains a NaN || d=0
			cout<<"Case "<< t <<": can't determine"<<endl;

		else {
			ans = d/sqrt(ans) -  d/u;
           //d/sqrt(u*u - v*v) = stearing with -ve angle so that the flow takes the boat to the destination
			cout<<"Case "<< t <<": "<< ans << endl;
		}
	}

	return 0;
}

UVa 11466 – Largest Prime Divisor

#include <iostream>
using namespace std;

int main() {
	long long n,ans,i;
	int counter;
	cin>>n;
	while (n!=0){

		if (n<0)
			n=-n;

		ans = -1;
		counter = 0;
		//print -1 for numbers that has 1 prime divisors as primes, pow of primes (4,8,9)
		for ( i = 2; i*i <= n && n!=1; i++) {
			while (n%i == 0){
				n/=i;
				ans = i;
			}
			if (ans == i)counter++;
		}


		if (n!=1 && ans!=-1)
			ans = n;
		else if (counter==1)
			ans = -1;

		cout<<ans<<endl;

		cin>>n;
	}

}

UVa 11661 – Burger Time?

#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

int main(){
	int n,min,lastD,lastR;
	string s;

	while (cin>>n){
		if(n==0 )
			break;

		cin>>s;
		min = 1<<25;

		lastD = lastR = -1;
		for(int i = 0 ; i<n ; i++){
			if(s[i]=='.')
				continue;
			else if (s[i]=='Z'){
				min = 0;
				break;
			}
			else if (s[i]=='D'){
				lastD = i;
				if (i-lastR<min && lastR!= -1)
					min = i-lastR;
			}
			else {
				lastR = i;
				if (i-lastD<min && lastD!=-1)
					min = i-lastD;
			}

		}
		cout<<min<<endl;
	}
	return 0;
}


UVa 11723 – Numbering Roads

#include <iostream>
#include <math.h>

using namespace std ;

int main(){
	int r, n ,ret ,t = 1;
	while (cin>>r>>n){
		if (r==0 || n==0)
			return 0;
		ret = n >=r ? 0 :ceil((double) (r-n)/n);
		if (ret>26)
			cout<<"Case "<<t<<": impossible"<<endl;
		else
			cout<<"Case "<<t<<": "<<ret << endl;
		t++;
	}
	return 0;
}

UVa 11742 – Social Constraints

#include <iostream>
#include <algorithm>

using namespace std;

int main(){
	int n,m,i,ways,incr,temp,first[21],second[21],dif[21],perm[9];

	while (true){
		cin>>n>>m;
		if (n==0 && m==0) return 0;
		for (i = 0 ; i < m ;i++)
			cin>>first[i]>>second[i]>>dif[i];

		for(i= 0 ; i  < n ; i++)
			perm[i] = i;

		ways = 0;
		incr = 0;
		do{
			incr = 1;
			for(i =0 ; i < m ; i ++){
				temp = abs(perm[first[i]] - perm[second[i]]);
				if ((dif[i]>0 && temp>dif[i]) || (dif[i]<0 && temp<-dif[i]) ){
					incr = 0;
					break;
				}
			}
			ways+=incr;
		}while (next_permutation(perm,perm+n));

		cout<<ways<<endl;
	}
	return 0;
}