Example 10-6 unrelated elements UVa1635

Source: Internet
Author: User

1. Title Description: Click to open the link

2. Thinking: Through the test can be found that the problem is equivalent to solving C (n-1,i) in the combination of the number of M is a multiple, you can use the unique decomposition theorem to judge: pre-decomposition M, and then use recursion to calculate each item contains m of the exponent of the factor.

3. Code:

#define _crt_secure_no_warnings #include <iostream> #include <algorithm> #include <string> #include <sstream> #include <set> #include <vector> #include <stack> #include <map> #include < queue> #include <deque> #include <cstdlib> #include <cstdio> #include <cstring> #include < cmath> #include <ctime> #include <functional>using namespace std; #define N 100005int fac[100][2];//a table, FAC[I][0] Storage factor, fac[i][1] store its exponent int fac_c[100];int a[n];void factor (int m)//Decomposition M{int&num = fac[0][0];//fac[0][0] is the table header, The total number of stores, using the reference is more convenient num = 0;for (int i = 2; i*i <= m;i++) if (m%i = = 0) {Fac[++num][0] = i;fac[num][1] = 0;do{fac[num][1]++;m/ = i;} while (m%i = = 0);//Will I except clean}if (M > 1)//If decomposition to the last m is still greater than 1, indicating that it is a prime number. Note: If you just judge what the element factor is, you can not judge it here, otherwise you must have this step {fac[++num][0] = m;fac[num][1] = 1;}} BOOL Check (int n, int j)//According to the recursive formula to calculate the J term, check the unique decomposition of the exponent {int num = Fac[0][0];int A = n-j;//is actually ((n-1) +j+1) after simplifying the result of int b = J;for (i NT i = 1; I <= num; i++) {int p = Fac[i][0];int&q = fAc_c[i];for (; a%p = = 0; a/= p, q++);//In order to improve efficiency, only the factor in the decomposition formula of M is used for (; b%p = 0; b/= p, q--);} for (int i = 1; I <= num;i++) if (fac[i][1] > Fac_c[i]) return False;return true; int main () {//freopen ("test.txt", "R", stdin), int n, m;while (CIN >> n >> m) {int cnt = 0;factor (m); Memset (Fac_c , 0, sizeof (fac_c)), for (int i = 1; i < n;i++)//Direct Check 1 to n-1 (starting from 0) if (check (n, i)) a[cnt++] = i + 1;printf ("%d\n", CNT); fo R (int i = 0; i < cnt; i++) printf ("%s%d", i = = 0?) "": "", A[i]);p rintf ("\ n");} return 0;}

Example 10-6 unrelated elements UVa1635

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.