1167: How Many Eggs Do I Have? 秦九韶定理

Result TIME Limit MEMORY Limit Run Times AC Times JUDGE
3s 8192K 2050 454 Standard

1st Jilin University ACM International Collegiate Programming Contest

Assume you are needed to count a basket of eggs. Even though counting is very easy but unfortunately you are forbidden to count them 1 by 1 instead of counting them c by c, where c is greater than 1. For example, if you count them 3 by 3, then there will be 1 egg left in the basket, if you count them 5 by 5, then there will be 3 eggs left in the basket and if you count them 7 by 7, then there will be 1 egg left again in the basket. Your task is to determine how many eggs there are originally in the basket.

Input Specification

The input consists of several test cases. The first line of each test case contains an integer N(1<=N<=5), then follow N lines, each of which contains two integers ci and bi(3<=ci<=49, 0<=bi<=ci-1, ci is a primary number). It means that you count the eggs ci by ci and finally there are bi eggs left. N=0 marks the last test case, which you should not process.

Output Specification

For each test case, you should print a line containing the minimal possible number of eggs in the basket.

Sample Input

3
3 1
5 3
7 1
0

Sample Output

43



#include <stdio.h>
#include <string.h>
int c[10],b[10];
int l[10];
int main ()
{
int N;
while(scanf("%d",&N)==1&&N)
{
for(int i=0;i<N;i++)
{
scanf("%d%d",&c[i],&b[i]);
l[i]=1;
}
for(int i=0;i<N;i++)
for(int j=0;j<N;j++)
if(i!=j) l[i]*=c[j];
for(int i=0;i<N;i++)
for(int j=1;;j++)
if(l[i]*j%c[i]==1) { l[i]*=j; break; }
int sum=0,tot=1;
for(int i=0;i<N;i++)
{
sum+=l[i]*b[i];
tot*=c[i];
}
printf("%d/n",sum%tot);
}
return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章