NYOJ-歐幾裡得,nyoj61

來源:互聯網
上載者:User

NYOJ-歐幾裡得,nyoj61
歐幾裡得時間限制:1000 ms  |  記憶體限制:65535 KB難度:0

描述

已知gcd(a,b)表示a,b的最大公約數。

現在給你一個整數n,你的任務是在區間[1,n)裡面找到一個最大的x,使得gcd(x,n)等於1。

輸入
輸入檔案的第一行是一個正整數T,表示有T組測試資料
接下來有T行,每行有一個正整數n (1<=n<=10^1000)。
輸出
每組測試輸出要求x。
範例輸入
247
範例輸出
36

代碼:

#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;}

解題思路:

相鄰的的兩個數最大公約數恒為 1,所以1~n中最大的X使得Gcd(x,n)==1,則x=n-1;【注意特列:當n=1時X=1】


對歐幾裡得的英文簡介

Euclid (about activities at the former 300 -), the ancient 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 Academy 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 Alexandria, 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 shortcut 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......餘下全文>>
 
歐幾裡得演算法

計算過程一模一樣,只是最後對1001模數:
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
然後對等式兩端同時除以模1001得

6 * 3837 = 1 (mod 1001)
於是 x = 6
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.