可惜没如果=_=
时光的河入海流

题目大意就是给定分子的区间和分母的区间,问你这个分数在化简以后分子和分母之和小于1000的个数。

正常的想法就是枚举分子和分母这个区间,很显然是不行的。

所以我们不能正着考虑。不妨倒着考虑,枚举化简以后的分子和分母,再看其扩大以后分子和分母同时在给定区间内的个数。

对于正着无法结局的问题我们应该考虑反着解决。

 

 1 #include "bits/stdc++.h"
 2 using namespace std;
 3 typedef long long LL;
 4 LL a,b,c,d,ans;
 5 LL gcd(LL x,LL y){return y==0?x:gcd(y,x%y);}
 6 int main(){
 7     LL i,j,x,y;
 8     scanf("%lld%lld%lld%lld",&a,&b,&c,&d);
 9     ans=0;
10     for (i=1;i<1000;i++)
11         for (j=1;j<1000;j++){
12             if (i+j>=1000 || gcd(i,j)!=1) continue;
13             x=max((a-1)/i+1,(c-1)/j+1);
14             y=min(b/i,d/j);
15             ans+=max(0ll,y-x+1);
16 //            cout<<i<<' '<<j<<' '<<max(0ll,y-x+1)<<endl;
17         }
18     printf("%lld",ans);
19     return 0;
20 }

 

posted on 2022-07-12 23:59  珍珠鸟  阅读(27)  评论(0编辑  收藏  举报