萌新求助,TLE5个点

P1072 [NOIP2009 提高组] Hankson 的趣味题

```cpp #include<iostream> #include<cstdio> #include<cctype> using std::swap; namespace xtracer{ #define getchar()(p1==p2&&(p2=(p1=buf)+fread(buf,1,1<<21,stdin),p1==p2)?EOF:*p1++) char buf[1<<21],*p1=buf,*p2=buf; typedef long long ll; void read(ll &n){ char c=getchar();ll x=0;bool f=0; for(;!isdigit(c);c=getchar())f^=!(c^45); for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48); if(f)x=-x;n=x; } void write(ll n){ if(n>=10)write(n/10); putchar(n%10+48); } } using namespace xtracer; ll gcd(ll a,ll b){ if(b>a)swap(a,b); if(b==0)return a; else return gcd(b,a%b); } ll lcm(ll a,ll b){ return a*b/gcd(a,b); } ll n; ll a,a1,b,b1; ll cnt=0; int main(){ read(n); while(n--){ cnt=0; read(a);read(a1);read(b);read(b1); for(ll i=1;i<=b1;i++){ if(gcd(i,a)==a1 and lcm(i,b)==b1)cnt+=1; } write(cnt); putchar('\n'); } return 0; } ``` 加了快读也无济于事
by xtracer @ 2021-02-04 09:27:23


@[xtracer](/user/341049) 只用遍历到sqrt(b)就行了,算两个数
by 王熙文 @ 2021-02-04 10:20:33


@[王熙文](/user/353688) 谢谢
by xtracer @ 2021-02-04 11:09:58


# 快读+O2+枚举b1因数 ```cpp #include<bits/stdc++.h> using namespace std; int gcd(int a,int b){ int s=min(a,b); int big=max(a,b); if(big%s==0)return s; else return gcd(s,big%s); } int lcm(int a,int b){ return a/gcd(a,b)*b; } inline int read(){ int x=0,f=1; char ch=getchar(); while(ch<'0'||ch>'9'){ if(ch=='-') f=-1; ch=getchar(); } while(ch>='0'&&ch<='9'){ x=(x<<1)+(x<<3)+(ch^48); ch=getchar(); } return x*f; } int main(){ int a0,a1,b0,b1; int n; scanf("%d",&n); while(n--){ a0=read();a1=read();b0=read();b1=read(); if(a0==1&&a1==1&&b0==1&&b1==1){ cout<<1; continue; } int cnt=0; for(int i=1;i<=b1/i;i+=1){ if(b1%i==0&&i!=sqrt(b1)){ if(gcd(a0,i)==a1&&lcm(b0,i)==b1){cnt++;} if(gcd(a0,b1/i)==a1&&lcm(b0,b1/i)==b1){cnt++;} } if(i==sqrt(b1)){ if(gcd(a0,i)==a1&&lcm(b0,i)==b1){ cnt++; } } } cout<<cnt<<endl; } } ```
by Jacky2009 @ 2021-08-31 14:06:10


|