Bestcoder #40

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.
T10,n109

Output

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

Sample Input

3
2
7
12

Sample Output

6
16
14

  • 暴力枚举啊,怎么刚开赛那会儿就想不通了?
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 109+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. n100

Output

For each case, print one line, the answer to the problem modulo 109+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.

#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, ai,j=Cji
if i < j, ai,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, x1y1x2y2p.<br/>x1x2105,y1y2105,2p109 .

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值