7-1 Conway's Conjecture (20分)

John Horton Conway, a British mathematician active in recreational mathematics, proposed a conjecture in 2014: arrange the factors of any given number in ascending order, and pull the exponents down, we can get another number. Keep doing so we must end up at a prime number. For example:

18=2×3​2​​

232=2​3​​×29

2329=17×137

17137 is a prime.

Now you are supposed to write a program to make one step verification of this conjecture. That is, for any given positive integer N, you must factorize it, and then test if the number obtained from its factors is a prime.

By the way, this conjecture has been proven false by James Davis, who has discovered a counter example: 135323853961879=13×53​2​​×3853×96179. Alas …

Input Specification:

Each input file contains one test case which gives a positive integer N (<10​5​​).

Output Specification:

For each case, first print in a line the number obtained from N’s factors. The in the next line, print Yes if the above number is a prime, or No if not.

Sample Input 1:

2329

Sample Output 1:

17137
Yes

Sample Input 2:

124

Sample Output 2:

2231
No

Sample Input 3:

87516

Sample Output 3:

2232111317
No
  • 思路:
    step 1: 分解因子(好像可以不是质因子)
    step 2: 用to_string将因子拼接起来(注意超过int范围,要用long long 存储)
    step 3: 判断拼接后是不是素数

  • T1 code:

#include <bits/stdc++.h>
using namespace std;
const int maxn = 100010;
struct factor{
	int x, cnt;
}fac[maxn];
int prime[maxn];
bool isPrime(int x){
	if(x == 1) return false;
	for(int i = 2; i * i <= x; ++i){
		if(x % i == 0) return false;
	}
	return true;
}
int Num = 0;
void init(){
	for(int i = 1; i < maxn; ++i){
		if(isPrime(i)){
			prime[Num++] = i;
		}
	}
}

int main(){
	int n, num = 0;
	scanf("%d", &n);
	init();
	int sqr = (int)sqrt(1.0 * n); 
	for(int i = 0; i < Num && prime[i] <= n; ++i){
		if(n % prime[i] == 0){
			fac[num].x = prime[i];
			fac[num].cnt = 0;
			while(n % prime[i] == 0){
				fac[num].cnt++;
				n /= prime[i];
			}
			num++;
		}
		if(n == 1) break;
	}
	string ans;
	for(int i = 0; i < num; ++i){
		ans += to_string(fac[i].x);
		if(fac[i].cnt != 1) ans += to_string(fac[i].cnt);
	}
	cout << ans <<endl;
	int res = stoi(ans);
	printf("%s", isPrime(res) ? "Yes" : "No");
	return 0;
}
  • T2 code:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 1010;
struct Fac
{
    int f, cnt;
}fac[maxn];
bool isPrime(ll x)
{
    if(x < 2) return false;
    ll sqr = sqrt(1.0 * x);
    for(int i = 2; i <= sqr; ++i)
    {
        if(x % i == 0) return false;
    }
    return true;
}
ll CatNum(int idex)
{
    string ans;
    for(int i = 0; i < idex; ++i)
    {
    	string tmpf = to_string(fac[i].f), tmpcnt = fac[i].cnt == 1 ? "" : to_string(fac[i].cnt);
    	ans += tmpf + tmpcnt;
    }
    return stoll(ans);
}
int main()
{
    int n, idex = 0;
    scanf("%d", &n);
    if(n == 1)	//case 4: 质数相关题:一定要检查下0, 1, 还有MAX 
    {
    	printf("1\nNo");
    	return 0;
	}
    int sqr = sqrt(1.0 * n);
    for(int i = 2; i <= sqr; ++i)
    {
        if(n % i == 0)
        {
            fac[idex].f = i;
            while(n % i == 0)
            {
                n /= i;
                fac[idex].cnt++;
            }
            idex++;
        }
    }
    if(n != 1)
    {
        fac[idex].f = n;
        fac[idex++].cnt = 1;
    }
    ll ans = CatNum(idex);
	printf("%lld\n", ans);
	printf("%s", isPrime(ans) ? "Yes" : "No");
    return 0;
}


Wrong: 没有对1单独处理

发布了316 篇原创文章 · 获赞 5 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_42347617/article/details/105252026