poj2891:Strange Way to Express Integers

简介: 题目连接: 分明$excrt$就过了。 为什么还要仔细读题呢?    $qwq$ 反正我没读题然后被卡$long \ long +$输出格式错$……$总共$WA$了四次 怕不是要退役…… 上代码:   #include #include #include using na...

题目连接:

分明$excrt$就过了。

为什么还要仔细读题呢?   

$qwq$

反正我没读题然后被卡$long \ long +$输出格式错$……$总共$WA$了四次

怕不是要退役……

上代码:

 

#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
long long a[100010],r[100010];
int n;
long long exgcd(long long a,long long b,long long &x,long long &y)
{
    if(b==0) return x=1,y=0,a;
    long long tmp=exgcd(b,a%b,y,x);
    y-=a/b*x;
    return tmp;
}
long long slove()
{
    long long M=a[1],R=r[1],x,y,d;
    for(int i=2;i<=n;i++)
    {
        d=exgcd(M,a[i],x,y);
        if((R-r[i])%d!=0) return -1;
        x=(R-r[i])/d*x%a[i];
        R-=x*M;
        M=M/d*a[i];
        R%=M;
    }
    return (R%M+M)%M;
}
int main()
{
    while(~scanf("%d",&n))
    {
        for(int i=1;i<=n;i++)
          scanf("%lld%lld",&a[i],&r[i]);
          printf("%lld\n",slove());
    }
    return 0;
}

 

相关文章
|
6月前
codeforces 285C - Building Permutation
题目大意是有一个含n个数的数组,你可以通过+1或者-1的操作使得其中的数是1--n中的数,且没有重复的数。 既然是这样的题意,那么我就应该把原数组中的数尽量往他最接近1--n中的位置放,然后求差绝对值之和,但有多个数,怎么使他们和最小,这样就要对其进行排序了,直接按大小给它们安排好位置,然后计算。
17 0
|
8月前
UVa11565 - Simple Equations
UVa11565 - Simple Equations
35 0
LeetCode 264. Ugly Number II
编写一个程序,找出第 n 个丑数。 丑数就是只包含质因数 2, 3, 5 的正整数。
52 0
LeetCode 264. Ugly Number II
LeetCode 263. Ugly Number
编写一个程序判断给定的数是否为丑数。 丑数就是只包含质因数 2, 3, 5 的正整数。
71 0
LeetCode 263. Ugly Number
|
Unix Python
LeetCode 71. Simplify Path
给定文件的绝对路径(Unix下的路径)字符串,简化此字符串。
62 0
LeetCode 71. Simplify Path
AtCoder Beginner Contest 214 F - Substrings(subsequence DP)
AtCoder Beginner Contest 214 F - Substrings(subsequence DP)
73 0
|
Perl
AtCoder Beginner Contest 217 F - Make Pair (区间dp)
AtCoder Beginner Contest 217 F - Make Pair (区间dp)
94 0
AtCoder Beginner Contest 176 D - Wizard in Maze(01BFS)
AtCoder Beginner Contest 176 D - Wizard in Maze(01BFS)
89 0
Yet Another Two Integers Problem
Yet Another Two Integers Problem
83 0
Yet Another Two Integers Problem