NYOJ-Euclidean, nyoj61
Euclidean time limit: 1000 MS | memory limit: 65535 KB difficulty: 0
-
Description
-
It is known that gcd (a, B) represents the maximum common divisor of a and B.
Now we will give you an integer n. Your task is to find the largest x in the range [1, n) so that gcd (x, n) is equal to 1.
-
Input
-
The first line of the input file is a positive integer T, indicating that there are T groups of test data
Next there are T rows, each row has a positive integer n (1 <= n <= 10 ^ 1000 ).
-
Output
-
X is required for each group of test outputs.
-
Sample Input
-
247
-
Sample output
-
36
Code:
#include<stdio.h>#include<string.h>char a[1001];int b[1001];int main(){int T;scanf("%d",&T);while(T--){int i,j;scanf("%s",a);int len=strlen(a);if(strcmp(a,"1")==0){printf("1\n");continue;}for(i=len-1,j=0;i>=0;--i,++j)b[j]=a[i]-'0';if(b[0]!=0){ b[0]=b[0]-1; }else{b[0]=10-1;b[1]--;for(i=1;i<len;++i){ if(b[i]<0) { b[i]=b[i]+10; b[i+1]--; } else break;}}if(b[len-1]==0)len--;for(i=len-1;i>=0;--i)printf("%d",b[i]);printf("\n");}return 0;}
Solution:
The maximum number of two adjacent numbers is 1, so 1 ~ If the largest X in n makes Gcd (x, n) = 1, x = n-1. [Note: WHEN n = 1, X = 1]
Introduction to Euclidean
Euclid (about activities at the former 300-), the specified ent Greek mathematician. his book "geometric original" (short for "original") is world renowned. on his life now know very little. the early years of Plato's Emy in Athens, educated, learned of the Greek classical mathematical science and culture. BC 300 years Euclid to emerge and rule Egypt in the Ptolemaic Wang (BC 364 ~ Former 283) at the invitation, he came to Alexander, where long-term work. he is a WEN Liang Dunhou educationalist, the aspiring mathematics of disabilities, always good at giving systematic guidance. but the opposition refuses to study assiduously, opportunistic style, are also opposed to narrow practical perspective. according Proclus (about 410 ~ 485) records, Ptolemy Wang asked Euclid, in addition to his "(geometry) the original", are there any other except cut to learning geometry. euclid replied: "In geometry, not specifically for the laying of King Avenue. "This sentence has been read through the ages later became the learning Monitor. shituobeiwushi (about 500) account of another story, one that only students started to learn first proposition, asked the geometry of Euclid after what will be. euclid: three coins to him, because he wants to get real in learning benefit.
Western geometry rise in Egypt, as Thales, and others shift of Ioannina in Greece, such as Pythagoras School to Athens 20st century. late in Greece because of lost independence, lea ...... remaining full text>
Euclidean Algorithm
The calculation process is the same, but the final Modulo for 1001 is as follows:
1 = 167-166
= 167-(834-4*167)
= 5*167-834
= 5*(1001-834)-834
= 5*1001-6*834
= 5*1001-6 * (3837-3*1001)
= 23*1001-6*3837
Then divide the two ends of the peering expression by the modulo 1001 at the same time.
6*3837 = 1 (mod 1001)
So x = 6