A. Table
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Simon has a rectangular table consisting of n rows and m columns. Simon numbered the rows of the table from top to bottom starting from one and the columns — from left to right starting from one. We'll represent the cell on the x-th row and the y-th column as a pair of numbers (x, y). The table corners are cells: (1, 1), (n, 1), (1, m), (n, m).

Simon thinks that some cells in this table are good. Besides, it's known that no good cell is the corner of the table.

Initially, all cells of the table are colorless. Simon wants to color all cells of his table. In one move, he can choose any good cell of table (x1, y1), an arbitrary corner of the table (x2, y2) and color all cells of the table (p, q), which meet both inequations: min(x1, x2) ≤ p ≤ max(x1, x2), min(y1, y2) ≤ q ≤ max(y1, y2).

Help Simon! Find the minimum number of operations needed to color all cells of the table. Note that you can color one cell multiple times.

Input

The first line contains exactly two integers n, m (3 ≤ n, m ≤ 50).

Next n lines contain the description of the table cells. Specifically, the i-th line contains m space-separated integers ai1, ai2, ..., aim. If aij equals zero, then cell (i, j) isn't good. Otherwise aij equals one. It is guaranteed that at least one cell is good. It is guaranteed that no good cell is a corner.

Output

Print a single number — the minimum number of operations Simon needs to carry out his idea.

Examples
Input
3 3
0 0 0
0 1 0
0 0 0
Output
4
Input
4 3
0 0 0
0 0 1
1 0 0
0 0 0
Output
2
Note

In the first sample, the sequence of operations can be like this:

  • For the first time you need to choose cell (2, 2) and corner (1, 1).
  • For the second time you need to choose cell (2, 2) and corner (3, 3).
  • For the third time you need to choose cell (2, 2) and corner (3, 1).
  • For the fourth time you need to choose cell (2, 2) and corner (1, 3).

In the second sample the sequence of operations can be like this:

  • For the first time you need to choose cell (3, 1) and corner (4, 3).
  • For the second time you need to choose cell (2, 3) and corner (1, 1).

题意:n*m的矩阵涂色  每次选取两个点 1个顶点 1个标记为1的点 形成矩形并涂色 问最少要涂几次使得n*m的矩阵全部涂满

题解: 贪心 特判顶点,边界

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <map>
10 #define ll  __int64
11 #define mod 1000000007
12 #define dazhi 2147483647
13 using namespace  std;
14 int n,m;
15 int mp[55][55];
16 int main()
17 {
18     scanf("%d %d",&n,&m);
19     for(int i=1;i<=n;i++)
20     {
21         for(int j=1;j<=m;j++)
22         {
23             scanf("%d",&mp[i][j]);
24         }
25     }
26     if(mp[1][m]==1||mp[n][m]==1||mp[1][1]==1||mp[n][1]==1)
27     {
28         printf("1\n");
29         return 0;
30     }
31     for(int i=2;i<=n-1;i++){
32         if(mp[i][1]==1)
33         {
34             printf("2\n");
35             return 0;
36         }
37     }
38       for(int i=2;i<=n-1;i++){
39         if(mp[i][m]==1)
40         {
41             printf("2\n");
42             return 0;
43         }
44     }
45       for(int i=2;i<=m-1;i++){
46         if(mp[1][i]==1)
47         {
48             printf("2\n");
49             return 0;
50         }
51     }
52       for(int i=2;i<=m-1;i++){
53         if(mp[n][i]==1)
54         {
55             printf("2\n");
56             return 0;
57         }
58     }
59     printf("4\n");
60     return 0;
61 }

 

B. Permutation
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

A permutation p is an ordered group of numbers p1,   p2,   ...,   pn, consisting of n distinct positive integers, each is no more than n. We'll define number n as the length of permutation p1,   p2,   ...,   pn.

Simon has a positive integer n and a non-negative integer k, such that 2k ≤ n. Help him find permutation a of length 2n, such that it meets this equation: .

Input

The first line contains two integers n and k (1 ≤ n ≤ 50000, 0 ≤ 2k ≤ n).

Output

Print 2n integers a1, a2, ..., a2n — the required permutation a. It is guaranteed that the solution exists. If there are multiple solutions, you can print any of them.

Examples
Input
1 0
Output
1 2
Input
2 1
Output
3 2 1 4
Input
4 0
Output
2 7 4 6 1 3 5 8
Note

Record |x| represents the absolute value of number x.

In the first sample |1 - 2| - |1 - 2| = 0.

In the second sample |3 - 2| + |1 - 4| - |3 - 2 + 1 - 4| = 1 + 3 - 2 = 2.

In the third sample |2 - 7| + |4 - 6| + |1 - 3| + |5 - 8| - |2 - 7 + 4 - 6 + 1 - 3 + 5 - 8| = 12 - 12 = 0.

题意:构造a数列 使得满足上述的式子

题解:1~2n排列如下

      (2n  2n-1) (2n-2 2n-3) ......   (4 3)(2 1)

=>  1 ,1 .....1,1

根据k 的大小 反转k对 输出即可

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <map>
10 #define ll  __int64
11 #define mod 1000000007
12 #define dazhi 2147483647
13 using namespace  std;
14 ll n,k;
15 struct node
16 {
17     ll l,r;
18     ll w;
19 }N[50005];
20 int main()
21 {
22     scanf("%I64d %I64d",&n,&k);
23     ll exm=n*2;
24     for(int i=1;i<=n;i++)
25     {
26         N[i].l=exm--;
27         N[i].r=exm--;
28         N[i].w=1;
29     }
30     for(int i=1;i<=n;i++)
31     {
32         if(k==0)
33           break;
34         if(k>=N[i].w)
35         {
36             k-=N[i].w;
37             swap(N[i].l,N[i].r);
38         }
39     }
40     for(int i=1;i<=n;i++)
41         printf("%I64d %I64d ",N[i].l,N[i].r);
42     return 0;
43 }

 

C. Prime Number
time limit per test
1 second
memory limit per test
256 megabytes
input
standard input
output
standard output

Simon has a prime number x and an array of non-negative integers a1, a2, ..., an.

Simon loves fractions very much. Today he wrote out number on a piece of paper. After Simon led all fractions to a common denominator and summed them up, he got a fraction: , where number t equals xa1 + a2 + ... + an. Now Simon wants to reduce the resulting fraction.

Help him, find the greatest common divisor of numbers s and t. As GCD can be rather large, print it as a remainder after dividing it by number 1000000007 (109 + 7).

Input

The first line contains two positive integers n and x (1 ≤ n ≤ 105, 2 ≤ x ≤ 109) — the size of the array and the prime number.

The second line contains n space-separated integers a1, a2, ..., an (0 ≤ a1 ≤ a2 ≤ ... ≤ an ≤ 109).

Output

Print a single number — the answer to the problem modulo 1000000007 (109 + 7).

Examples
Input
2 2
2 2
Output
8
Input
3 3
1 2 3
Output
27
Input
2 2
29 29
Output
73741817
Input
4 5
0 0 0 0
Output
1
Note

In the first sample . Thus, the answer to the problem is 8.

In the second sample, . The answer to the problem is 27, as 351 = 13·27, 729 = 27·27.

In the third sample the answer to the problem is 1073741824 mod 1000000007 = 73741817.

In the fourth sample . Thus, the answer to the problem is 1.

题意:  通分之后 求分子与分母的gcd  对1e9+7取模

题解:找到分子各项中最小的指数 并标记指数存在的次数 从低指数向上不断进位 

注意所求指数应当小于等于分母的指数 之后用到快速幂。

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstdlib>
 4 #include <cstring>
 5 #include <algorithm>
 6 #include <stack>
 7 #include <queue>
 8 #include <cmath>
 9 #include <map>
10 #define ll  __int64
11 #define mod 1000000007
12 #define dazhi 2147483647
13 using namespace  std;
14 ll n,x;
15 ll a[100005];
16 ll b[100005];
17 map<ll,ll>mp;
18 ll sum;
19 ll gcd(ll a,ll b){return b==0?a:gcd(b,a%b);}
20 ll quickmod(ll aa,ll bb)
21 {
22     ll re=1;
23     while(bb)
24     {
25         if(bb&1)
26             re=(re*aa)%mod;
27         aa=(aa*aa)%mod;
28         bb=bb>>1;
29     }
30     return re%mod;
31 }
32 int main()
33 {
34     sum=0;
35     int jishu=0;
36     mp.clear();
37     scanf("%I64d %I64d",&n,&x);
38     for(ll i=1;i<=n;i++)
39     {
40         scanf("%I64d",&a[i]);
41         sum+=a[i];
42     }
43     for(ll i=1;i<=n;i++)
44     {
45         ll exm=sum-a[i];
46         if(mp[exm]==0)
47         {
48             b[jishu++]=exm;
49         }
50         mp[exm]++;
51     }
52     sort(b,b+jishu);
53     ll ans=b[0];
54     while(1)
55     {
56         if(mp[ans]%x==0){
57             mp[ans+1]+=(mp[ans]/x);
58             ans++;
59         }
60         else
61             break;
62     }
63     if(ans>sum)
64       ans=sum;
65     printf("%I64d\n",(quickmod(x,ans)%mod));
66     return 0;
67 }