【POJ2891】Strange Way to Express Integers(拓展CRT)

【POJ2891】Strange Way to Express Integers(拓展CRT)

题面

Vjudge
板子题。

题解

拓展\(CRT\)模板题。

#include<iostream>
#include<cstdio>
using namespace std;
#define ll long long
#define MAX 111111
ll exgcd(ll a,ll b,ll &x,ll &y)
{
    if(!b){x=1,y=0;return a;}
    ll d=exgcd(b,a%b,y,x);
    y-=a/b*x;return d;
}
int n;
ll m[MAX],a[MAX];
int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        bool fl=true;ll x,y;
        for(int i=1;i<=n;++i)scanf("%lld%lld",&m[i],&a[i]);
        for(int i=2;i<=n;++i)
        {
            ll d=exgcd(m[1],m[i],x,y),g=a[i]-a[1],t;
            if(g%d){fl=false;break;}
            x*=g/d;t=m[i]/d;x=(x%t+t)%t;
            a[1]+=x*m[1];m[1]*=t;a[1]%=m[1];
        }
        if(!fl)puts("-1");
        else printf("%lld\n",(a[1]%m[1]+m[1])%m[1]);
    }
    return 0;
}

原文地址:https://www.cnblogs.com/cjyyb/p/9316034.html

时间: 2024-07-30 11:27:52

【POJ2891】Strange Way to Express Integers(拓展CRT)的相关文章

解题报告 之 POJ2891 Strange Way to Express Integers

解题报告 之 POJ2891 Strange Way to Express Integers 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 a1, a2, 

POJ2891——Strange Way to Express Integers(模线性方程组)

Strange Way to Express Integers DescriptionElina 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 a1, a2, …, ak. For some n

【EXCRT模板】POJ2891/LuoGu4777Strange Way to Express Integers拓展中国剩余定理

这道题需要exgcd的基础 POJ的题干描述十分恶心 Strange Way to Express Integers Time Limit: 1000MS Memory Limit: 131072K Total Submissions: 21217 Accepted: 7120 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negati

POJ2891 Strange Way to Express Integers 扩展欧几里德 中国剩余定理

欢迎访问~原文出处--博客园-zhouzhendong 去博客园看该题解 题目传送门 - POJ2891 题意概括 给出k个同余方程组:x mod ai = ri.求x的最小正值.如果不存在这样的x,那么输出-1.不满足所有的ai互质. 题解 互质就简单,但是不互质就有些麻烦,到现在我还是不大懂. 具体证明可以求教大佬,如果我懂了,会更新的. 代码 #include <cstring> #include <cstdio> #include <algorithm> #in

POJ2891 Strange Way to Express Integers【一元线性同余方程组】

题目链接: http://poj.org/problem?id=2891 题目大意: 选择k个不同的正整数a1.a2.-.ak,对于某个整数m分别对ai求余对应整数ri,如果 适当选择a1.a2.-.ak,那么整数m可由整数对组合(ai,ri)唯一确定. 若已知a1.a2.-.ak以及m,很容易确定所有的整数对(ai,ri),但是题目是已知a1. a2.-.ak以及所有的整数对(ai,ri),求出对应的非负整数m的值. 思路: 题目可以转换为给定一系列的一元线性方程 x ≡ r1( mod a1

poj2891 Strange Way to Express Integers

扩展欧几里得,注意防溢出. http://poj.org/problem?id=2891 1 #include <cstdio> 2 using namespace std; 3 typedef __int64 LL; 4 const int maxn = 1e5 + 10; 5 6 LL a[maxn], r[maxn]; 7 int n; 8 LL egcd(LL a, LL b, LL& x, LL& y){ 9 if(!b){ 10 x = 1, y = 0; 11 r

POJ.2891.Strange Way to Express Integers(扩展CRT)

题目链接 扩展中国剩余定理:1(直观的).2(详细证明). #include <cstdio> #include <cctype> #define gc() getchar() typedef long long LL; const int N=1e6+5; LL n,m[N],r[N]; inline LL read() { LL now=0,f=1;register char c=gc(); for(;!isdigit(c);c=gc()) if(c=='-') f=-1; f

poj——2891 Strange Way to Express Integers

Strange Way to Express Integers Time Limit: 1000MS   Memory Limit: 131072K Total Submissions: 16839   Accepted: 5625 Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers. The way is

数论F - Strange Way to Express Integers(不互素的的中国剩余定理)

F - Strange Way to Express Integers Time Limit:1000MS     Memory Limit:131072KB     64bit IO Format:%I64d & %I64u Submit Status Description Elina is reading a book written by Rujia Liu, which introduces a strange way to express non-negative integers.