O(1) 的小乐

Job Hunting

公告

记录我的生活和工作。。。
<2011年5月>
24252627282930
1234567
891011121314
15161718192021
22232425262728
2930311234

统计

  • 随笔 - 182
  • 文章 - 1
  • 评论 - 41
  • 引用 - 0

留言簿(10)

随笔分类(70)

随笔档案(182)

文章档案(1)

如影随形

搜索

  •  

最新随笔

最新评论

阅读排行榜

评论排行榜

SRM 303U

DIV2 1000

使用一个整数的所有素因子能否组成一个回文字符串?要求数出在A与B之间满足要求的这样的整数的数目。

暴力解决就OK,next_permutation 的复杂度要计算准确!不是简单的数目的阶乘!!

 

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 阅读(133) 评论(0)  编辑 收藏 引用 所属分类: Algorithm


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


统计系统