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"<