问题描述
100 可以表示为带分数的形式:100 = 3 + 69258 / 714。
还可以表示为:100 = 82 + 3546 / 197。
注意特征:带分数中,数字1~9分别出现且只出现一次(不包含0)。
类似这样的带分数,100 有 11 种表示法。
输入格式
从标准输入读入一个正整数N (N<1000*1000)
输出格式
程序输出该数字用数码1~9不重复不遗漏地组成带分数表示的全部种数。
注意:不要求输出每个表示,只统计有多少表示法!
样例输入1
100
样例输出1
11
样例输入2
105
样例输出2
6
思路:如果直接把数字分成三个部分 进行深搜 复杂度显然是不能接受的 所以我们可以手写全排列(也可以用next_permutation) 然后再全排列数字之间加上 + 和 / 号进行检验
#include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<string> #include<vector> #include<stack> #include<bitset> #include<cstdlib> #include<cmath> #include<set> #include<list> #include<deque> #include<map> #include<queue> #define ll long long int using namespace std; //inline ll gcd(ll a,ll b){return b?gcd(b,a%b):a;} //inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;} int moth[13]={0,31,28,31,30,31,30,31,31,30,31,30,31}; int dir[4][2]={1,0 ,0,1 ,-1,0 ,0,-1}; int dirs[8][2]={1,0 ,0,1 ,-1,0 ,0,-1, -1,-1 ,-1,1 ,1,-1 ,1,1}; const int inf=0x3f3f3f3f; const ll mod=1e9+7; int n; int num[9]={1,2,3,4,5,6,7,8,9}; int ans=0; void jug(){ int a,b,c; a=b=c=0; for(int i=0;i<=6;i++){ a=a*10+num[i]; if(a>=n) break; b=0; for(int j=i+1;j<=7;j++){ b=b*10+num[j]; c=0; for(int k=j+1;k<=8;k++){ c=c*10+num[k]; } if(b%c==0&&b&&c&&a+b/c==n) ++ans; } } } int main(){ // ios::sync_with_stdio(false); scanf("%d",&n); do{ jug(); }while(next_permutation(num,num+9)); printf("%d ",ans); return 0; }