zoukankan      html  css  js  c++  java
  • 51Nod-1441 士兵的数字游戏

    题目要求是求出a!/b!(a>=b)的结果,让其不断做除法,最多能做多少次。这个问题首先可以转化为求a!中所有质因子个数-b!中所有质因子个数。以前做过一道快速求某个阶乘对一个素数的因子个数的题(http://www.cnblogs.com/LukeStepByStep/p/5889679.html),然后就跑偏了,怎么写都得遍历素数表。后来发现,对于枚举所有素数,可以在生成素数表的过程中把每个数对于所有素数的因子个数求出来。然后a!的素数因子个数又可以通过前缀和来表示,这个问题就变得简单了。

    教训啊,有些问题稍微变动一下,最优解的思路都是不同的,要灵活变通。

    #include <iostream>
    #include <vector>
    using namespace std;
    bool ps[5000005];
    int pre[5000005];
    vector<int> su;
    int cal(int j,int i)
    {
        int cnt=0;
        int temp=j;
        while(j%i==0)
            cnt++,j/=i;
    
        return cnt;
    }
    int main()
    {
        cin.sync_with_stdio(false);
        int t;
        fill(ps,ps+5000005,true);
        fill(pre,pre+5000005,0);
        for(int i=2;i<5000005;i++)
        {
            if(ps[i])
            {
                pre[i]=1;
                su.push_back(i);
                for(int j=i*2;j<5000005;j+=i)
                {
    
                    pre[j]+=cal(j,i),ps[j]=false;
    
                }
    
            }
        }
        //cout<<pre[6]<<endl;
        pre[0]=pre[1]=0;
        for(int i=3;i<5000005;i++)
            pre[i]+=pre[i-1];
        cin>>t;
    
        while(t--)
        {
            int a;
            int b;
            cin>>a>>b;
            cout<<pre[a]-pre[b]<<endl;
            //cout<<p2<<endl;
        }
        return 0;
    }
  • 相关阅读:
    [codevs2800]送外卖
    python JSON处理
    python系统编码格式
    python,django,mysql版本号查询
    django开发总结:
    python之---类和实例
    django Q和F查询
    合并多个python list以及合并多个 django QuerySet 的方法
    python学习之---匿名函数,返回函数,偏函数
    python学习之---生成器
  • 原文地址:https://www.cnblogs.com/LukeStepByStep/p/7026571.html
Copyright © 2011-2022 走看看