SRM 303U
DIV2 1000
使用一個(gè)整數(shù)的所有素因子能否組成一個(gè)回文字符串?要求數(shù)出在A與B之間滿足要求的這樣的整數(shù)的數(shù)目。
暴力解決就OK,next_permutation 的復(fù)雜度要計(jì)算準(zhǔn)確!不是簡(jiǎn)單的數(shù)目的階乘!!
template<class T> string toString(T n){ostringstream ost;ost<<n;ost.flush();return ost.str();} bool judge(string c) { for(int i=0; i<=c.size()/2; i++) { if(c[i]!= c[c.size()-i-1]) return false; } return true; } bool func(int num) { vector<string> p; int temp = num; for(int i=2; i*i<=num; i++) { while(temp % i==0) { p.push_back(toString(i)); temp/=i; } } if(temp!=1) p.push_back(toString(temp)); sort(p.begin(), p.end()); do { string c; for(int i=0; i<p.size(); i++) c+=p[i]; if(judge(c)){ for(int i=0; i<p.size(); i++) cout<<p[i]<<" "; cout<<endl; return true;} }while(next_permutation(p.begin(), p.end())); return false; } class PrimePalindromic { public: int count(int A, int B) { int ret = 0; for(int i=A; i<=B; i++) if(func(i)){ cout<<" "<<i<<endl; ret++;} return ret; } };
posted on 2012-06-01 16:39 Sosi 閱讀(153) 評(píng)論(0) 編輯 收藏 引用 所屬分類: Algorithm