SPOJ:Red John is Back(DP)

Red John has committed another murder. But this time, he doesn't leave a red smiley behind. What he leaves behind is a puzzle for Patrick Jane to solve. He also texts Teresa Lisbon that if Patrick is successful, he will turn himself in. The puzzle begins as follows.

There is a wall of size 4xN in the victim's house where. The victim also has an infinite supply of bricks of size 4x1 and 1x4 in her house. There is a hidden safe which can only be opened by a particular configuration of bricks in the wall. In every configuration, the wall has to be completely covered using the bricks. There is a phone number written on a note in the safe which is of utmost importance in the murder case. Gale Bertram wants to know the total number of ways in which the bricks can be arranged on the wall so that a new configuration arises every time. He calls it M. Since Red John is back after a long time, he has also gained a masters degree in Mathematics from a reputed university. So, he wants Patrick to calculate the number of prime numbers (say P) up to M (i.e. <= M). If Patrick calculates P, Teresa should call Red John on the phone number from the safe and he will surrender if Patrick tells him the correct answer. Otherwise, Teresa will get another murder call after a week.

You are required to help Patrick correctly solve the puzzle.

Input

The first line of input will contain an integer T followed by T lines each containing an integer N. 1<=T<=20, 1<=N<=40

Output

Print exactly one line of output for each test case. The output should contain the number P.

Sample test(s)

input

2
1
7

output

0
3

Note

For N = 1, the brick can be laid in 1 format only

The number of primes <= 1 is 0 and hence the answer.

For N = 7, one of the ways in which we can lay the bricks is

There are 5 ways of arranging the bricks for N = 7 and there are 3 primes <= 5 and hence the answer 3.

Source : Hackerrank.com

Contest arranged by প্রোগ্রামিং প্রবলেম (Programming Problem in Bengali)

题意:给定4*N的空格子,现在叫你用1*4或者4*1的板子去填放,问有多少种放法M,输出小于M的素数个数pM。

思路:如果是2*N用1*2或者2*1格子填的题,推出是斐波拉契数列。其特点是如果横着放,那么上下连续几块都要横着放。此题即是对于1*N的格子,用1*4的格子填有多少种方案。dp记录即可。

(今天啦啦操表演,所以emm,刷刷水题。

#include<bits/stdc++.h>
using namespace std;
const int maxn=110;
const int maxm=220000;
int dp[maxn][maxn],sum[maxn];
int p[maxm],vis[maxm+10],num[maxm],tot;
void prime()
{
    for(int i=2;i<=maxm;i++){
        if(!vis[i]) p[++tot]=i;
        for(int j=1;j<=tot&&i*p[j]<=maxm;j++){
            vis[i*p[j]]=1;
            if(i%p[j]==0) break;
        }
        num[i]=num[i-1]+(1-vis[i]);
    }
}
int main()
{
    int T,N,i,j,k;
    prime();
    for(i=4;i<=40;i++) dp[i][1]=1;
    for(i=2;i<=10;i++){
        for(j=i*4;j<=40;j++)
          for(k=(i-1)*4;k<=j-4;k++)
           dp[j][i]+=dp[k][i-1];
    }
    for(i=1;i<=40;i++){
        for(j=1;j<=i;j++)
         for(k=1;k<=j/4;k++)
           sum[i]+=dp[j][k];
        sum[i]+=1;
    }
    scanf("%d",&T);
    while(T--){
        scanf("%d",&N);
        printf("%d\n",num[sum[N]]);
    }
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/hua-dong/p/9110664.html
今日推荐