带分数(DFS)

100 可以表示为带分数的形式:100=3+69258/714
还可以表示为:100=82+3546/197
注意特征:带分数中,数字 1∼9 分别出现且只出现一次(不包含 0)。

类似这样的带分数,100 有 11 种表示法。

输入格式
一个正整数。

输出格式
输出输入数字用数码 1∼9 不重复不遗漏地组成带分数表示的全部种数。

数据范围
1≤N<106
输入样例1:

100

输出样例1:

11

输入样例2:

105

输出样例2:

6

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;
const int N = 20;

int n;
int ans;
bool st[N],backup[N];

bool check(int a,int c)
{
    long long b = n * (long long)c - c * a;
  
    if(!a||!b||!c) return false;
    
    memcpy(backup,st,sizeof st);
    
    while(b)
    {
        int x = b % 10;
        b /= 10;
         
        if(!x||backup[x]) return false;
        backup[x] = true;
    }
    
    for(int i=1;i<=9;i++)
       if(!backup[i]) return false;
       
    return true;
}

void dfs_c(int u,int a,int c)
{
    if(u>9) return ;

    if(check(a,c)) ans++;
    
    for(int i=1;i<=9;i++)
       if(!st[i])
       {
           st[i]=true;
           dfs_c(u+1,a,c*10+i);
           st[i]=false;
       }
}

void dfs_a(int u,int a)
{
    if(a>=n) return ;
    
    if(a) dfs_c(u,a,0);
    
    for(int i=1;i<=9;i++)
       if(!st[i])
       {
           st[i]=true;
           dfs_a(u+1,a*10+i);
           st[i]=false;
       }
}

int main()
{
    cin>>n;
    
    dfs_a(0,0);
    
    cout<<ans<<endl;
    return 0;
}
posted @   三枪一个麻辣烫  阅读(2)  评论(0编辑  收藏  举报  
相关博文:
阅读排行:
· 震惊!C++程序真的从main开始吗?99%的程序员都答错了
· winform 绘制太阳,地球,月球 运作规律
· 【硬核科普】Trae如何「偷看」你的代码?零基础破解AI编程运行原理
· 上周热点回顾(3.3-3.9)
· 超详细:普通电脑也行Windows部署deepseek R1训练数据并当服务器共享给他人
点击右上角即可分享
微信分享提示