Zoj 1003 crashing balloon search

Source: Internet
Author: User

Question: there are 100 balloons labeled with 1-numbers. Each time you step on a balloon, your score can be multiplied by the number of the balloon (the initial score is 1, each balloon can only be step on once ).

Question: If A> B, obtain all possible decomposition conditions of A and B (the product of numbers 1 ). Then, as long as there is a decomposition condition, so that the factor of a Does not include the factor of B, A is possible.

#include<cstdio>#include<cstring>#include<algorithm>#include<cstdlib>using namespace std;#define lint long long#define MAXN 101lint a, b;bool used[MAXN];int f1[MAXN][MAXN]; int f2[MAXN][MAXN];void dfs(int k, lint mul, int f[][MAXN]){    if(k == 1 || mul == 1)    {        if(mul == 1)        {            f[0][0]++;            int cnt = f[0][0];            for(int i = 1; i <= 100; i++)                if(used[i]) f[cnt][++f[cnt][0]] = i;        }        return;    }    if(mul % k == 0)    {        used[k] = true;        dfs(k - 1, mul / k, f);    }    used[k] = false;    dfs(k - 1, mul, f);}bool cmp(int i, int j){    for(int k = 1; k <= f1[i][0]; k++)        for(int t = 1; t <= f2[j][0]; t++)            if(f1[i][k] == f2[j][t]) return false;    return true;}bool judge(){    if(f1[0][0] == 0 && f2[0][0] == 0)        return true;    if(f1[0][0] == 0 && f2[0][0] != 0)        return false;    if(f1[0][0] != 0 && f2[0][0] == 0)        return true;    for(int i = 1; i <= f1[0][0]; i++)        for(int j = 1; j <= f2[0][0]; j++)            if(cmp(i, j)) return true;    return false;}void print(){    printf("f1: %d\n", f1[0][0]);    for(int i = 1; i <= f1[0][0]; i++)    {        for(int j = 1; j <= f1[i][0]; j++)            printf("%d ", f1[i][j]);        printf("\n");    }    printf("f2: %d\n", f2[0][0]);    for(int i = 1; i <= f2[0][0]; i++)    {        for(int j = 1; j <= f2[i][0]; j++)            printf("%d ", f2[i][j]);        printf("\n");    }    printf("\n");}int main(){    while(scanf("%lld %lld", &a, &b) != EOF)    {        if(a < b) swap(a, b);        if(a == b || a <= 100) { printf("%lld\n", a); continue; }        memset(f1, 0, sizeof(f1));        memset(f2, 0, sizeof(f2));        memset(used, 0, sizeof(used));        dfs(100, a, f1);        memset(used, 0, sizeof(used));        dfs(100, b, f2);        if(judge()) printf("%lld\n", a);        else printf("%lld\n", b);        //print();    }    return 0;}

Another solution:

# Include <cstdio> # include <algorithm> using namespace STD; bool F1, F2; void DFS (int numa, int numb, int K) {If (numb = 1) {F2 = true; If (NUMA = 1) F1 = true;/* check whether NUMA can be decomposed after Numb is decomposed. This ensures that some public factors are used by numb and cannot be used by NUMA */} If (k = 1 | (F1 & F2) return; if (NUMA % K = 0) DFS (NUMA/K, numb, k-1); // factor K, if (numb % K = 0) DFS (NUMA, numb/K, k-1); // factor K, used by B and not used by a by DFS (NUMA, numb, k-1); // factor K, neither used by a nor used by B} int main () {int A, B; while (scanf ("% d", & A, & B )! = EOF) {if (a <B) Swap (a, B); F1 = F2 = false; DFS (a, B, 100); If (! F1 & F2) printf ("% d \ n", B); else printf ("% d \ n", a);} return 0 ;}

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.