一些因为它是素数件 [英] A number as it's prime number parts

查看:115
本文介绍了一些因为它是素数件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要打印的方式,你可以重新present一个给定的数字,因为它是素数部分的数量。

让我澄清一下:比方说,我一直在考虑这个号码7。现在,首先,我要找到所有的质数小于7,这是2,3和5。现在,在多少我的方法可以总结这些数字(我可以使用同一个号码多次我想要的),这样​​的结果等于7?例如,数7具有五种方式:

  2 + 2 + 3
2 + 3 + 2
2 + 5
3 + 2 + 2
5 + 2
 

我完全有这个任务丢失。首先,我想我会做有用元素的数组像这样:{2,2,2,3,3,5}(7/2 = 3,所以2必须出现三次,同去同3,这得到两张事件再度发生)。在此之后,通过数组循环,并选择领导者,确定多远,我们在数组中的。我知道这个解释是可怕的,所以这里的code:

 的#include<的iostream>
#包括<载体>

INT primes_all [25] = {2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79 ,83,89,97};

诠释的main()
{
    INT编号;
    给std :: cin>>数;

    的std ::矢量< INT> primes_used;

    的for(int i = 0;我α25;我++){
        如果(primes_all [1]  - ;数字&安培;&安培;数primes_all [I]→1){
            对于(INT K = 0; K<数/ primes_all [I]; k ++)
                primes_used.push_back(primes_all [I]);
        }
        别人休息;
    }

    INT结果为0;

    用于(为size_t I = 0; I< primes_used.size();我++){
        INT J = primes_used.size() -  1;
        INT new_num =号 -  primes_used [I]

        而(new_num→1&安培;&安培; J&-1)〜
        {
            如果(J> -1),而(primes_used [J] GT; new_num和放大器;&放大器; J> 0)j--;

            如果(J =&安培;!&安培; J&-1)〜{
                new_num  -  = primes_used [J]。

                性病::法院<< primes_used [1]  - ;&其中; << primes_used [J]<< << new_num<<的std :: ENDL;
            }

            j--;
        }

        如果(new_num == 0)结果++;
    }

    性病::法院<<结果<<的std :: ENDL;

    系统(暂停);
    返回0;
}
 

这根本不​​起作用。很简单,因为它背后的想法是错误的。下面是关于限制的小细节:

  • 在时间限制:1秒
  • 内存限制:128 MB

此外,可以给出的最大数量是100。这就是为什么我做了素数的阵列下方100结果生长速度非常快,给定的数字变大了,需要一个BigInteger类以后,但是这不是一个问题。

几个结果众所周知:

 输入结果

7 5
20 732
80 10343662267187
 

所以...任何想法?这是一个组合的问题吗?我不需要code,只是一个想法。我还是个新手,C ++,但我会管理


请记住,3 + 2 + 2比2 + 3 + 2的不同。 此外,分别在给定数量是素本身,它不会被计算。例如,如果给定的数目为7,只有这些总和是有效的:

  2 + 2 + 3
2 + 3 + 2
2 + 5
3 + 2 + 2
5 + 2
7< =排除
 

解决方案

动态规划是你的朋友在这里。

考虑27号。

如果7有5个结果,而20有732的结果,那么你知道,27至少有(732 + 5)的结果。您可以使用precomputed值的,当您去使用两个可变系统(1 + 26,2 + 25 ...等)。你不必重新计算25或26,因为你已经都做了。

I have to print the number of ways you can represent a given number as it's prime number parts.

Let me clarify: Let's say I have been given this number 7. Now, first of all, I have to find all the prime numbers that are less than 7, which are 2, 3 and 5. Now, in how many ways can I summarize those numbers (I can use one number as many times I want) so that the result equals 7? For example, number 7 has five ways:

2 + 2 + 3
2 + 3 + 2
2 + 5
3 + 2 + 2
5 + 2

I'm totally lost with this task. First I figured I'd make an array of usable elements like so: { 2, 2, 2, 3, 3, 5 } (7/2 = 3, so 2 must appear three times. Same goes with 3, which gets two occurences). After that, loop through the array and choose a 'leader' that determines how far in the array we are. I know the explanation is horrible, so here's the code:

#include <iostream>
#include <vector>

int primes_all[25] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97};

int main()
{
    int number;
    std::cin >> number;

    std::vector<int> primes_used;

    for(int i = 0; i < 25; i++) {
        if(primes_all[i] < number && number-primes_all[i] > 1) {
            for(int k = 0; k < number/primes_all[i]; k++)
                primes_used.push_back(primes_all[i]);
        }
        else break;
    }

    int result = 0;

    for(size_t i = 0; i < primes_used.size(); i++) {
        int j = primes_used.size()-1;
        int new_num = number - primes_used[i];

        while(new_num > 1 && j > -1)
        {
            if(j > -1) while(primes_used[j] > new_num && j > 0) j--;

            if(j != i && j > -1) {
                new_num -= primes_used[j];

                std::cout << primes_used[i] << " " << primes_used[j] << " " << new_num << std::endl;
            }

            j--;
        }

        if(new_num == 0) result++;
    }

    std::cout << result << std::endl;

    system("pause");
    return 0;
}

This simply doesn't work. Simply because the idea behind it is wrong. Here's a little details about the limits:

  • Time limit: 1 second
  • Memory limit: 128 MB

Also, the biggest number that can be given is 100. That's why I made the array of prime numbers below 100. The result grows very fast as the given number gets bigger, and will need a BigInteger class later on, but that's not an issue.

A few results known:

Input    Result

7        5
20       732
80       10343662267187

SO... Any ideas? Is this a combinatory problem? I don't need code, just an idea. I'm still a newbie to C++ but I'll manage


Keep in mind that 3 + 2 + 2 is different than 2 + 3 + 2. Also, were the given number to be a prime itself, it won't be counted. For example, if the given number is 7, only these sums are valid:

2 + 2 + 3
2 + 3 + 2
2 + 5
3 + 2 + 2
5 + 2
7 <= excluded

解决方案

Dynamic programming is your friend here.

Consider the number 27.

If 7 has 5 results, and 20 has 732 results, then you know that 27 has at least (732 + 5) results. You can use a two variable system (1 + 26, 2 + 25 ... etc) using the precomputed values for those as you go. You don't have to recompute 25 or 26 because you already did them.

这篇关于一些因为它是素数件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆