ACM 2009. 6. 2. 20:49
반응형
I got accept from Judge Server

----------------------------------------------------------------------------------------------
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
 
#define MAXV   200
#define NOCOLOR   0
#define BLACK   1
#define RED    2
#define CONNECTED  1
#define DISCONNECTED 0
 
typedef struct
{
 int edges[MAXV][MAXV];
 int color[MAXV];
 int nvertices;     
 int nedges;     
}Graph;
 
int  checkBicoloring(int init);
int  checkBicoloring(int init, int color);
void initEdge();
 
Graph graph;
 
int main(void)
{
 int i, first=0, second=0, checkBicolor=0;
 
 while(1){
  scanf("%d", &graph.nvertices);
  if(graph.nvertices == 0)
   break;
  
  if(graph.nvertices < 1 || graph.nvertices > MAXV)
   continue;
 
  scanf("%d", &graph.nedges);
 
  initEdge();
 
  for(i=0; i<graph.nedges; i++){
   scanf("%d %d", &first, &second);
   graph.edges[first][second] = CONNECTED;
   graph.edges[second][first] = CONNECTED;
  }
 
  if(checkBicoloring(0, BLACK) == 0){
   printf("BICOLORABLE.\n");
  } 
  else{
   printf("NOT BICOLORABLE.\n");
  }
 } 
 return 0;
}
 
void initEdge()
{
 int i, j;
 for(i=0; i<MAXV; i++){
  graph.color[i] = NOCOLOR;
  for(j=0; j<MAXV; j++){
   graph.edges[i][j] = DISCONNECTED;
  }
 }
}
 
int checkBicoloring(int init, int color)
{
 int i;
 if (graph.color[init] == NOCOLOR)
  graph.color[init] = color;
 
 for (i=0; i<graph.nvertices; i++){
  if (graph.edges[init][i] == CONNECTED){
   if(graph.color[init] == graph.color[i]){
    return 1;
   }
 
   if(graph.color[i] == NOCOLOR){ 
    if(graph.color[init] == BLACK)
     return checkBicoloring(i, RED);
    else
     return checkBicoloring(i, BLACK);
   } 
  }
 }
 return 0;
}


반응형

'ACM' 카테고리의 다른 글

[Accept] 10099 - The Tourist Guide  (0) 2009.06.02
[Time Limit] 10099 - The Tourist Guide  (0) 2009.06.02
[Accept] 100 - The 3n+1 problem  (0) 2009.06.02
10006 - Carmichael Numbers  (0) 2009.06.02
850 - Crypt Kicker II  (0) 2009.06.02
posted by ssuk1010
:
ACM 2009. 6. 2. 20:47
반응형


I got accept from Judge Server

----------------------------------------------------------------------------------------------

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
#define MAX_NUM 1000000
int getCycleLength(unsigned int input);
 
typedef struct Number{
 int num1;
 int num2;
 unsigned int cycleLength;
}Number;
 
char *number;
 
int main(void)
{
 char line[100];
 int  numCount=0, tmp;
 int  i, cycleLength, check=0;
 Number  num;
 
 while(scanf("%d %d", &num.num1, &num.num2) == 2){
  
  if(num.num1>1000000 ||  num.num2>1000000 || num.num1<1 || num.num2<1){
   continue;
  }
 
  check = 0;
  if(num.num1 > num.num2){
   tmp       = num.num2;
   num.num2 = num.num1;
   num.num1 = tmp;
   check  = 1;
  }
 
  number = (char*)malloc(MAX_NUM);
  memset(number, '1', MAX_NUM);
 
  num.cycleLength = 0;
  for(i=num.num2; i>=num.num1; i--)
  {
   if(number[i-1]=='0')
    continue;
 
   cycleLength = getCycleLength(i);
   if(!cycleLength)
    return 0;
 
   if(cycleLength > num.cycleLength ){
    num.cycleLength = cycleLength;
   }
  }
  if(check){
   printf("%d %d %d\n", num.num2, num.num1, num.cycleLength);
  }
  else
  {
   printf("%d %d %d\n", num.num1, num.num2, num.cycleLength);
  }
  free(number);
 }
 return 0;
}
 
int getCycleLength(unsigned int input)
{
 unsigned int cycleLength=1;
 while(input != 1)
 {
  if(input-1<MAX_NUM && input>0){
   number[input-1] = '0';
  // printf("%d = %c\n", input, number[input-1]);
  }
  //printf("%d ", input);
  if(input%2 == 1){
   input = (input*3) +1;
   cycleLength++;
  }
  else {
   input = (input/2);
   cycleLength++;
  }
 }
 //printf("%d\n", input);
 return cycleLength;
}

반응형

'ACM' 카테고리의 다른 글

[Time Limit] 10099 - The Tourist Guide  (0) 2009.06.02
[Accept] 10004 - Bicoloring problem  (0) 2009.06.02
10006 - Carmichael Numbers  (0) 2009.06.02
850 - Crypt Kicker II  (0) 2009.06.02
10039 - Railroads  (0) 2009.06.02
posted by ssuk1010
:
ACM 2009. 6. 2. 20:38
반응형


  Carmichael Numbers 

An important topic nowadays in computer science is cryptography. Some people even think that cryptography is the only important field in computer science, and that life would not matter at all without cryptography. Alvaro is one of such persons, and is designing a set of cryptographic procedures for cooking paella. Some of the cryptographic algorithms he is implementing make use of big prime numbers. However, checking if a big number is prime is not so easy. An exhaustive approach can require the division of the number by all the prime numbers smaller or equal than its square root. For big numbers, the amount of time and storage needed for such operations would certainly ruin the paella.

However, some probabilistic tests exist that offer high confidence at low cost. One of them is the Fermat test.

Let a be a random number between 2 and n - 1 (being n the number whose primality we are testing). Then, n is probably prime if the following equation holds: 

\begin{displaymath}a^n \bmod n = a
\end{displaymath}

If a number passes the Fermat test several times then it is prime with a high probability.

Unfortunately, there are bad news. Some numbers that are not prime still pass the Fermat test with every number smaller than themselves. These numbers are called Carmichael numbers.

In this problem you are asked to write a program to test if a given number is a Carmichael number. Hopefully, the teams that fulfill the task will one day be able to taste a delicious portion of encrypted paella. As a side note, we need to mention that, according to Alvaro, the main advantage of encrypted paella over conventional paella is that nobody but you knows what you are eating.

Input 

The input will consist of a series of lines, each containing a small positive number n ( 2 < n < 65000). A number n = 0 will mark the end of the input, and must not be processed.

Output 

For each number in the input, you have to print if it is a Carmichael number or not, as shown in the sample output.

Sample Input 

1729
17
561
1109
431
0

Sample Output 

The number 1729 is a Carmichael number.
17 is normal.
The number 561 is a Carmichael number.
1109 is normal.
431 is normal.



Miguel Revilla 
2000-08-21

반응형

'ACM' 카테고리의 다른 글

[Accept] 10004 - Bicoloring problem  (0) 2009.06.02
[Accept] 100 - The 3n+1 problem  (0) 2009.06.02
850 - Crypt Kicker II  (0) 2009.06.02
10039 - Railroads  (0) 2009.06.02
10110 - Light, more light  (0) 2009.06.02
posted by ssuk1010
: