Bestcoder

Tom and paper

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)

Problem Description

There is a piece of paper in front of Tom, its length and width are integer. Tom knows the area of this paper, he wants to know the minimum perimeter of this paper.

Input

In the first line, there is an integer T indicates the number of test cases. In the next T lines, there is only one integer n in every line, indicates the area of paper.
$ T\leq 10,n\leq {10}^{9} $

Output

For each case, output a integer, indicates the answer.

Sample Input

3
2
7
12

Sample Output

6
16
14

  • 暴力枚举啊,怎么刚开赛那会儿就想不通了?
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    int main()
    {
    int T;
    scanf("%d", &T);
    while (T--)
    {
    int n;
    scanf("%d", &n);
    int i, a, b;
    if (n > 1)
    for (i = (int)sqrt(n); i > 0; i++)
    {
    a = i;
    b = n / a;
    if (a * b == n)
    {
    break;
    }
    }
    else
    {
    a = 1;
    b = n / a;
    };
    //cout<<a<<' '<<b<<endl;
    printf("%d\n", 2 * a + 2 * b);
    }
    return 0;
    }

Tom and permutation

Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)

Problem Description

Tom has learned how to calculate the number of inversions in a permutation of n distinct objects by coding, his teacher gives him a problem:
Give you a permutation of n distinct integer from 1 to n, there are many permutations of 1-n is smaller than the given permutation on dictionary order, and for each of them, you can calculate the number of inversions by coding. You need to find out sum total of them.
Tom doesn’t know how to do, he wants you to help him.
Because the number may be very large, output the answer to the problem modulo $ {10}^{9}+{7} $ .

Input

Multi test cases(about 20). For each case, the first line contains a positive integer n, the second line contains n integers, it’s a permutation of 1-n. $ n\leq 100 $

Output

For each case, print one line, the answer to the problem modulo $ {10}^{9}+7 $ .

Sample Input

3
2 1 3
5
2 1 4 3 5

Sample Output

1
75

Hint

The input may be very big, we might as well optimize input.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include <iostream>
#include <stdio.h>
#include <string>
#include <string.h>
#include <algorithm>
#include <vector>
#include <map>
#include <set>
#include <stdlib.h>
#include <queue>
#include <math.h>
#pragma comment(linker, "/STACK:102400000,102400000")

using namespace std ;

typedef long long LL ;

int a[108] ;
LL dp[108] , fac[108] ;
LL big[108] , les[108] , eb[108] ;
LL N[108] ;

const LL mod = 1000000007LL ;

int main(){


fac[0] = 1LL ;
for(LL i = 1 ; i <= 100 ; i++){
fac[i] = fac[i-1] * i % mod ;
}
dp[1] = 0LL ;
for(LL i = 2 ; i <= 100 ; i++){
dp[i] = i * dp[i-1] % mod + i*(i-1)/2 * fac[i-1] % mod ;
}
/*
for(int i = 1 ; i <= 100 ; i++){
cout<< i << " " <<dp[i]<<endl ;
}

/*
int n ;
while(cin>>n){
for(int i = 0 ; i < n ; i++) a[i] = i ;

int s = 0 ;
do{

for(int i = 0 ; i < n ; i++){
for(int j = i+1 ; j < n ; j++){
if(a[i] > a[j]) s++ ;
}
}

}while(next_permutation(a , a+n)) ;

cout<< n<<" : " << s << endl ;

}

*/
int n ;
while(scanf("%d" , &n) != EOF){

for(int i = 1 ; i <= n ; i++) scanf("%d" , &a[i]) ;

memset(N , 0 , sizeof(N)) ;

for(int i = 1 ; i <= n ; i++){

LL s = 0 ;
for(int j = 1 ; j < i ; j++){
if(a[j] > a[i]) s++ ;
}
N[i] = N[i-1] + s ;
}


for(int i = 1 ; i <= n ; i++){
big[i] = les[i] = 0LL ;

for(int j = 1 ; j < i ; j++){
if(a[j] > a[i]) big[i]++ ;
}
for(int j = i+1 ; j <= n ; j++){
if(a[j] < a[i]) les[i]++ ;
}
}

memset(eb , 0 , sizeof(eb)) ;

for(int k = 1 ; k <= n ; k++){
for(int i = 1 ; i < k ; i++){
for(int j = k ; j <= n ; j++){
if(a[i] > a[j]) eb[k]++ ;
}
}
}

LL sum = 0 ;
for(int i = 1 ; i <= n ; i++){

sum += (eb[i] + N[i-1])*(les[i] * fac[n-i] )% mod ;
sum %= mod ;
sum += fac[n-i] * (les[i] * (les[i]-1) / 2) % mod ;
sum %= mod ;
sum += dp[n-i] * les[i] % mod ;
sum %= mod ;
}

cout<< sum << endl ;
}

return 0 ;
}

Tom and matrix

Time Limit: 3000/1500 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)

Problem Description

Tom was on the way home from school. He saw a matrix in the sky. He found that if we numbered rows and columns of the matrix from 0, then, $ {a} _ {i,j}={C} _ {i}^{j}$
if i < j, $ {a}_{i,j}=0$
Tom suddenly had an idea. He wanted to know the sum of the numbers in some rectangles. Tom needed to go home quickly, so he wouldn’t solve this problem by himself. Now he wants you to help him.
Because the number may be very large, output the answer to the problem modulo a prime p.

Input

Multi test cases(about 8). Each case occupies only one line, contains five integers, $ x_{1}、y_{1}、x_{2}、y_{2}、p.
x_{1}\leq x_{2}\leq {10}^{5},y_{1}\leq y_{2}\leq {10}^{5},2\leq p\leq {10}^{9}$ .

Output

For each case, print one line, the answer to the problem modulo p.

Sample Input

0 0 1 1 7
1 1 2 2 13
1 0 2 1 2

Sample Output

3
4
1

1

Author

Semprathlon / Simfae Dean

Posted on

05/10/2015

Updated on

07/19/2023

Licensed under

Comments