zoukankan      html  css  js  c++  java
  • [TJOI2009]猜数字

    Description
    现有两组数字,每组k个,第一组中的数字分别为:a1,a2,...,ak表示,第二组中的数字分别用b1,b2,...,bk表示。其中第二组中的数字是两两互素的。求最小的非负整数n,满足对于任意的i,n - ai能被bi整除。

    Input
    输入数据的第一行是一个整数k,(1 ≤ k ≤ 10)。接下来有两行,第一行是:a1,a2,...,ak,第二行是b1,b2,...,bk

    Output
    输出所求的整数n。

    Sample Input
    3
    1 2 3
    2 3 5

    Sample Output
    23


    (b_i|n-a_i),意味着(nequiv a_i(\%b_i)),所以我们列个方程

    [egin{cases}nequiv a_1(\%b_1) onumber\nequiv a_2(\%b_2) onumber\...end{cases} ]

    直接用crt求解即可

    /*program from Wolfycz*/
    #include<cmath>
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define inf 0x7f7f7f7f
    using namespace std;
    typedef long long ll;
    typedef long double ld;
    typedef unsigned int ui;
    typedef unsigned long long ull;
    inline char gc(){
    	static char buf[1000000],*p1=buf,*p2=buf;
    	return p1==p2&&(p2=(p1=buf)+fread(buf,1,1000000,stdin),p1==p2)?EOF:*p1++;
    }
    template<typename T>inline T frd(T x){
    	int f=1; char ch=gc();
    	for (;ch<'0'||ch>'9';ch=gc())	if (ch=='-')    f=-1;
    	for (;ch>='0'&&ch<='9';ch=gc())	x=(x<<1)+(x<<3)+ch-'0';
    	return x*f;
    }
    template<typename T>inline T read(T x){
    	int f=1;char ch=getchar();
    	for (;ch<'0'||ch>'9';ch=getchar())	if (ch=='-')	f=-1;
    	for (;ch>='0'&&ch<='9';ch=getchar())	x=(x<<1)+(x<<3)+ch-'0';
    	return x*f;
    }
    inline void print(int x){
    	if (x<0)    putchar('-'),x=-x;
    	if (x>9)	print(x/10);
    	putchar(x%10+'0');
    }
    namespace Math{
    	ll mlt(ll _a,ll _b,ll _p){
    		ll _c=(ld)_a*_b/_p;
    		ll _Ans=_a*_b-_c*_p;
    		if (_Ans<0)	_Ans+=_p;
    		return _Ans;
    	}
    	ll gcd(ll a,ll b){return !b?a:gcd(b,a%b);}
    	void exgcd(ll a,ll b,ll &x,ll &y){
    		if (!b){x=1,y=0;return;}
    		exgcd(b,a%b,x,y);
    		ll t=x; x=y,y=t-a/b*y;
    	}
    	ll Ex_GCD(ll a,ll b,ll c){
    		ll d=gcd(a,b),x,y;
    		if (c%d)	return -1;
    		a/=d,b/=d,c/=d;
    		exgcd(a,b,x,y);
    		x=(mlt(x,c,b)+b)%b;
    		return x;
    	}
    }
    int A[15],m[15];
    int main(){
    	int n=read(0); ll SM=1,Ans=0;
    	for (int i=1;i<=n;i++)	A[i]=read(0);
    	for (int i=1;i<=n;i++)	SM*=(m[i]=read(0));
    	for (int i=1;i<=n;i++){
    		ll M=SM/m[i],res=Math::mlt(A[i],Math::Ex_GCD(M,m[i],1),SM);
    		Ans=(Ans+Math::mlt(res,M,SM))%SM;
    	}
    	printf("%lld
    ",Ans);
    	return 0;
    }
    
  • 相关阅读:
    python3给socket模块设置代理
    yield、greenlet与协程gevent
    线程池
    并发通信、生产者与消费者模型
    多进程和多线程
    非阻塞套接字与IO多路复用
    14.python模块之subprocess
    判断页面是否滑到底部
    @vue/cli 3.x 版本配置productionGzip提高性能
    vue跳转到指定位置
  • 原文地址:https://www.cnblogs.com/Wolfycz/p/10630150.html
Copyright © 2011-2022 走看看