Blue Bridge Cup algorithm solution to a problem the previous questions mixed number

Title Description

Problem description
100 may be represented in the form of mixed fractions: 69 258 + 3 = 100/714.
It can also be expressed as: 82 + 100 = 3546/197.
Note wherein: a mixed fraction, the numbers 1 to 9 appears once and only once, respectively (not including 0).
Mixed fraction like this, there are 11 100 notation.

Input format
from the standard input a positive integer N (N <1000 * 1000)
the output format of
the program of the digital output by a digital 1 ~ 9 will not be repeated without omission mixed fraction consisting of all the number of species represented.
Note: does not require the output of each said only statistics on the number representation!
Sample Input 1
100
Sample Output 1
. 11
Sample Input 2
105
Sample Output 2
. 6

answer:

This question is enumerated on the bin, the 1 to 9, all combinations enumerated ( use next_permutation()function, can own handwriting full permutation function ) **, and then determines whether a positive integer N ** composition (i.e. such an arrangement enumerated, then the above plus '+' and '/' number, is equal to N can not see), but the operator will find it cumbersome and time out. It is necessary to use the technique of pruning : expression: num1 num2 + / num3
. 1 . Num1 (first number taken), if num1> = N, then the latter is certainly not 0, it was impossible for N, directly break out.
2 . Num2 must be larger than num3, as num2/num3an integer greater than 1, we must first ensure digit num2 to be> = num3, equivalent to a total of nine, then num1 took away i bit, then left 9-i bit, num2 from the need to take at least i i + (9-i) the position of / 2.
** For example, when the second, such as ** 123456789, num1 = 123, then if the num2 at least 456, because the number of bits to ensure that no less than num3, but also pay attention when num1 = 1234, the initial num2 is 56 because i=4, i+(9-i)/2=4+5/2=6from the beginning to take 6.
3 . The second step will not necessarily determine the num2> = num3, in the third step needs to be added num2>=num3, followed by even num2%num3==0, finally num1 + num2 / num3.
** Note: ** If the second pruning can not read can not add, as can be.

Code:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <climits>
#include <cstring>
#include <string>
#include <algorithm>
#include <vector>
#include <deque>
#include <list>
#include <utility>
#include <set>
#include <map>
#include <stack>
#include <queue>
#include <bitset>
#include <iterator>
using namespace std;

typedef long long ll;
const int inf = 0x3f3f3f3f;
const ll  INF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1.0);
const double E = exp(1.0);
const int MOD = 1e9+7;
const int MAX = 1e5+5;

int N;
int a[9+5] = {1,2,3,4,5,6,7,8,9};
int vis[9+5] = {0};
int sum = 0;

int func(int i,int j)// 计算数组a,i~j组成的数字
{
    int sum = 0;
    for(int k = i; k < j; k++)
    {
        sum = sum*10 + a[k];
    }
    return sum;
}

// 手写全排列算法
void func2()// 根据dfs求出的全排列来判断是否能够组成带分数
{
    for(int i = 1; i <= 9; i++)
    {
        int num1 = func(0,i);
        if(num1 >= N)// 剪枝1:如果第一个数>=N,说明再往后面肯定也是>N
        {
            break;
        }
        for(int j = i + (9-i)/2; j <= 8; j++)// 剪枝2:num2必须要>=num3,所以至少数的个数要相等
        {
            int num2 = func(i,j);
            int num3 = func(j,9);
            // 剪枝3:num2必须要>=num3,num2必须能整除num3,然后就是符合题目的要求
            if(num2 >= num3 && num2%num3 == 0 && num1+num2/num3 == N)
            {
                sum++;
            }
        }
    }
}

void dfs(int i)
{
    if(i == 9)
    {
        func2();
        return;
    }
    for(int j = 1; j <= 9; j++)
    {
        if(!vis[j])
        {
            vis[j] = true;
            a[i] = j;
            dfs(i+1);
            vis[j] = false;
        }
    }
}

int main()
{
    /*
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    */
    cin >> N;
    /* 方法1:库里面的全排列函数
    do
    {
        for(int i = 1; i <= 9; i++)
        {
            int num1 = func(0,i);
            if(num1 >= N)// 剪枝1:如果第一个数>=N,说明再往后面肯定也是>N
            {
                break;
            }
            for(int j = i + (9-i)/2; j <= 8; j++)// 剪枝2:num2必须要>=num3,所以至少数的个数要相等
            {
                int num2 = func(i,j);
                int num3 = func(j,9);
                // 剪枝3:num2必须要>=num3,num2必须能整除num3,然后就是符合题目的要求
                if(num2 >= num3 && num2%num3 == 0 && num1+num2/num3 == N)
                {
                    sum++;
                }
            }
        }
    }while(next_permutation(a,a+9));*/

    /* 方法2:手写全排列函数 */
    dfs(0);

    cout << sum << endl;
    return 0;
}
He published 197 original articles · won praise 18 · views 20000 +

Guess you like

Origin blog.csdn.net/qq_41708792/article/details/105331699