Problem description
100 can be expressed as a fractional form: 100 = 3 + 69258/714.
It can also be represented as: 100 = 82 + 3546/197.
Note features: With fractions, the digital 1~9 appear only once (not including 0).
Similar to the band score, 100 has 11 notation.
Input format
Reads a positive integer N (n<1000*1000) from the standard input
Output format
The program outputs this number with a digital 1~9 not to be omitted to make up the total number of points expressed with fractions.
Note: No output is required for each representation, only how many representations are counted!
Sample input 1100 Sample output 111 sample Input 2105 sample output 26 First of all violence, TLE, 33 points, reason I still can't think of good.
1 //pure violence hung up. 2#include <stdio.h>3#include <string.h>4#include <iostream>5 using namespacestd;6 7 intvis[Ten], visall[Ten];8 9 BOOLCheckintnum) {Ten while(num) { One inttemp = num%Ten; A if(Vis[temp])return false; - if(temp = =0)return false; -vis[temp]++; theNum/=Ten; - } - return true; - } + - BOOLCheckall () { + for(intI=1; i<Ten; ++i) { A if(Vis[i]! =1)return false; at } - return true; - } - - voidCopynum (intA[],intb[]) { - for(intI=1; i<Ten; ++i) { inA[i] =B[i]; - } to } + - intMain () { the intN; * while(Cin >>N) { $memset (Vis,0,sizeof(Vis));Panax Notoginsengmemset (Visall,0,sizeof(Visall)); - intAns =0; the + for(intL=1; l<n; ++l) { Amemset (Vis,0,sizeof(Vis)); the if(!check (L))Continue; + copynum (Visall, Vis); - $ for(intdown=1; down<100000; ++Down ) { $ intup = (n-l) *Down ; - if(Down > Up)Continue; - if(up% down)Continue; the copynum (Vis, visall); - if(!check (down) | | |!check (UP)) {Wuyi Continue; the } - if(Checkall ()) { Wu //cout << l << ' << down << ' << up << Endl; -ans++; About } $ } - } - -cout << ans <<Endl; A } + return 0; the}View Code
Then the optimization. Still feel the Dfs good magic to say.
1 /*2 just the brute force optimization. 3 The left number is traversed first, and then the DFS searches for the length and number of the denominator. 4 */5 6#include <stdio.h>7#include <string.h>8#include <iostream>9 using namespacestd;Ten One intLens, V, ans; A intvis[Ten], visall[Ten]; - - BOOLCheckintnum) { the intK =0; - while(num) { - inttemp = num%Ten; - if(Vis[temp])return false; +vis[temp]++; -k++; +Num/=Ten; A } atLENS =9-K;//number of remaining available numbers - return true; - } - - voidCopyvis () { - for(intI=0; i<Ten; ++i) { inVisall[i] =Vis[i]; - } to } + - intJudgeintUP) {//Fenmu the intK =0; * Copyvis (); $ while(UP) {Panax Notoginseng inttemp = up%Ten; - if(Visall[temp])return-1; theVisall[temp] =1; +k++; AUp/=Ten; the } + returnK; - } $ $ voidDfsintLenintVal) {//Len Fenzi - if(Len > lens/2)return; - if(judge (v*val) = = Lens-len) ans++; the for(intI=1; i<Ten; ++i) { - if(Vis[i])Continue;WuyiVis[i] =1; theDFS (len+1, val*Ten+i); -Vis[i] =0; Wu } - } About $ - - intMain () { - intN; A while(Cin >>N) { +Ans =0; the for(intL=1; l<n; ++l) { -memset (Vis,0,sizeof(Vis)); $vis[0] =1; the if(!check (L))Continue; thev = n-l; theDfs0,0);//Len Fenmu the } -cout << ans <<Endl; in } the return 0; the}View Code
Then there is the idea that the nine numbers can be fully arranged, and then from eight empty to find two put + and/. Reason, in this case, the time complexity is 10^6*8*7 do not know whether can be too ....
Blue Bridge Cup practice system previous questions with score Dfs