zoukankan      html  css  js  c++  java
  • I. Por Costel and the Pairs

    http://codeforces.com/gym/100923/problem/I (题面来源)

    time limit per test
    1 second
    memory limit per test
    64 megabytes
    input
    perechi3.in
    output
    perechi3.out

    We don't know how Por Costel the pig arrived at FMI's dance party. All we know is that he did.

    The dance floor is hot because Por Costel broke the air conditioner. On the dance floor, the boys and girls are clumsy. There are  boys at the party. The -th one has clumsiness level . There ale also  girls at the party. The -th girl has clumsiness level  as well. Por Costel finds that a pair of a boy and a girl can dance only if the product of their clumsiness levels is at most , i.e. girl clumsiness level * boy clumsiness level  . Por Costel thinks that a sack of oats with a slice of leek could make a better dancing pair than the people at this party. Nonetheless, he would like to find out how many pairs (boy, girl) can dance. The input will contain the number  representing the number of tests. The next  lines will contain the number .

    Input

    The input file perechi3.in will contain the number  representing the number of tests. The next  lines will each contain a single integer .

    Output

    The output file perechi3.out should have  lines. Line  should hold the answer for the -th test.

    Example
    input
    11
    1
    4
    5
    10
    100
    99
    16
    64
    49
    50
    48
    output
    1
    8
    10
    27
    482
    473
    50
    280
    201
    207
    198

    其实还是比较容易推出来
    ans = [n/1] +[n/2] + [n/3]......+[n/n] []表示向下取整。
    但是当然直接算是不行的,时间复杂度是O(n)
    容易想到一种O(√n)的做法,n-n/2就是所有i,[n/i]=1的数量,以此类推,n-n/2-n/3就是所有i,[n/i] = 2的数量,这样复杂度就可以降到O(√n)
    但是当然也过不了,于是可以发现,如果还剩两个数,那么接下来i = [n/2] + 1,很明显,下一个i必须满足[n/i] < 2 才有意义。
     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 long long int n ;
     4 
     5 void Solve()
     6 {
     7     scanf("%I64d",&n);
     8     long long sy = n , i = 2ll , ans = 0ll;
     9     while( 1 )
    10     {
    11         long long a = n / i;
    12         long long jian = sy - a ;
    13         ans += jian * ( i - 1ll );
    14         sy -= jian; 
    15         if(sy == 0 ) break;
    16         i = n / sy + 1 ;
    17         
    18     }
    19     printf("%I64d
    ",ans);
    20 }
    21 
    22 int main()
    23 {
    24     freopen("perechi3.in","r",stdin);
    25     freopen("perechi3.out","w",stdout);
    26     int t; scanf("%d",&t);
    27     while(t--)
    28     {
    29         Solve();
    30     }
    31     
    32 }
  • 相关阅读:
    共用体类型,结构体类型
    动态内存分配与指向它的指针变量
    向上转型,向下转型
    枚举类型中的构造方法、成员方法
    由setTimeout()里的this引出的this
    eclipse快捷键
    js中运算符的优先级
    关于js闭包杂记
    sublime在Mac osx下安装z-codeing(即emmet)
    利用js得到某个范围内的整数随机数
  • 原文地址:https://www.cnblogs.com/Ateisti/p/6308623.html
Copyright © 2011-2022 走看看