首页 > 其他 > 详细

POJ 2891 Strange Way to Express Integers

时间:2014-01-21 09:55:41      阅读:379      评论:0      收藏:0      [点我收藏+]
Strange Way to Express Integers
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 8912   Accepted: 2705

Description

Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is described as following:

Choose k different positive integers a1a2…, ak. For some non-negative m, divide it by every ai (1 ≤ i ≤ k) to find the remainder ri. If a1a2, …, ak are properly chosen, m can be determined, then the pairs (airi) can be used to express m.

“It is easy to calculate the pairs from m, ” said Elina. “But how can I find m from the pairs?”

Since Elina is new to programming, this problem is too difficult for her. Can you help her?

Input

The input contains multiple test cases. Each test cases consists of some lines.

  • Line 1: Contains the integer k.
  • Lines 2 ~ k + 1: Each contains a pair of integers airi (1 ≤ i ≤ k).

Output

Output the non-negative integer m on a separate line for each test case. If there are multiple possible values, output the smallest one. If there are no possible values, output -1.

Sample Input

2
8 7
11 9

Sample Output

31

Hint

All integers in the input and the output are non-negative and can be represented by 64-bit integral types.

Source


    看一下维基百科的资料思路就很容易有了,不过这题有了思路后,会有些难写,太乱,本以为计算的中间过程会溢出,交了一次竟然A了,1A 好啊
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#define N 1000000
using namespace std;
struct num
{
    __int64 a,r;
}t[N];
__int64 x,y;
int main()
{
    //freopen("data.in","r",stdin);
    __int64 f(__int64 a,__int64 b);
    __int64 a,r;
    int n;
    while(scanf("%d",&n)!=EOF)
    {
        for(int i=0;i<=n-1;i++)
        {
            scanf("%I64d %I64d",&t[i].a,&t[i].r);
        }
        __int64 X=1,Y=0;
        bool ch = true;
        __int64 ans;
        for(int i=0;i<=n-1;i++)
        {
            __int64 ai = t[i].a;
            __int64 ri = t[i].r;
            ri = ((ri-Y)%ai+ai)%ai;
            __int64 d = f(X,ai);
            if(ri%d!=0)
            {
                ch = false;
                break;
            }
            __int64 temp= ai/d;
            __int64 u = (((-x/temp)+1)*temp)+x;
            if((x>0&&u<x)||x<0)
            {
                x = u;
            }
            temp = ai/d;
            __int64 v=(x*(ri/d))%(temp);
            __int64 pre = X;
            X = (X*temp);
            Y = pre*v+Y;
            if(i==n-1)
            {
                ans = Y%X;
            }
        }
        if(ch)
        {
            printf("%I64d\n",ans);
        }else
        {
            printf("-1\n");
        }
    }
    return 0;
}
__int64 f(__int64 a,__int64 b)
{
    if(b==0)
    {
        x = 1;
        y = 0;
        return a;
    }
    __int64 d = f(b,a%b);
    __int64 temp = x;
    x = y;
    y = temp - (a/b)*y;
    return d;
}



POJ 2891 Strange Way to Express Integers

原文:http://blog.csdn.net/yongxingao/article/details/18227827

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!