My Google Code Jam 2010

A. Snapper Chain

 

 

Problem

 

The Snapper is a clever little gadget that, on one side, plugs into a power socket and, on the other side, exposes a power socket for plugging in a light or other device.

 

When the Snapper is in the ON state and is receiving power from the input socket, then the connected device is receiving power as well. When you snap your fingers, the Snapper toggles between the ON and OFF states. Of course, snapping your fingers only has an effect if the Snapper is plugged in and is receiving power from the socket.

 

In hopes of destroying the universe by means of a singularity, I have purchased N Snapper devices and chained them together by plugging the first one into a power socket, the second one into the first one, and so on. The light is plugged into the Nth Snapper.

 

Initially, all the Snappers are in the OFF state, so only the first one is receiving power from the socket, and the light is off. I snap my fingers once, which toggles the first Snapper into the ON state and gives power to the second one. I snap my fingers again, which toggles both Snappers and then promptly cuts power off from the second one, leaving it in the ON state, but with no power. I snap my fingers the third time, which toggles the first Snapper again and gives power to the second one. Now both Snappers are in the ON state, and if my light is plugged into the second Snapper it will be on.

 

I keep doing this for hours. Will the light be on or off after I have snapped my fingers K times? The light is on if and only if it's receiving power from the Snapper it's plugged into.

 

Input

 

The first line of the input gives the number of test cases, T. T lines follow. Each one contains two integers, N and K.

 

Output

 

For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is either "ON" or "OFF", indicating the state of the light bulb.

 

Limits

 

1 ≤ T ≤ 10,000.

 

Small dataset

 

1 ≤ N ≤ 10;

0 ≤ K ≤ 100;

 

Large dataset

 

1 ≤ N ≤ 30;

0 ≤ K ≤ 108;

 

Sample

 

 

Input 

 

Output 

 

4

1 0

1 1

4 0

4 47

Case #1: OFF

Case #2: ON

Case #3: OFF

Case #4: ON

 

My Code:

 

 

#include<stdio.h>

int main()

{

    FILE *in,*ou;

    int i,T,N;

    long K,a[31];

    if((in=fopen("A-large.in","r"))==NULL)

    {

        printf("Can not open the file.");

        exit(0);

    }

    if((ou=fopen("SnapperChain.ou","w"))==NULL) 

    {

        printf("Can not open the file");

        exit(0);

    }

    a[0]=1;

    for(i=1;i<=30;i++) a[i]=a[i-1]*2;

    fscanf(in,"%d",&T);

    for(i=0;i<T;i++)

    {

        fscanf(in,"%d%d",&N,&K);

        fprintf(ou,"Case #%d: ",i+1);

        if((K+1)%a[N]==0) fprintf(ou,"ON/n");

        else fprintf(ou,"OFF/n");

    } 

    fclose(in);

    fclose(ou);

    getch();

    return 0;  

}

 

 

B. Fair Warning
Problem
On our planet, Jamcode IX, three Great Events occurred. They happened 26000, 11000 and 6000 slarboseconds ago. In 4000 slarboseconds, the amount of time since all of those events will be multiples of 5000 slarboseconds, the largest possible amount... and the apocalypse will come.
Luckily for you, you live on Jamcode X! The apocalypse came on Jamcode IX less than a year ago. But Jamcode X has a worrying prophecy: "After the moment of reckoning, on the first optimum anniversary of the N Great Events, the apocalypse will come. 64 bits will not save you. You have been warned."
The people of Jamcode X are very concerned by this prophecy. All of the Great Events have already happened, and their times have been measured to the nearest slarbosecond; but nobody knows when their optimum anniversary will occur. After studying the diary of a scientist from Jamcode IX, scientists working on the problem have come up with a theory:
The moment of reckoning is now, the moment you solve this problem. At some time y ≥ 0 slarboseconds from now, the number of slarboseconds since each of the Great Events will be divisible by some maximum number T. If you can find the smallest value of y that gives this largest possible T, that will give you the optimum anniversary when the apocalypse will come.
On Jamcode IX, for example, there were 3 Great Events and they happened 26000, 11000 and 6000 slarboseconds before the moment of reckoning. 4000 slarboseconds later, the amount of time since each event was a multiple of T=5000 slarboseconds, and the apocalypse came.
Your job is to compute the amount of time until the apocalypse comes. But remember the prophecy: even though the people of Jamcode X have been solving problems for two years, and 64-bit integers have always been enough, they might not always be enough now or in the future.
Input
The first line of the input gives the number of test cases, C. C lines follow. Each starts with a single integer N, which is followed by a space and then N space-separated integers ti, the number of slarboseconds since Great Event i occurred.
Output
For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the minimum number of slarboseconds until ti + y is a multiple of the largest possible integer factor T for all i.
Limits
1 ≤ C ≤ 100.
ti ≠ tj for some i, j.
Small dataset
2 ≤ N ≤ 3.
1 ≤ ti ≤ 108.
Large dataset
2 ≤ N ≤ 1000.
1 ≤ ti ≤ 1050.
Sample
Input 
 
3
3 26000000 11000000 6000000
3 1 10 11
2 800000000000000000001 900000000000000000001
Output 
 
Case #1: 4000000
Case #2: 0
Case #3: 99999999999999999999
My code:
#include <stdio.h>
#include <string.h>
#define  MAXCHAR 100
void Add (char *ch1 , char *ch2 , char *ch3)
{// ch3 = ch1 + ch2;
 int len1 = strlen (ch1) , len2 = strlen (ch2) , len3 , i , tmp , num1[MAXCHAR] , num2[MAXCHAR] , num3[MAXCHAR];
 memset (num1 , 0 , sizeof(num1)); 
 memset (num2 , 0 , sizeof(num2)); 
 memset (num3 , 0 , sizeof(num3));
 
  for (i=len1-1;i>=0;i--) num1[len1-1-i] = ch1[i] - 48; for (i=len2-1;i>=0;i--) num2[len2-1-i] = ch2[i] - 48;
 i = 0 , tmp = 0 , len3 = len1 > len2 ? len1 : len2;
 while (i < len3)
 {
  num3[i] = num1[i] + num2[i] + tmp;
  tmp = 0;
  if (num3[i] >= 10) { tmp = num3[i]/10; num3[i] %= 10; }
  i ++;
 }
 if (tmp != 0) { num3[i] = tmp; i ++; }
 len3 = i;
 for (i=len3-1;i>=0;i--) ch3[len3-1-i] = num3[i] + 48;
 ch3[len3] = '/0';
 return ;
}
void Minus (char *ch1 , char *ch2 , char *ch3)
{ // 保證 ch1 > ch2 : ch3 = ch1 - ch2
 int i , tmp , len1 = strlen (ch1) , len2 = strlen (ch2) , len3 , num1[MAXCHAR] , num2[MAXCHAR] , num3[MAXCHAR];
 memset (num1 , 0 , sizeof(num1)); 
 memset (num2 , 0 , sizeof(num2));
 memset (num3 , 0 , sizeof(num3));
 for (i=0;i<len1;i++) num1[len1-1-i] = ch1[i] - 48; for (i=0;i<len2;i++) num2[len2-1-i] = ch2[i] - 48;
 i = 0 , tmp = 0 , len3 = len1 > len2 ? len1 : len2;
 while (i < len3)
 {
  num3[i] = num1[i] - num2[i] - tmp;
  if (num3[i] < 0) { num3[i] += 10 , tmp = 1; }
  else tmp = 0;
  i ++;
 }
 
 while (len3 > 1 && num3[len3-1] == 0) len3 --;
 for (i=0;i<len3;i++) ch3[i] = num3[len3-1-i] + 48;
 ch3[len3] = '/0';
 return ;
}
void MUL (char *ch1 , char *ch2 , char *ch3)
{// ch3 = ch1*ch2;
 int i , j , len1 = strlen (ch1) , len2 = strlen (ch2) , len3 , tmp , num1[MAXCHAR] , num2[MAXCHAR] , num3[MAXCHAR];
 memset (num1 , 0 , sizeof(num1)); 
 memset (num2 , 0 , sizeof(num2)); 
 memset (num3 , 0 , sizeof(num3));
 for (i=len1-1;i>=0;i--) num1[len1-1-i] = ch1[i] - 48; for (i=len2-1;i>=0;i--) num2[len2-1-i] = ch2[i] - 48;
 for (i=0;i<len1;i++)
 {
  tmp = 0;
  for (j=0;j<len2;j++)
  {
   num3[i+j] += num1[i]*num2[j] + tmp; 
   tmp = 0;
   if (num3[i+j] >= 10) { tmp = num3[i+j]/10; num3[i+j] %= 10; }
  }
  while (tmp != 0) { num3[i+j] += tmp; tmp = num3[i+j]/10; j ++; }
 }
 len3 = len1+len2;
 while (len3 > 1 && num3[len3-1] == 0) len3 --; 
 for (i=0;i<len3;i++) ch3[len3-1-i] = num3[i] + 48;
 ch3[len3] = '/0';
 return ;
}
int cmp (char *ch1 , char *ch2) 
{// ch1 > ch2 return 1;  ch1 < ch2 return -1; ch1 == ch2 return 0;
 int i , len1 = strlen (ch1) , len2 = strlen (ch2);
 if (len1 > len2) return 1;
 if (len2 > len1) return -1; 
 for (i=0;i<len1;i++)
 {
  if (ch1[i] > ch2[i]) return 1;
  if (ch1[i] < ch2[i]) return -1;
 }
 return 0;
}
void Div (char *ch1 , char *ch2 , char *ch3)
{//保證ch1>ch2 && ch2 != 0 : ch3 = ch1/ch2 , ch1 = ch1%ch2 , 需要支持函數:cmp () , Minus ();
 int i , len1 , len2 , tmp;
 char ch4[MAXCHAR] , ch5[MAXCHAR];
 ch3[0] = '0' , ch3[1] = '/0';
 while (cmp (ch1 , ch2) >= 0)
 {
  len1 = strlen (ch1) , len2 = strlen (ch2);
  tmp = len1-len2-1 > 0 ? len1-len2-1 : 0;
  
  ch4[0] = '1';
     memcpy (ch5 , ch2 , (len2+1)*sizeof(char));
  for (i=0;i<tmp;i++) { ch4[1+i] = '0' , ch5[len2+i] = '0'; } 
  ch4[1+i] = '/0'; ch5[len2+i] = '/0';
  Add (ch3 , ch4 , ch3); // ch3 += ch4;
  Minus (ch1 , ch5 , ch1); // ch1 -= ch5;
 }
 return ;
}
void Mod (char *ch1 , char *ch2)
{// 保證ch1 >= ch2 : ch1 %= ch2 , 需要支持函數:Minus () , cmp ();
 int i , len1 , len2 , tmp;
 char ch3[MAXCHAR];
 while (cmp (ch1 , ch2) >= 0)
 {
  len1 = strlen (ch1) , len2 = strlen (ch2); 
  tmp = len1-len2-1 > 0 ? len1-len2-1 : 0;
  memcpy (ch3 , ch2 , (len2+1)*sizeof(char));
  for (i=0;i<tmp;i++) ch3[len2 + i] = '0'; ch3[len2 + i] = '/0';
  Minus (ch1 , ch3 , ch1);
 }
 return ;
}
void GCD (char *ch1 , char *ch2 , char *ch3)
{//最大公約數 , ch3 = gcd(ch1 , ch2) , 需要支持函數:mod () , cmp () , Minus;
 int  len1 , len2 , tmp , flag = 1;
 while (1) {
  tmp = cmp (ch1 , ch2);
  if (tmp == 1) Mod (ch1 , ch2); 
  else if (tmp == 0) break ;
  else Mod (ch2 , ch1);
  len1 = strlen (ch1) , len2 = strlen (ch2);
  if (len1 == 1 && ch1[0] == '0') { flag = 2; break ; }
  if (len2 == 1 && ch2[0] == '0') { flag = 1; break ; }   
 }
 if (flag == 1) memcpy (ch3 , ch1 , (strlen (ch1)+1)*sizeof(char)); 
 else memcpy (ch3 , ch2 , (strlen (ch2)+1)*sizeof(char));
 return ;
}
int main ()
{
 int C,N,i,j,k,t,l,flag;
 char ch[1000][MAXCHAR],temp[MAXCHAR],g[MAXCHAR],temp2[MAXCHAR],temp3[MAXCHAR];
 FILE *in,*ou;
  if((in=fopen("B-large.in","r"))==NULL)
    {
        printf("Can not open the file");
        exit(0);
    }    
    if((ou=fopen("ou.txt","w"))==NULL)
    {
        printf("Can not open the file");
        exit(0);
    }
    fscanf(in,"%d",&C);
    for(i=0;i<C;i++)
    {
       //printf("%d",i);
       fscanf(in,"%d",&N);
       t=N;
       for(j=0;j<t;j++) 
       {
           fscanf(in,"%s",ch[j]);
           flag=0;
           for(k=0;k<j;k++) 
               if(cmp(ch[j],ch[k])==0)
               {
                   flag=1;
                   j--;
                   t--;
               }
       }
       N=t;
       for(j=0;j<N;j++)
          for(k=j+1;k<N;k++)
          {
              t=cmp(ch[j],ch[k]);
              if(t==1)
              {
                  strcpy(temp,ch[j]);
                  strcpy(ch[j],ch[k]);
                  strcpy(ch[k],temp);
              }
          }
       //for(j=0;j<N;j++) fprintf(ou,"%s ",ch[j]);
       //fprintf(ou,"/n");
       Minus(ch[1],ch[0],temp);
       //printf("%s/n",temp);
       for(j=0;j<N-2;j++)
       {   
           Minus(ch[j+2],ch[j+1],temp2);
           //printf("%s  %s",temp,temp2);
           GCD(temp,temp2,temp3);
           strcpy(temp,temp3);           
       }
       //printf("%s/n",temp);
       strcpy(temp3,ch[N-1]);
       Mod(temp3,temp);
       //printf("%s/n",temp3);
       if(cmp(temp3,"0/0")!=0) Minus(temp,temp3,temp2);
       else strcpy(temp2,"0/0");
       fprintf(ou,"Case #%d: %s/n",i+1,temp2);
    }
    
    
// while (scanf ("/n%s %s" , temp2 , temp3) != EOF)
//{
   //Mod (temp2 , temp3);
   //printf ("%s/n" , temp2);
 //}
 fclose(in);
 fclose(ou);
 getch();
 return 0; 
}
C. Theme Park
The roller coaster can hold k people at once. People queue for it in groups. Groups board the roller coaster, one at a time, until there are no more groups left or there is no room for the next group; then the roller coaster goes, whether it's full or not. Once the ride is over, all of its passengers re-queue in the same order. The roller coaster will run R times in a day.
For example, suppose R=4, k=6, and there are four groups of people with sizes: 1, 4, 2, 1. The first time the roller coaster goes, the first two groups [1, 4] will ride, leaving an empty seat (the group of 2 won't fit, and the group of 1 can't go ahead of them). Then they'll go to the back of the queue, which now looks like 2, 1, 1, 4. The second time, the coaster will hold 4 people: [2, 1, 1]. Now the queue looks like 4, 2, 1, 1. The third time, it will hold 6 people: [4, 2]. Now the queue looks like [1, 1, 4, 2]. Finally, it will hold 6 people: [1, 1, 4]. The roller coaster has made a total of 21 Euros!
Input
The first line of the input gives the number of test cases, T. T test cases follow, with each test case consisting of two lines. The first line contains three space-separated integers: R, k and N. The second line contains N space-separated integers gi, each of which is the size of a group that wants to ride. g0 is the size of the first group, g1 is the size of the second group, etc.
Output
For each test case, output one line containing "Case #x: y", where x is the case number (starting from 1) and y is the number of Euros made by the roller coaster.
Limits
1 ≤ T ≤ 50.
gi ≤ k.
Small dataset
1 ≤ R ≤ 1000.
1 ≤ k ≤ 100.
1 ≤ N ≤ 10.
1 ≤ gi ≤ 10.
Large dataset
1 ≤ R ≤ 108.
1 ≤ k ≤ 109.
1 ≤ N ≤ 1000.
1 ≤ gi ≤ 107.
Sample
Input 
 
3
4 6 4
1 4 2 1
100 10 1
1
5 5 10
2 4 2 3 4 2 1 2 1 3
Output 
 
Case #1: 21
Case #2: 100
Case #3: 20
My code:
#include<stdio.h>
#include<stdlib.h>
int main()
{
    FILE *in,*ou;
    long long R,k,N,i,T,j,cur=0ll,temp=0ll,result=0ll,count,lastcur=0ll,a[1000],value[1000],curs[1000];
    if((in=fopen("sub-4.in","r"))==NULL)
    {
        printf("Can not open the file");
        exit(0);
    }    
    if((ou=fopen("ou.txt","w"))==NULL)
    {
        printf("Can not open the file");
        exit(0);
    }
    
    fscanf(in,"%I64d",&T);
    for(i=0ll;i<T;i++)
    {
        printf("%I64d",i);
        fscanf(in,"%I64d%I64d%I64d",&R,&k,&N);
        for(j=0ll;j<N;j++) fscanf(in,"%I64d",&a[j]);
        for(j=0ll;j<N;j++)
        {
            temp=0ll;
            cur=j;
            count=0ll;
            while(temp<=k&&count<=N) 
            {     
               temp+=a[cur];
               lastcur=cur;
               cur++;
               if(cur==N) cur-=N;
               count++;
            }  
            temp-=a[lastcur];
            cur=lastcur;
            curs[j]=cur;
            value[j]=temp;
        }
        result=0ll;
        cur=0ll;
        for(j=0ll;j<R;j++)
        {
            result+=value[cur];
            cur=curs[cur];
        }
        fprintf(ou,"Case #%I64d: %I64d/n",i+1,result);
    }
    fclose(in);
    fclose(ou);
    getch();
    return 1;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章