http://codeforces.com/gym/100923/problem/I (题面来源)
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
.
The input file perechi3.in will contain the number representing the number of tests. The next
lines will each contain a single integer
.
The output file perechi3.out should have lines. Line
should hold the answer for the
-th test.
11
1
4
5
10
100
99
16
64
49
50
48
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 }