upc组队赛14 As rich as Crassus【扩展中国剩余定理】
As rich as Crassus
题目链接
题目描述
Crassus, the richest man in the world, invested some of his money with the Very Legitimate International Bank. The Bank offered a remarkable interest rate. They promised that given an initial investment of x, the balance at the beginning of the nth year would be xn (counting the first year as year 1).
At the beginning of the 3rd year, there is a problem. It turns out that this investment opportunity is too good to be true, and it is actually a fraud. The Bank has spent all the money, and the directors have disappeared. Since Crassus is very rich, the Government decides to help him. They will pay him back his initial deposit out of taxpayers’ money.
The Bank has lost all records of Crassus’ original deposit, but does have information about what Crassus’ current deposit value should be. This information is stored on 3 separate computers. Unfortunately, each computer only has a limited amount of memory, and is also very badly designed, so each computer stores integers modulo Ni, for i = 1,2,3. Though these values are all large enough to correctly store the initial value x, Crassus now has so much money ‘invested’ with the Bank that the computers don’t have enough memory to store it correctly. I.e. x3>Ni for all i = 1,2,3.
As the government official in charge of giving Crassus his initial deposit back, you must find the value of the original x that he invested. You know the numbers N1,N2,N3, and the value x3 mod Ni for all i. You also read in the documentation for the computers that the numbers N1,N2,N3, have the property that if p is a prime number and p divides Ni, then p does not divide Nj for all i ≠ j.
输入
The first line contains a single number T indicating the number of test cases (0 < T ≤ 10).
The next 2T lines of input come in pairs as follows. The first line of each pair contains three numbers N1,N2,N3, separated by spaces (0 < Ni < 231 for all i).
The second line of each pair contains the values x3 mod Ni for each i, again separated by spaces.
输出
The value of x for each test case, written out in full, each on a new line.
样例输入
2
6 11 19
5 4 11
25 36 7
16 0 6
样例输出
5
6
题解
扩展剩余定理 板子来自传送门
代码
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,n) for(int i=a;i<n;i++)
#define scac(x) scanf("%c",&x)
#define sca(x) scanf("%d",&x)
#define sca2(x,y) scanf("%d%d",&x,&y)
#define sca3(x,y,z) scanf("%d%d%d",&x,&y,&z)
#define scl(x) scanf("%lld",&x)
#define scl2(x,y) scanf("%lld%lld",&x,&y)
#define scl3(x,y,z) scanf("%lld%lld%lld",&x,&y,&z)
#define pri(x) printf("%d\n",x)
#define pri2(x,y) printf("%d %d\n",x,y)
#define pri3(x,y,z) printf("%d %d %d\n",x,y,z)
#define prl(x) printf("%lld\n",x)
#define prl2(x,y) printf("%lld %lld\n",x,y)
#define prl3(x,y,z) printf("%lld %lld %lld\n",x,y,z)
#define mst(x,y) memset(x,y,sizeof(x))
#define ll long long
#define LL long long
#define pb push_back
#define mp make_pair
#define P pair<double,double>
#define PLL pair<ll,ll>
#define PI acos(1.0)
#define eps 1e-6
#define inf 1e17
#define mod 1e9+7
#define INF 0x3f3f3f3f
#define N 1005
const int maxn = 5000005;
ll m[105],a[105],n=3;
LL exgcd(LL a,LL b,LL &x,LL &y){
if(!b){x=1,y=0;return a;}
LL re=exgcd(b,a%b,x,y),tmp=x;
x=y,y=tmp-(a/b)*y;
return re;
}
LL work(){
LL M=m[1],A=a[1],t,d,x,y;int i;
for(i=2;i<=n;i++){
d=exgcd(M,m[i],x,y);
if((a[i]-A)%d) return -1;
x*=(a[i]-A)/d,t=m[i]/d,x=(x%t+t)%t;
A=M*x+A,M=M/d*m[i],A%=M;
}
A=(A%M+M)%M;
return A;
}
int main()
{
int t;
sca(t);
while(t--)
{
rep(i,1,n+1) scl(m[i]);
rep(i,1,n+1) scl(a[i]);
ll ans = work();
if(ans==-1)
printf("-1\n");
else
{
if(!ans)
printf("0\n");
else
{
ll tmp = pow(ans,1.0/3.0);
if(tmp * tmp * tmp < ans)
printf("%lld\n", tmp + 1);
else
printf("%lld\n", tmp);
}
}
}
return 0;
}
upc组队赛14 As rich as Crassus【扩展中国剩余定理】的更多相关文章
- 扩展中国剩余定理 (exCRT) 的证明与练习
原文链接https://www.cnblogs.com/zhouzhendong/p/exCRT.html 扩展中国剩余定理 (exCRT) 的证明与练习 问题模型 给定同余方程组 $$\begin{ ...
- P4777 【模板】扩展中国剩余定理(EXCRT)/ poj2891 Strange Way to Express Integers
P4777 [模板]扩展中国剩余定理(EXCRT) excrt模板 我们知道,crt无法处理模数不两两互质的情况 然鹅excrt可以 设当前解到第 i 个方程 设$M=\prod_{j=1}^{i-1 ...
- (伪)再扩展中国剩余定理(洛谷P4774 [NOI2018]屠龙勇士)(中国剩余定理,扩展欧几里德,multiset)
前言 我们熟知的中国剩余定理,在使用条件上其实是很苛刻的,要求模线性方程组\(x\equiv c(\mod m)\)的模数两两互质. 于是就有了扩展中国剩余定理,其实现方法大概是通过扩展欧几里德把两个 ...
- P4777 【模板】扩展中国剩余定理(EXCRT)
思路 中国剩余定理解决的是这样的问题 求x满足 \[ \begin{matrix}x \equiv a_1(mod\ m_1)\\x\equiv a_2(mod\ m_2)\\ \dots\\x\eq ...
- P4777 【模板】扩展中国剩余定理(EXCRT)&& EXCRT
EXCRT 不保证模数互质 \[\begin{cases} x \equiv b_1\ ({\rm mod}\ a_1) \\ x\equiv b_2\ ({\rm mod}\ a_2) \\ ... ...
- [poj2891]Strange Way to Express Integers(扩展中国剩余定理)
题意:求解一般模线性同余方程组 解题关键:扩展中国剩余定理求解.两两求解. $\left\{ {\begin{array}{*{20}{l}}{x = {r_1}\,\bmod \,{m_1}}\\{ ...
- 欧几里得(辗转相除gcd)、扩欧(exgcd)、中国剩余定理(crt)、扩展中国剩余定理(excrt)简要介绍
1.欧几里得算法(辗转相除法) 直接上gcd和lcm代码. int gcd(int x,int y){ ?x:gcd(y,x%y); } int lcm(int x,int y){ return x* ...
- poj 2891 Strange Way to Express Integers【扩展中国剩余定理】
扩展中国剩余定理板子 #include<iostream> #include<cstdio> using namespace std; const int N=100005; ...
- hdu 1573 X问题【扩展中国剩余定理】
扩展中国剩余定理的板子,合并完之后算一下范围内能取几个值即可(记得去掉0) #include<iostream> #include<cstdio> #include<cm ...
随机推荐
- java虚拟机笔记-1
java虚拟机学习笔记 Java技术的核心就是Java虚拟机,因为所有的Java程序都在虚拟机上运行.Java程序的运行需要Java虚拟机.Java API和Java Class文件的配合.Java虚 ...
- DP50题(转)
转自https://www.luogu.org/blog/cccx2016/dp50-ti-ti-hao dp50题: poj1014 poj1015 poj1018 poj1036 poj1038 ...
- docker pull理解误区
docker run 命令 如果local image中有对应 镜像+tag 不会从新拉取镜像 docker pull 会进行拉取 先进行镜像更改 [root@master01 ~]# docker ...
- python学习第二十九天函数局部变量如何改变外部变量
python函数局部变量如何改变外部变量,之前我说过,局部变量是没办法改变外部变量的,除非局部变量找不到,去外部找,输出变量,使用关键词global 使变量改变外部变量. 1,使用关键词global ...
- Windows程序设计--(五)绘图基础
5.1 GDI的结构 图形设备接口(GDI:Graphics Device Interface)是Windows的子系统,它负责在视讯显示器和打印机上显示图形. 5.2 设备环境 5.2.1 获取设备 ...
- windos忘记密码登陆如何修复
一.简单的方法: 开机启动windows,进入欢迎界面后,会出现输入用户名密码提示框,这时候,同时按住Ctrl+Alt+Delete,会跳出一个账号窗口,输入用户名:administer,按回车即可. ...
- springcloud费话之配置中心基础(SVN)
目录: springcloud费话之Eureka基础 springcloud费话之Eureka集群 springcloud费话之Eureka服务访问(restTemplate) springcloud ...
- 道路识别demo
最近做的道路识别一开始终于弄懂了点东西,一开始在网上找到了一个简单的道路识别的opencvsharp的版本.我觉得opencvsharp真的是一个很好的东西,它封装了比opencv更多的数据结构和库, ...
- pip命令一般使用
pip类似RedHat里面的yum,安装Python包非常方便.本节详细介绍pip的安装.以及使用方法. 1.pip下载安装 1.1 pip下载 1 # wget "https://py ...
- java 两个对象共使一个方法