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

【转】核心网络—BestCoder需要注意的几个问题

核心网络—BestCoder需要注意的几个问题.

1.不能随意hack,hack成功有加分,但是hack失败也会扣分!
2.不要随意提交已经AC的题目,罚时是这样的:最后一次AC的之前所有提交(无论AC与否)均算作罚时!其中AC的标为Accepted(Past),被hack掉的AC也会标为Accepted(Past)。
3.AC不代表真正的AC,比赛最后阶段有hack数据,如果被hack掉就不算AC。hack数据一般都是非常变态的输入,或者是大数据!有时候有些人刻意利用题目bug构造变态的数据导致全场无人AC……
4.hack时只能读别人的代码,无法复制,这是锻炼读代码的能力。
5.如果有一场比赛注册了,但是忘做了,只要没有该场比赛的提交记录就不会使Rating降低。
6.只有本场比赛进入前300才能使Rating升高,否则就降低。
7.ABCD四道题的难度是依次升高的,分数也是依次升高的,根据错的次数和AC时间而使所得分数不同。
8.比赛题目是民间的ACMer出的,如果发现题目中有大量的英语语法错误,请不要见怪。
9.比赛日期并非固定在周五或者周六或者周日,根据杭电oj的具体日程安排而定。比如,如果有网络赛,可能就要提前或者延后了。
10.最后你的很多AC的题目可能都会被hack掉,请不要见怪。
11.

以上错误我几乎都犯过……
O__O”…

【Bestcoder

1001 - Rikka with string

Accepts: 395
Submissions: 2281
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/65536 K (Java/Others)
Problem Description

As we know, Rikka is poor at math. Yuta is worrying about this situation, so he gives Rikka some math tasks to practice. There is one of them:

One day, Yuta got a string which contains n letters but Rikka lost it in accident. Now they want to recover the string. Yuta remembers that the string only contains lowercase letters and it is not a palindrome string. Unfortunately he cannot remember some letters. Can you help him recover the string?

It is too difficult for Rikka. Can you help her?
Input

This problem has multi test cases (no more than 20). For each test case, The first line contains a number n(1≤n≤1000). The next line contains an n-length string which only contains lowercase letters and ‘?’ – the place which Yuta is not sure.
Output

For each test cases print a n-length string – the string you come up with. In the case where more than one string exists, print the lexicographically first one. In the case where no such string exists, output “QwQ”.
Sample Input

5
a?bb?
3
aaa

Sample Output

aabba
QwQ

WA得智商都被拉低了 QAQ

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int dfs(int step,bool &state)//1:to check
{
int res;
if (step<m-1) res=dfs(step+1,state);
int pos=n-vec[step]-1;
cout<<step<<' '<<str[vec[step]]<<' '<<str[pos]<<endl;
for(char ch='a';ch<='z';ch++)
if (state&&str[pos]==ch)
{
if (ch<'z') continue;
else return -1;//error
}
else
{
str[vec[step]]=ch;
if (state&&str[pos]!='?'&&ch!=str[pos]) state=0;
if (ch!=str[pos]) return 1;
}
return -1;
}

AC:

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
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<string>
#include<queue>
#include<stack>
#include<set>
#include<map>
#define CLEAR(a) memset((a),0,sizeof((a)))

using namespace std;

typedef long long LL;
const double pi = acos(-1.0);
const int maxn=2e3;
const int inf=99999999;
const float eps=1e-3;

vector<int> vec;
int n,m;
char s[maxn],str[maxn];

int dfs(int step,bool &state)//1:to check
{

int pos=n-vec[step]-1;
for(char ch='a';ch<='z';ch++)
{
str[vec[step]]=ch;
if (str[pos]!='?'&&ch!=str[pos]) {state=0;}
if (step<m-1) dfs(step+1,state);
if (state&&str[pos]!='?'&&ch!=str[pos]||!state) return 1;
}

return -1;
}

int main()
{

while(~scanf("%d",&n))
{
vec.clear();
scanf("%s",s);
memcpy(str,s,sizeof(s));
for(int i=0;i<n;i++)
if (s[i]=='?')
vec.push_back(i);
m=vec.size();
bool tmp=1;

if (m>1||m==1&&!(n%2&&vec[0]==n/2))
{
if (dfs(0,tmp)>=0) puts(str);
else puts("QwQ");
}
else
{
bool ans=1;
for(int i=1;i<=strlen(s)/2;i++)
if (s[i-1]!=s[strlen(s)-i])
{
ans=0;break;
}
if (m==1&&n%2&&vec[0]==n/2) str[vec[0]]='a';
puts(ans?"QwQ":str);
}

}
return 0;
}