Thursday 13 February 2014

UVA sloved: problem number: 10579 - Fibonacci Numbers

10579 - Fibonacci Numbers
Time limit: 3.000 seconds

Problem E: Fibonacci Numbers

A Fibonacci sequence is calculated by adding the previous two members of the sequence, with the first two members being both 1.
f (1) = 1, f (2) = 1, f (n > 2) = f (n - 1) + f (n - 2)

Input and Output 

Your task is to take numbers as input (one per line), and print the corresponding Fibonacci number.

Sample Input 

3
100

Sample Output 

2
354224848179261915075

Note: No generated fibonacci number in excess of 1000 digits will be in the test data, i.e. f (20) = 6765 has 4 digits..
Local_UVa'2003
_____________________________________________________________________

solution: 

import java.math.BigInteger;
import java.util.Scanner;

public class Main

{
    public static void main(String[] args)
    {
      Scanner input= new Scanner(System.in);
      int x;
      
      while(input.hasNext())
      {
          x=input.nextInt();
          BigInteger h=BigInteger.ONE;
          BigInteger m=BigInteger.ONE;
          BigInteger sum=BigInteger.ZERO;
          while(x>2)
          {
              sum=h.add(m);
              m=h;
              h=sum;
              x--;
          }
       System.out.println(sum);
      }
    }
}


/* had a little help from a big pal.*/

Wednesday 12 February 2014

UVA sloved: problem number: 11448 - Who said crisis?

11448 - Who said crisis?
Time limit: 10.000 seconds 
 B. Who Said Crisis? 

Context

The company Assistance for Critical Moments (ACM) is helping other companies to overcome the current economical crisis. As experts in computing machinery, their job is to calculate the cost/benefit balance of the other companies. They receive two numbers, indicating the total amount of benefits and costs, and they have to compute the final balance.

The Problem

You have to solve the complex business problem of computing balances. You are given two positive integer numbers, corresponding to the benefits and the costs. You have to obtain the total balance, i.e., the difference between benefits and costs.

The Input

The first line of the input contains an integer indicating the number of test cases.
For each test case, there is a line with two positive integer numbers, A and B, corresponding to the benefits and the costs, respectively. Both numbers are between 0 and a googol (10100) to the power of a hundred.

The Output

For each test case, the output should consist of a line indicating the balance: A-B.

Sample Input

4
10 3
4 9
0 8
5 2

Sample Output

7
-5
-8
3

_________________________________________________________________


solution: 
import java.util.Scanner;
import java.math.BigInteger;

public class Main
{
public static void main(String[] args)
{
Scanner input= new Scanner(System.in);
BigInteger x,y;
int c,d;

c= input.nextInt();
d= 0;

while(c>d)
{
x= input.nextBigInteger();
y= input.nextBigInteger();
System.out.println(x.subtract(y));
d++;
}
}
}


Saturday 8 February 2014

UVA sloved: problem number: 10110 - Light, more light

10110 - Light, more light

Time limit: 3.000 seconds
Light, more light

The Problem

There is man named "mabu" for switching on-off light in our University. He switches on-off the lights in a corridor. Every bulb has its own toggle switch. That is, if it is pressed then the bulb turns on. Another press will turn it off. To save power consumption (or may be he is mad or something else) he does a peculiar thing. If in a corridor there is `n' bulbs, he walks along the corridor back and forth `n' times and in i'th walk he toggles only the switches whose serial is divisable by i. He does not press any switch when coming back to his initial position. A i'th walk is defined as going down the corridor (while doing the peculiar thing) and coming back again. Now you have to determine what is the final condition of the last bulb. Is it on or off?
 

The Input

The input will be an integer indicating the n'th bulb in a corridor. Which is less then or equals 2^32-1. A zero indicates the end of input. You should not process this input.

The Output

Output "yes" if the light is on otherwise "no" , in a single line.

Sample Input

3
6241
8191
0

Sample Output

no
yes
no



by-
Sadi Khan
Suman Mahbub
01-04-2001

_______________________________________________

solution:

#include<stdio.h>
#include<math.h>

int main()
{
long n, a, b;
while(scanf("%lld", &n)==1)
{
if(n==0)
break;
a= sqrt(n);
b=a*a;

if(b==n)
printf("yes\n");
else
printf("no\n");
};
return 0;
}