• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            求連續(xù)n個(gè)數(shù)每個(gè)數(shù)的因數(shù)個(gè)數(shù)之和

            今天個(gè)人賽的一個(gè)題
            We define the function f(x) = the number of divisors of x. Given two integers a and b (a ≤ b), please
            calculate f(a) + f(a+1) + ... + f(b).
            Input
            Two integers a and b for each test case, 1 ≤ a ≤ b ≤ 231 1.
            The input is terminated by a line with a = b
            = 0.
            Output
            The value of f(a) + f(a+1) + ... + f(b).
            Sample Input
            9 12
            1 2147483647
            0 0
            Sample Output
            15
            46475828386
            Hint
            For the first test case:
            9 has 3 divisors: 1, 3, 9.
            10 has 4 divisors: 1, 2, 5, 10.
            11 has 2 divisors: 1, 11.
            12 has 6 divisors: 1, 2, 3, 4, 6, 12.
            So the answer is 3 + 4 + 2 + 6 = 15.

            O(n)的算法很好想

            1到m中可以被i整除的數(shù)的個(gè)數(shù)為 m/i

            ?所以用for(sum =0,i=0;i<=m;i++)

            sum += m/i;

            ?sum即是f(1)+f(2)+f(m)的值.

            這樣的算法復(fù)雜度是O(N);

            但諸位大哥也看到了 數(shù)據(jù)范圍很大 所以我們按照慣例---要優(yōu)化···

            其實(shí)我們可以只算從1到sqrt(m)? 具體說來每次不但要加m/i 還要加(m/i-m/(i+1))*i;

            后面加的那個(gè)對應(yīng)的是跟i對應(yīng)的另一半···

            形象一點(diǎn)吧

            拿12來說

            就是 12 6 4 3 2 2 1 1 1 1 1 1

            我們算的從1到3 后面對應(yīng)的就是
            (12/1-12/2)*1
            (12/2-12/3)*2
            (12/3-12/4)*3

            這個(gè)也算規(guī)律吧

            這樣一來規(guī)模就是O(sqrt(N))

            還是貼CODE:
            #include<iostream>
            #include <cmath>

            using namespace std;
            int a[10]={0,1,3,5,8,10};
            long long int f(long long int m)
            {
            ??????? if(m<=5) return a[m];
            ??????? long long int sum=0;
            ??????? long long int t=sqrt(m*1.0);
            ??????? for(long long int i=1;i<=t;i++)
            ??????? {
            ??????????????? sum+=m/i;
            ??????????????? sum+=(m/i-m/(i+1))*i;
            ??????? }
            ??????? return sum;
            }
            int main()
            {
            ??????? long long int m,n;
            ??????? while(cin>>m>>n&&(m||n))
            ??????????????? cout<<f(n)-f(m-1)<<endl;

            }



            posted on 2008-05-24 23:59 Victordu 閱讀(555) 評論(0)  編輯 收藏 引用


            只有注冊用戶登錄后才能發(fā)表評論。
            網(wǎng)站導(dǎo)航: 博客園   IT新聞   BlogJava   博問   Chat2DB   管理


            導(dǎo)航

            <2008年5月>
            27282930123
            45678910
            11121314151617
            18192021222324
            25262728293031
            1234567

            統(tǒng)計(jì)

            常用鏈接

            留言簿(5)

            隨筆檔案(46)

            搜索

            積分與排名

            最新評論

            閱讀排行榜

            評論排行榜

            久久精品亚洲日本波多野结衣 | 久久免费观看视频| 久久精品www人人爽人人| 亚洲欧美成人久久综合中文网| 久久久久国产| 久久久高清免费视频| 久久精品无码一区二区无码| 久久96国产精品久久久| 久久高清一级毛片| 伊人久久大香线蕉av不变影院| 久久国产热精品波多野结衣AV| 91精品国产91久久久久久青草| 久久久久久久久66精品片| 99久久精品影院老鸭窝| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 一本色道久久综合亚洲精品| 国产精品99精品久久免费| 久久国产一片免费观看| 国产婷婷成人久久Av免费高清| 久久精品免费大片国产大片 | 久久乐国产综合亚洲精品| 久久不见久久见免费视频7| 亚洲?V乱码久久精品蜜桃| 欧美精品一区二区精品久久| 久久久久久久精品妇女99 | 色99久久久久高潮综合影院| 久久综合久久久| 久久亚洲精品中文字幕| 欧美日韩精品久久久免费观看| WWW婷婷AV久久久影片| 久久妇女高潮几次MBA| 亚洲伊人久久综合中文成人网| 777久久精品一区二区三区无码| 日韩精品久久无码中文字幕| 久久免费视频1| 少妇被又大又粗又爽毛片久久黑人| 日本免费一区二区久久人人澡| 久久亚洲AV成人出白浆无码国产 | 久久99精品国产麻豆| 日本精品久久久久久久久免费| 久久精品草草草|