lightoj 1319 - Monkey Tradition

可以用中国剩余定理也可以用线性同余方程组,时间分别是0.028和0.036

在这个问题里逆元一定有解

#include<bits/stdc++.h>
using namespace std;

long long p[15];//ans==r[i](mod p[i])
long long r[15];
int n;
void extgcd(long long a,long long b,long long &d,long long &x,long long &y)//return gcd(a,b)(==a*x+b*y)
{
    if(!b)
        d=a,x=1,y=0;
    else
    {
        extgcd(b,a%b,d,y,x);
        y-=x*(a/b);
    }
}
long long chinese_remainder()
{
    long long M=1;
    for(int i=0; i<n; i++)
        M*=p[i];
    long long ans=0;
    long long m;
    long long d,x,y;
    for(int i=0; i<n; i++)
    {
        m=M/p[i];
        extgcd(m,p[i],d,x,y);
        ans=(ans+r[i]*m*x)%M;
    }
    return (ans+M)%M;
}
int main(void)
{
    int t,Case=0;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        for(int i=0; i<n; i++)
            scanf("%lld%lld",&p[i],&r[i]);
        printf("Case %d: %lld\n",++Case,chinese_remainder());
    }
    return 0;
}
#include<bits/stdc++.h>
using namespace std;

vector<long long>A,B,M;//A[i]*x==B[i](mod M[i])
long long gcd(long long a,long long b)
{
    return b==0?a:gcd(b,a%b);
}
void extgcd(long long a,long long b,long long &d,long long &x,long long &y)//return gcd(a,b)(==a*x+b*y)
{
    if(!b)
        d=a,x=1,y=0;
    else
    {
        extgcd(b,a%b,d,y,x);
        y-=x*(a/b);
    }
}
long long mod_inverse(long long a,long long m)
{
    long long x,y,d;
    extgcd(a,m,d,x,y);
    return (x%m+m)%m;
}
pair<long long,long long>linear_congruence(const vector<long long>&A,const vector<long long>&B,const vector<long long>&M)
{
    long long x=0,m=1;
    for(int i=0; i<A.size(); i++)
    {
        long long a=A[i]*m,b=B[i]-A[i]*x,d=gcd(M[i],a);
        if(b%d!=0)return pair<long long,long long>(0,-1);
        long long t=b/d*mod_inverse(a/d,M[i]/d)%(M[i]/d);
        x=x+m*t;
        m*=M[i]/d;
    }
    return pair<long long,long long>((x%m+m)%m,m);
}
int main(void)
{
    int t,Case=0;
    int n;
    scanf("%d",&t);
    while(t--)
    {
        scanf("%d",&n);
        long long m,b;
        A.clear();
        B.clear();
        M.clear();
        for(int i=0; i<n; i++)
        {
            scanf("%lld%lld",&m,&b);
            A.push_back(1);
            B.push_back(b);
            M.push_back(m);
        }
        printf("Case %d: %lld\n",++Case,linear_congruence(A,B,M).first);
    }
    return 0;
}

时间: 2024-10-11 13:31:56

lightoj 1319 - Monkey Tradition的相关文章

lightoj 1319 - Monkey Tradition (中国剩余定理)

1319 - Monkey Tradition PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB In 'MonkeyLand', there is a traditional game called "Bamboo Climbing". The rules of the game are as follows: 1)       There are N monkeys who play

Light OJ 1319 Monkey Tradition(中国剩余定理)

In 'MonkeyLand', there is a traditional game called "Bamboo Climbing". The rules of the game are as follows: 1)       There are N monkeys who play this game and there are N bamboos of equal heights. Let the height be L meters. 2)       Each monk

Monkey Tradition(中国剩余定理)

Monkey Tradition Time Limit: 2000MS   Memory Limit: 32768KB   64bit IO Format: %lld & %llu Submit Status Description In 'MonkeyLand', there is a traditional game called "Bamboo Climbing". The rules of the game are as follows: 1)       There

(light oj 1319) Monkey Tradition 中国剩余定理(CRT)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1319 In 'MonkeyLand', there is a traditional game called "Bamboo Climbing". The rules of the game are as follows: 1) There are N monkeys who play this game and there are N bamboos of equal h

LightOJ 1004 Monkey Banana Problem (DP 数字三角形)

1004 - Monkey Banana Problem PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You are in the world of mathematics to solve the great "MonkeyBanana Problem". It states that, a monkey enters into a diamond shaped twodimen

Lightoj 1004 - Monkey Banana Problem

1004 - Monkey Banana Problem    PDF (English) Statistics Forum Time Limit: 2 second(s) Memory Limit: 32 MB You are in the world of mathematics to solve the great "Monkey Banana Problem". It states that, a monkey enters into a diamond shaped two

LightOJ 1004 - Monkey Banana Problem 【DP】

题目链接:http://www.lightoj.com/volume_showproblem.php?problem=1004 题意:两个数塔相接,求最大的路径和. 解法:简单DP. 代码: #include <stdio.h> #include <ctime> #include <math.h> #include <limits.h> #include <complex> #include <string> #include <

(LightOJ 1004) Monkey Banana Problem 简单dp

You are in the world of mathematics to solve the great "Monkey Banana Problem". It states that, a monkey enters into a diamond shaped two dimensional array and can jump in any of the adjacent cells down from its current position (see figure). Wh

LightOJ - 1370 Bi-shoe and Phi-shoe

题目链接:http://lightoj.com/login_main.php?url=volume_showproblem.php?problem=1370 题目大意:给N个数a[i], N <= 1e6,问使 Φ(x) >= a[i] 成立的最小x的所有x的和是多少. 解题思路:我们知道的是对于素数 m 来说,phi[m] = m - 1.另一方面,对于一个合数 m 来说, phi[m] < phi[x] , x > m && x 是素数. 因此,我们可以认为,每