标签:中国 概述 space oid 防止 同余 攻击 同余方程 lin
\[ \begin{equation} \left\{ \begin{array}{lr} x\equiv a_1(mod\ m_1),\ x\equiv a_2(mod\ m_2),\ x\equiv a_3(mod\ m_3),\ ......\ x\equiv a_n(mod\ m_n).\ \end{array} \right. \end{equation} \]
#include"bits/stdc++.h"
#define int long long
using namespace std;
typedef long long LoveLive;
inline int read()
{
int out=0,fh=1;
char jp=getchar();
while ((jp>'9'||jp<'0')&&jp!='-')
jp=getchar();
if (jp=='-')
{
fh=-1;
jp=getchar();
}
while (jp>='0'&&jp<='9')
{
out=out*10+jp-'0';
jp=getchar();
}
return out*fh;
}
const int MAXN=11;
int a[MAXN],m[MAXN];
int n;
void exgcd(int a,int b,int &x,int &y)
{
if(!b)
{
x=1;
y=0;
return;
}
exgcd(b,a%b,x,y);
int t=x;
x=y;
y=t-(a/b)*y;
}
int crt(int a[],int m[],int n)
{
int res=0,N=1,x,y;
for(int i=1;i<=n;++i)
N*=m[i];
for(int i=1;i<=n;++i)
{
int tmp=N/m[i];
exgcd(m[i],tmp,x,y);
res=(res+y*tmp*a[i])%N;
}
return (res+N)%N;
}
signed main()
{
n=read();
for(int i=1;i<=n;++i)
m[i]=read(),a[i]=read();
int ans=crt(a,m,n);
printf("%lld\n",ans);
return 0;
}
\[ \begin{equation}\left\{ \begin{array}{lr} x\equiv a_1(mod\ m_1),\\ x\equiv a_2(mod\ m_2),\\ x\equiv a_3(mod\ m_3),\\ ......\\ x\equiv a_n(mod\ m_n).\\ \end{array}\right.\end{equation} \]
#include"bits/stdc++.h"
#define int long long
using namespace std;
typedef long long LoveLive;
inline int read()
{
int out=0,fh=1;
char jp=getchar();
while ((jp>'9'||jp<'0')&&jp!='-')
jp=getchar();
if (jp=='-')
{
fh=-1;
jp=getchar();
}
while (jp>='0'&&jp<='9')
{
out=out*10+jp-'0';
jp=getchar();
}
return out*fh;
}
const int MAXN=1e5+10;
int a[MAXN],m[MAXN],n;
int exgcd(int a,int b,int &x,int &y)
{
if(!b)
{
x=1;
y=0;
return a;
}
int d=exgcd(b,a%b,x,y);
int t=x;
x=y;
y=t-(a/b)*y;
return d;
}
int excrt(int a[],int m[],int n)
{
int k1,k2;
for(int i=2;i<=n;++i)
{
int g=exgcd(m[i],m[1],k2,k1);
int c=a[1]-a[i];
if(c%g)
return 0;
k1*=c/g;
k1%=m[i];
a[1]=a[1]+(-k1)*m[1];//注意修改的先后顺序
m[1]=m[1]*m[i]/g;
}
return 1;
}
signed main()
{
while(~scanf("%d",&n))
{
for(int i=1;i<=n;++i)
m[i]=read(),a[i]=read();
int flag=excrt(a,m,n);
if(!flag)
puts("-1");
else
printf("%lld\n",(a[1]%m[1]+m[1])%m[1]);
}
return 0;
}
\[ \begin{equation}\left\{ \begin{array}{lr} x\equiv a_1(mod\ m_1),\\ x\equiv a_2(mod\ m_2),\\ x\equiv a_3(mod\ m_3),\\ ......\\ x\equiv a_n(mod\ m_n).\\ \end{array}\right.\end{equation} \]
#include"bits/stdc++.h"
#define int long long
using namespace std;
typedef long long LoveLive;
inline int read()
{
int out=0,fh=1;
char jp=getchar();
while ((jp>'9'||jp<'0')&&jp!='-')
jp=getchar();
if (jp=='-')
{
fh=-1;
jp=getchar();
}
while (jp>='0'&&jp<='9')
{
out=out*10+jp-'0';
jp=getchar();
}
return out*fh;
}
multiset<int> sword;
multiset<int>::iterator it;
int n,m;
const int MAXN=1e5+10;
int fmul(int a,int b,int mod)
{
assert(mod>0);
a=(a%mod+mod)%mod;
b=(b%mod+mod)%mod;
int res=0;
while(b)
{
if(b&1)
res=(res+a)%mod;
a=(a+a)%mod;
b>>=1;
}
return res;
}
int exgcd(int a,int b,int &x,int &y)
{
if(!b)
{
x=1;
y=0;
return a;
}
int d=exgcd(b,a%b,x,y);
int t=x;
x=y;
y=t-(a/b)*y;
return d;
}
int a[MAXN],p[MAXN],atk[MAXN];
int award[MAXN];
bool check()
{
for(int i=1;i<=n;++i)
if(p[i]!=1)
return false;
return true;
}
void pre()
{
for(int i=1;i<=n;++i)
{
it=sword.upper_bound(a[i]);
if(it!=sword.begin())
--it;
atk[i]=*it;
sword.erase(it);
sword.insert(award[i]);
}
}
void solve_sp()
{
int ans=-1;
for(int i=1;i<=n;++i)
{
ans=max(ans,(a[i]+atk[i]-1)/atk[i]);
}
printf("%lld\n",ans);
}
int A[MAXN],M[MAXN];
int inv(int k,int mod)
{
int x,y;
exgcd(k,mod,x,y);
return (x%mod+mod)%mod;
}
bool build_equations()
{
int x,y;
for(int i=1;i<=n;++i)
{
int g=exgcd(a[i],p[i],x,y);
g=exgcd(g,atk[i],x,y);
atk[i]/=g;
a[i]/=g;
p[i]/=g;
int flag=exgcd(atk[i],p[i],x,y);
if(flag!=1)
return false;
A[i]=fmul(a[i],inv(atk[i],p[i]),p[i]);
M[i]=p[i];
}
return true;
}
void excrt()
{
int k1,k2;
for(int i=2;i<=n;++i)
{
int g=exgcd(M[i],M[1],k2,k1);
int c=A[1]-A[i];
if(c%g)
{
puts("-1");
return;
}
k1=fmul(k1,c/g,M[i]);
/*k1*=c/g;
k1%=M[i];*/
A[1]=A[1]-fmul(k1,M[1],M[1]*(M[i]/g));//打括号,防止爆long long
M[1]=M[1]*(M[i]/g);
A[1]%=M[1];
}
printf("%lld\n",(A[1]%M[1]+M[1])%M[1]);
}
signed main()
{
// freopen("dragon8.in","r",stdin);
int T=read();
while(T--)
{
sword.clear();
n=read();
m=read();
for(int i=1;i<=n;++i)
a[i]=read();
for(int i=1;i<=n;++i)
p[i]=read();
for(int i=1;i<=n;++i)
award[i]=read();
for(int i=1;i<=m;++i)
sword.insert(read());
pre();
if(check())
{
solve_sp();
continue;
}
if(build_equations())
excrt();
else
puts("-1");
}
return 0;
}
标签:中国 概述 space oid 防止 同余 攻击 同余方程 lin
原文地址:https://www.cnblogs.com/jklover/p/10311117.html