SPOJ CZ_PROB1 - Summing to a Square Prime

题目链接http://www.spoj.com/problems/CZ_PROB1/

题目大意:Sp2 是所有素数中能够被分解为两个数的平方和的素数的集合。P(a,b)指的是在a的划分中,最大元素不超过b的划分的个数(就是那个经典整数划分动态规划题)。给你个n,k,问P(sp2[n],k)是多少。

  • 0<T<501
  • 0<n<501
  • 1<SP2(n)<7994
  • 0<k<4

解题思路:既然题中已经给出sp2集合的限制,那么sp2直接打表,按照经典动态规划算法计算p(a,b)然后再打表,然后。。。就没有然后了

转移方程:

if(j > i)  p[i][j] = p[i][i];
else if(j == i)  p[i][j] = p[i][j - 1] + 1;
else  p[i][j] = p[i][j - 1] + p[i - j][j];

初始化的时候 p[1][1] = p[1][2] = p[1][3] = p[1][4] = 1,其余0即可。

代码:

 1 const int maxn = 1e4 + 5;
 2 bool vis[maxn];
 3 vector<int> vec;
 4 int n, k;
 5 int p[maxn][10];
 6 
 7 bool isprime(int x){
 8     if(x == 2 || x == 3) return true;
 9     if(x == 1 || x % 2 == 0) return false;
10     for(int i = 2; i * i <= x; i++)
11         if(x % i == 0) return false;
12     return true;
13 }
14 void dowork(){
15     memset(vis, 0, sizeof(vis));
16     for(int i = 1; i <= 90; i++){
17         for(int j = 1; j <= 90; j++){
18             int x = i * i + j * j;
19             if(isprime(x) && x < maxn && vis[x] == 0) {
20                 vec.push_back(x);
21                 vis[x] = true;
22             } 
23         }
24     }
25     sort(vec.begin(), vec.end());
26     memset(p, 0, sizeof(p));
27     p[1][1] = p[1][2] = p[1][3] = p[1][4] = 1; 
28     for(int i = 2; i < 8000; i++){
29         p[i][1] = 1;
30         for(int j = 1; j < 4; j++){
31             if(j > i) p[i][j] = p[i][i];
32             else if(j == i) p[i][j] = p[i][j - 1] + 1;
33             else p[i][j] = p[i][j - 1] + p[i - j][j];
34         }
35     }    
36 } 
37 void solve(){
38     int a = vec[n - 1], b = k;
39     printf("%d\n", p[a][b]);
40 }
41 
42 int main(){
43     dowork();
44     int t;
45     scanf("%d", &t);
46     while(t--){
47         scanf("%d %d", &n, &k);
48         solve();
49     }
50 }

题目:

CZ_PROB1 - Summing to a Square Prime

 

SP2={pp:prime(x1,x2Z,p=x21+x22)}SP2={p∣p:prime∧(∃x1,x2∈Z,p=x12+x22)} is the set of all primes that can be represented as the sum of two squares. The function SP2(n)SP2(n) gives the nnth prime number from the set SP2SP2. Now, given two integers nn(0<n<5010<n<501) and kk (0<k<40<k<4), find p(SP2(n),k)p(SP2(n),k) where p(a,b)p(a,b) gives the number of unordered ways to sum to the given total ‘aa’ with ‘bb’ as its largest possible part. For example: p(5,2)=3p(5,2)=3 (i.e. 2+2+12+2+1, 2+1+1+12+1+1+1, and 1+1+1+1+11+1+1+1+1). Here 55 is the total with 22 as its largest possible part.

Input

The first line gives the number of test cases TT followed by TT lines of integer pairs, nn and kk.

Constraints

  • 0<T<5010<T<501
  • 0<n<5010<n<501
  • 1<SP2(n)<79941<SP2(n)<7994
  • 0<k<40<k<4

Output

The p(SP2(n),k)p(SP2(n),k) for each nn and kk. Append a newline character to every test cases’ answer.

Example

Input:
3
2 2
3 2
5 3

Output:
3
7
85

转载于:https://www.cnblogs.com/bolderic/p/7404528.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值