POJ 2891 Strange Way to Express Integers

本文探讨了一种独特的整数表达方法,通过选择不同的正整数进行除法操作,从而得到余数,进而推导出原始整数。通过具体实例和输入输出样例,详细解释了解题步骤和技巧。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值