列表

详情


NC54304. Fractions

描述

You are given a positive integer n.
Find a sequence of fractions ai / bi, i = 1…k (where ai and bi are positive integers) for some k such that:
  • bi divides n, 1 < bi < n for i = 1…k
  • 1 ≤ ai < bi for i = 1…k

输入描述

The input consists of a single integer n (2 ≤ n ≤ 109).

输出描述

In the first line print “YES” if there exists such a sequence of fractions or “NO” otherwise.
If there exists such a sequence, next lines should contain a description of the sequence in the following format.
The second line should contain integer k (1 ≤ k ≤ 100 000) — the number of elements in the sequence. It is guaranteed that if such a sequence exists, then there exists a sequence of length at most 100 000.
Next k lines should contain fractions of the sequence with two integers ai and bi on each line.

示例1

输入:

2

输出:

NO

示例2

输入:

6

输出:

YES
2
1 2
1 3

说明:

In the second example there is a sequence 1/2, 1/3 such that 1/2 + 1/3 = 1 − 1/6

原站题解

上次编辑到这里,代码来自缓存 点击恢复默认模板

C++14(g++5.4) 解法, 执行用时: 3ms, 内存消耗: 500K, 提交时间: 2020-10-06 13:35:03

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
const int p = 1e9 + 7;

int main()
{
    ll n;
    scanf("%lld",&n);
    ll m=n-1;

    ll x=1;
    for(ll i=2;i*i<=n;i++){
        if(n%i) continue;
        while(n%i==0) x*=i,n/=i;
        break;
    }

    if(n==1||x==1){
        cout<<"NO"<<endl;
        return 0;
    }
    for(int i=1;i<x;i++){
        if((m-n*i)%x==0){

            printf("YES\n2\n");
        cout<<i<<" "<<x<<endl;
        cout<<(m-n*i)/x<<" "<<n<<endl;
        }
    }

}

C++(clang++11) 解法, 执行用时: 10ms, 内存消耗: 504K, 提交时间: 2020-10-26 22:05:29

#include<bits/stdc++.h>

#define ll long long

using namespace std;

ll n;

int main()
{
	scanf("%lld",&n);
	for (ll i=2;i<=sqrt(n);i++)
		if(n%i==0)
			for (ll j=1;j<i;j++)
				if((n-1-j*n/i)%i==0)
				{
					printf("YES\n2\n%lld %lld\n%lld %lld\n",j,i,(n-1-j*n/i)/i,n/i);
					return 0;
				}

	puts("NO");

	return 0;
}

上一题