题目描述
100 可以表示为带分数的形式:100 = 3 + 69258 / 714。
还可以表示为:100 = 82 + 3546 / 197。
注意特征:带分数中,数字1~9分别出现且只出现一次(不包含0)。
类似这样的带分数,100 有 11 种表示法。
输入
从标准输入读入一个正整数N (N<1000*1000)
输出
程序输出该数字用数码1~9不重复不遗漏地组成带分数表示的全部种数。
样例输入
100
样例输出
11
代码如下:
#include <iostream>
using namespace std;
#include <cstring>
int n,ans = 0;
const int N = 11;
bool use[N],use2[N];bool check(int suma,int sumc)
{long long b = (long long )sumc * n - suma*sumc;if (!b){return false;}memcpy(use2,use,sizeof(use));//memcpy这一步用的不错!!!while(b){if (use2[b%10] || !(b%10))//这一步很关键!!!{return false;}use2[b%10] = true;b /= 10;}for (int i =1;i<=9;i++){if (!use2[i]){return false;}}return true;
}void dfs_c(int u,int suma,int sumc)
{if (u > 9){return ;}if (check(suma,sumc)){ans++;return ;}for (int i = 1;i<=9;i++){if (!use[i]){use[i] = true;dfs_c(u+1,suma,sumc*10+i);use[i] = false;}}
}void dfs_a(int u,int suma)
{if(suma>=n){return ;}if (u){dfs_c(u,suma,0);}for (int i = 1;i<=9;i++){if (!use[i]){use[i] = true;dfs_a(u+1,suma*10+i);use[i] = false;}}
}int main()
{cin>>n;dfs_a(0,0);cout<<ans<<endl;return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
typedef long long LL;
const int N = 12;
bool vis[N];
int n;
int ans;
bool check(int suma,int sumc)
{bool st[N];LL sum_b = (LL)sumc*n-(LL)suma*sumc;memcpy(st,vis,sizeof(vis));while(sum_b){if (st[sum_b%10] || sum_b%10==0) return false;//这一步很关键!!!st[sum_b%10] = true;sum_b = sum_b/10;}for (int i = 1;i<=9;i++){if (!st[i]) return false;}return true;
}void dfs_c(int u,int suma,int sumc)
{if (u > 9) return ;if (check(suma,sumc) && sumc!=0){ans++;return ;}for (int i = 1;i<=9;i++){if (!vis[i]){vis[i] =true;dfs_c(u+1,suma,sumc*10+i);vis[i] = false;}}
}void dfs_a(int u,int suma)
{dfs_c(u,suma,0);for (int i = 1;i<=9;i++){if (!vis[i]){vis[i] = true;dfs_a(u+1,suma*10+i);vis[i] = false;}}
}int main()
{cin>>n;dfs_a(0,0);cout<<ans<<endl;return 0;
}