当前位置:网站首页>[cf571e] geometric progressions -- number theory and prime factor decomposition
[cf571e] geometric progressions -- number theory and prime factor decomposition
2022-06-11 04:23:00 【Ouye xjx】
CF571E Geometric Progressions
Answer key
I looked almost completely at This article explains Taxi .
The product is too large to handle , So we put all a i , b i a_i,b_i ai,bi Do prime factorization .
Then consider merging two series , The result is obvious : Or there is no solution , Or get a number , Or we can get a new sequence of proportional numbers . Our task is to n n n Two series of numbers are merged , Final output ( The first term of the proportional series is output ). For the convenience of , We can regard a number as a common ratio 1 A series of equal proportions .
remember c p ( x ) c_p(x) cp(x) Express x x x Prime number in prime factor decomposition of the p p p The number of times , So let's merge two series a ∗ b ? a*b^? a∗b? and c ∗ d ? c*d^? c∗d? ( The purpose is to find the natural number coefficient x , y x,y x,y bring a ∗ b x = c ∗ d y a*b^x=c*d^y a∗bx=c∗dy) when , Each prime factor can be enumerated p p p, And then discuss the situation :
- c p ( b ) c_p(b) cp(b) and c p ( d ) c_p(d) cp(d) All equal to 0, If at this time c p ( a ) ≠ c p ( c ) c_p(a)\neq c_p(c) cp(a)=cp(c) There must be no solution , Otherwise it doesn't matter ;
- c p ( b ) c_p(b) cp(b) and c p ( d ) c_p(d) cp(d) One of the two is equal to 0, At this time, the coefficient on the non-zero side has been determined ( There may also be no natural number solution , So there's no solution ), The coefficient of zero is to be determined ;
- c p ( b ) c_p(b) cp(b) and c p ( d ) c_p(d) cp(d) None of them 0, At this point, we get the bivariate equation c p ( b ) x − c p ( d ) y = c p ( c ) − c p ( a ) c_p(b)x-c_p(d)y=c_p(c)-c_p(a) cp(b)x−cp(d)y=cp(c)−cp(a), Write it down and put it aside .
After processing , There are several situations :
- unsolvable , Then don't worry ;
- Neither of the two coefficients is determined , In this case, if not both series have only one value , Then there must be several bivariate equations left . After the linear correlation in these equations is de duplicated , If there are two or more equations , Then the coefficient has a unique solution , Substitute judgment ; If there is only one equation , Then we need to use the extended Euclidean algorithm to solve x , y x,y x,y Are non negative integers , And x x x or y y y The smallest solution , Then you can merge a new series of proportional numbers . Let this new sequence of equal proportions be a ′ ∗ b ′ ? a'*b'^? a′∗b′?, So there are c p ( a ′ ) = c p ( a ) + x × c p ( b ) c p ( b ′ ) = lcm ( c p ( b ) , c p ( d ) ) c_p(a')=c_p(a)+x\times c_p(b)\\ c_p(b')=\text{lcm}(c_p(b),c_p(d)) cp(a′)=cp(a)+x×cp(b)cp(b′)=lcm(cp(b),cp(d))
- One of the coefficients is fixed , Then a certain sequence of numbers can be treated as a number , Then repeat the above process . At this time, enumerating prime factor values may appear 1、2 Two cases , So the last two coefficients can be determined , Then we will continue to discuss :
- Both coefficients are fixed , Then take it in for inspection .
In order to facilitate weight removal , I used a large constant map. Finally, according to the prime factor, the output module is decomposed a a a The value of the can .
The code looks long , In fact, most of them are repeaters .
Code
#include<bits/stdc++.h>//JZM yyds!!
#define ll long long
#define lll __int128
#define uns unsigned
#define fi first
#define se second
#define IF (it->fi)
#define IS (it->se)
#define END putchar('\n')
#define lowbit(x) ((x)&-(x))
#define inline jzmyyds
using namespace std;
const int MAXN=114514;
const ll INF=1e18;
ll read(){
ll x=0;bool f=1;char s=getchar();
while((s<'0'||s>'9')&&s>0){
if(s=='-')f^=1;s=getchar();}
while(s>='0'&&s<='9')x=(x<<1)+(x<<3)+(s^48),s=getchar();
return f?x:-x;
}
int readuns(){
int x=0;char s=getchar();
while(s<'0'||s>'9')s=getchar();
while(s>='0'&&s<='9')x=(x<<1)+(x<<3)+(s^48),s=getchar();
return x;
}
int ptf[50],lpt;
void print(ll x,char c='\n'){
if(x<0)putchar('-'),x=-x;
ptf[lpt=1]=x%10;
while(x>9)x/=10,ptf[++lpt]=x%10;
while(lpt>0)putchar(ptf[lpt--]^48);
if(c>0)putchar(c);
}
const ll MOD=1e9+7;
ll ksm(ll a,ll b,ll mo){
ll res=1;
for(;b;b>>=1,a=a*a%mo)if(b&1)res=res*a%mo;
return res;
}
ll exgcd(ll a,ll b,ll&x,ll&y){
if(!b)return x=1,y=0,a;
ll d=exgcd(b,a%b,y,x);
return y-=a/b*x,d;
}
ll gcd(ll a,ll b){
return !b?a:gcd(b,a%b);}
map<ll,ll> getp(ll x){
map<ll,ll>res;
for(ll i=2;i*i<=x;i++)if(x%i==0){
ll&d=res[i]=0;
while(x%i==0)x/=i,d++;
}if(x>1)res[x]=1;
return res;
}
#define pll pair<ll,ll>
struct itn{
ll a,b,c;itn(){
}
itn(ll A,ll B,ll C){
ll d=gcd(gcd(A,B),C);
if(A<0)d=-d;
if(d)a=A/d,b=B/d,c=C/d;
else a=b=c=0;
}
bool operator<(const itn&y)const{
return a<y.a||(a==y.a&&(b<y.b||(b==y.b&&c<y.c)));
}
pll rsl()const{
ll x,y,d=exgcd(a,abs(b),x,y),p=b/d,q=a/d;
if(b<0)y=-y;
if(c%d)return pll(-1,-1);
x*=c/d,y*=c/d;
if(q<0)q=-q,p=-p;
if(y<0)x-=p*((-y+q-1)/q),y+=q*((-y+q-1)/q);
if(p<0)p=-p,q=-q;
if(x<0)y-=q*((-x+p-1)/p),x+=p*((-x+p-1)/p);
if(x>0){
if(q>0)y+=q*(x/p),x-=p*(x/p);
else{
q=-q;ll md=min(x/p,y/q);
x-=p*md,y-=q*md;
}
}
if(x<0||y<0)return pll(-1,-1);
return pll(x,y);
}
};
pll getcro(const itn&p,const itn&q){
ll D=p.a*q.b-p.b*q.a;
if(!D)return pll(-1,-1);
ll D1=p.c*q.b-p.b*q.c,D2=p.a*q.c-p.c*q.a;
if(D<0)D=-D,D1=-D1,D2=-D2;
if(D1<0||D2<0||D1%D||D2%D)return pll(-1,-1);
return pll(D1/D,D2/D);
}
map<ll,ll>a,b;
bool ok=1,hv=0;
int main()
{
for(int N=read();N--;){
ll A=read(),B=read();
if(!ok)continue;
map<ll,ll>c=getp(A),d=getp(B);
for(auto it:d)c[it.fi];
if(!hv){
swap(a,c),swap(b,d),hv=1;continue;}
for(auto it:c)a[it.fi];
set<itn>st;
ll u=-1,v=-1;
for(auto mmp:a){
const ll p=mmp.fi,ci=b[p],cj=d[p];
// printf("\n %lld %lld %lld %lld %lld\n",p,a[p],c[p],ci,cj);
if(!ci&&!cj){
if(a[p]==c[p])continue;
ok=0;break;
}if(!ci){
ll dt=a[p]-c[p];
if(dt<0||dt%cj){
ok=0;break;}
v=dt/cj;continue;
}if(!cj){
ll dt=c[p]-a[p];
if(dt<0||dt%ci){
ok=0;break;}
u=dt/ci;continue;
}st.insert(itn(ci,-cj,c[p]-a[p]));
}
if(!ok)continue;
if(u<0&&v<0){
if(st.empty())continue;
if(st.size()==1){
pll rs=(*st.begin()).rsl();
if(rs.fi<0){
ok=0;continue;}
for(auto mmp:a){
const ll p=mmp.fi,ci=b[p],cj=d[p];
a[p]+=rs.fi*ci;
if(!ci||!cj)b[p]=0;
else b[p]=ci/gcd(ci,cj)*cj;
}continue;
}
itn f=*st.begin(),g=*st.rbegin();
pll rs=getcro(f,g);
if(rs.fi<0){
ok=0;continue;}
for(auto x:st)if(rs.fi*x.a+rs.se*x.b!=x.c)ok=0;
if(!ok)continue;
u=rs.fi,v=rs.se;
}
if(u<0||v<0){
for(auto mmp:a){
const ll p=mmp.fi;
if(u>=0)a[p]+=u*b[p],b[p]=0;
if(v>=0)c[p]+=v*d[p],d[p]=0;
}u=v=0;
for(auto mmp:a){
const ll p=mmp.fi,ci=b[p],cj=d[p];
if(!ci&&!cj){
if(a[p]==c[p])continue;
ok=0;break;
}if(!ci){
ll dt=a[p]-c[p];
if(dt<0||dt%cj){
ok=0;break;}
v=dt/cj;continue;
}if(!cj){
ll dt=c[p]-a[p];
if(dt<0||dt%ci){
ok=0;break;}
u=dt/ci;continue;
}
}if(!ok)continue;
}
if(u>=0&&v>=0){
for(auto mmp:a){
const ll p=mmp.fi;
a[p]+=u*b[p],c[p]+=v*d[p],b[p]=0;
if(a[p]^c[p]){
ok=0;break;}
}
if(!ok)continue;
}
}ll ans=1;
if(!ok)return printf("-1\n"),0;
for(auto it:a)(ans*=ksm(it.fi,it.se,MOD))%=MOD;
print(ans);
return 0;
}
边栏推荐
- Embedded basic interface PWM
- mysql存储过程
- 正大国际;做主帐户需要了解什么
- Feature selection algorithm based on bare bones particleswarm optimization
- Unity message framework notificationcenter
- 正大国际 至秋天的第一个主帐户
- App live broadcast source code, platform login page and password modification page
- Unity advanced backpack system
- 店铺门面转让出租小程序开发制作功能介绍
- Guanghetong officially released the sc126 series of intelligent modules to promote more intelligent connection
猜你喜欢

直播助力杭州电商独角兽冲击上市,分账系统重构电商交易新格局
![[laser principle and application-2]: key domestic laser brands](/img/55/a87169bb75429f323159e3b8627cc6.jpg)
[laser principle and application-2]: key domestic laser brands

Unity 地图映射

一款自适应的聊天网站-匿名在线聊天室PHP源码

Unity item model rotating display

JVM(6):Slot变量槽、操作数栈、代码追踪、栈顶缓存技术

MySQL stored procedure

AI helps release legal potential energy! Release of iterms contract intelligent review system

SQL injection correlation analysis

Guanghetong 5g module fg650-cn and fm650-cn series are produced in full scale, accelerating the efficient implementation of 5g UWB applications
随机推荐
域名解析耗时是什么?域名解析耗时影响因素有哪些?
给你一个项目,你将如何开展性能测试工作?
Unity message framework notificationcenter
超简单 CameraX 人脸识别效果封装
司马炎爷爷 告诉你什么叫做内卷!
Esp32 development -lvgl uses internal and external fonts
众昂矿业:氟化工是萤石的主要消耗领域
Image detection related model data format
数据分析师必知必会的统计学知识
直播助力杭州电商独角兽冲击上市,分账系统重构电商交易新格局
d结构用作多维数组的索引
27 pieces of advice for communication young people
Unity 传送机制的实现
What is the time-consuming domain name resolution? What are the influencing factors of domain name resolution time?
Golang generics: generics
Given a project, how will you conduct performance testing?
App live broadcast source code, platform login page and password modification page
Matter protocol
[激光器原理与应用-2]:国内激光器重点品牌
Guanghetong successfully won the bidding for China Unicom Yanfei CAT1 customized module with the largest share