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 }
  • 相关阅读:
    JVM性能调优监控工具jps、jstack、jmap、jhat、jstat使用详解
    JVM运行时内存结构
    nginx(Window下安装 & 配置文件参数说明 & 实例)
    Linux常用命令
    Redis(Windows安装方法与Java调用实例 & 配置文件参数说明 & Java使用Redis所用Jar包 & Redis与Memcached区别 & redis-cli.exe命令及示例)
    Caché数据库学习笔记(3)
    Caché数据库学习笔记(2)
    Caché数据库学习笔记(1)
    [读书笔记]机器学习:实用案例解析(6)
    [读书笔记]机器学习:实用案例解析(5)
  • 原文地址:https://www.cnblogs.com/Ateisti/p/6308623.html
Copyright © 2011-2022 走看看