1635:例 5Strange Way to Express Integers

Posted smartljy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了1635:例 5Strange Way to Express Integers相关的知识,希望对你有一定的参考价值。

#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll n,m,a,lcm,now;
bool flag;
void exgcd(ll a,ll b,ll &d,ll &x,ll &y)

    if(b==0)
    
        d=a;
        x=1;
        y=0;
    
    else
    
        exgcd(b,a%b,d,x,y);
        ll t=x;
        x=y;
        y=t-a/b*y;
    

int main()

    ll d,x,y,k;
    while(~scanf("%lld",&n))
    
        scanf("%lld%lld",&m,&a);
        lcm=m;now=a;flag=true;
        for(int i=1;i<n;i++)
        
            scanf("%lld%lld",&m,&a);
            a=((a%m)-(now%m)+m)%m;
            exgcd(lcm,m,d,x,y);
            if(a%d==0)
            k=(x*(a/d)%(m/d)+(m/d))%(m/d);
            else flag=false;
            now+=k*lcm;
            lcm=lcm*m/d;
            now=((now%lcm)+lcm)%lcm;
        
        if(!flag) printf("-1");
        else printf("%lld",now);
        puts("");
    
    return 0;

 

以上是关于1635:例 5Strange Way to Express Integers的主要内容,如果未能解决你的问题,请参考以下文章

LeetCode 1055. Shortest Way to Form String

Kappa Architecture: A Different Way to Process Data

Leetcode: Shortest Way to Form String

A. The Way to Home

A way to connect to network through WIFI on Centos 7

(26)A delightful way to teach kids about computers