同餘定理:poj2769 Reduced ID Numbers(標記數組)+hdu1021 Fibonacci Again(應用於遞推公式)

來源:互聯網
上載者:User

Reduced ID Numbers

Description T. Chur teaches various groups of students at university U. Every U-student has a unique Student Identification Number (SIN). A SIN s is an integer in the range 0 ≤ s ≤ MaxSIN with MaxSIN = 10 6-1. T. Chur finds this range of SINs too large for identification within her groups. For each group, she wants to find the smallest positive integer m, such that within the group all SINs reduced modulo m are unique.

Input On the first line of the input is a single positive integer N, telling the number of test cases (groups) to follow. Each case starts with one line containing the integer G (1 ≤ G ≤ 300): the number of students in the group. The following G lines each contain one SIN. The SINs within a group are distinct, though not necessarily sorted.

Output For each test case, output one line containing the smallest modulus m, such that all SINs reduced modulo m are distinct.

Sample Input

211248663124866111111987651

Sample Output

18

解題思路:標記數組

   沒什麼好的辦法,從1開始枚舉,開一個標記數組bool p[i]表示存不存在餘數是i的ID Numbers,出現的話跳出迴圈,否則繼續標記,直到最先找到i使得任意一個ID number對i均不同餘為止,此時i最小,即為我們所要的最小的滿足每組各個同學編號均不同餘。

   注意:不要對每一個i都進行memset(p,false,sizeof(p)),數組p[maxn]很大會逾時,只需將可能出現的p[1-i]初始化為false即可。

參考代碼+部分解釋:

#include #include #include #include #include #include #includeusing namespace std;const int maxn=1000000+10;int a[350];bool p[maxn];                         //開一個標記數組int main(){   // freopen("input.txt","r",stdin);    int T;cin>>T;    while(T--){      int n;cin>>n;      for(int i=0;i>a[i];      int i=1;      while(1){        bool ok=true;       // memset(p,false,sizeof(p));      //標記數組初始化       for(int j=0;j

                                                        Fibonacci Again
Problem Description There are another kind of Fibonacci numbers: F(0) = 7, F(1) = 11, F(n) = F(n-1) + F(n-2) (n>=2).
 
Input Input consists of a sequence of lines, each containing an integer n. (n < 1,000,000).
 
Output Print the word "yes" if 3 divide evenly into F(n).

Print the word "no" if not.
 
Sample Input

 0 1 2 3 4 5   

Sample Output
 no no yes no no no 


解題思路:同餘定理的應用。

參考代碼:

#include #include #include #include #include #include #includeusing namespace std;const int maxn=1000000+10;int F[maxn];                      int main(){  //  freopen("input.txt","r",stdin);   F[0]=7;F[1]=11;   for(int i=2;i>n){    if(F[n]==0) cout<<"yes"<


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.