P2152 [SDOI2009] SuperGCD - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
该题求两个大数的最大公约数,辗转相除法不适用,要用到辗转相减法;
即对于两个数a,b有四种情况:
a是奇数,b是偶数:gcd(a,b/2)
a是偶数,b是奇数:gcd(a/2,b)
a是偶数,b是偶数:2*gcd(a/2,b/2)
a是奇数,b是奇数:gcd(b,a-b);
不会写压位高精,先放着
P1072 [NOIP2009 提高组] Hankson 的趣味题 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn)
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using ld = long double;
using ull = unsigned long long;
/*
由于x与b0的最小公倍数是b1
则x必定是b1的一个因子
先找出b1的所以因子,然后遍历找出其中符合题目条件的即可
*/
ll gcd(ll a,ll b) {
ll c;
while(b) {
c=a%b;
a=b;
b=c;
}
return a;
}
map<int,int>mp;
void fen(ll x) {
for(int i=1; i*i<=x; i++) {
if(x%i==0) {
mp[i]++;
if(i*i==x) {
continue;
}
mp[x/i]++;
}
}
}
//ll dd[100000];
void solve() {
ll a0,a1,b0,b1,i,j,u=0;
cin>>a0>>a1>>b0>>b1;
fen(b1);
for(auto b:mp) {
ll c1=b.first;
if(gcd(a0,c1)==a1&&c1*b0/gcd(c1,b0)==b1) {
// cout<<c1<<" ";
u++;
}
}
mp.clear();
cout<<u<<endl;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int t;
cin>>t;
while(t--) {
solve();
}
}