UVA10976-Fractions Again?!【枚举】

  1. 链接

    Fractions Again?!

  2. 思路

    因为 1 k − 1 x = 1 y \frac{1}{k}-\frac{1}{x}=\frac{1}{y} k1x1=y1,又因为 x ≥ y x\geq y xy,所以 1 k − 1 y ≤ 1 y \frac{1}{k}-\frac{1}{y}\leq\frac{1}{y} k1y1y1,即 y ≤ 2 k y\leq2k y2k,枚举y判断x即可。

  3. 代码

    #include
    using namespace std;
    
    typedef long long ll;
    const int maxn = 2e5+10;
    const int mod = 1e9+7;
    
    int a[maxn];
    
    int main(void){
    	int k;
    	while(cin>>k){
    		int cnt = 0;
    		for(int y = k+1; y <= 2*k; y++){
    			int a1 = k*y;
    			int a2 = y-k;
    			if(a1%a2==0) cnt++;
    		}
    		cout<<cnt<<endl;
    		for(int y = k+1; y <= 2*k; y++){
    			int a1 = k*y;
    			int a2 = y-k;
    			if(a1%a2==0){
    				int x = a1/a2;
    				printf("1/%d = 1/%d + 1/%d\n",k,x,y); 
    			}
    		}
    	} 
    	return 0;
    }
    

你可能感兴趣的:(枚举)