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

#math #dynamic-programming

SP2={p∣p:prime∧(?x1,x2∈Z,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
时间: 2024-12-28 15:20:11

SPOJ CZ_PROB1 - Summing to a Square Prime的相关文章

Project Euler 87 :Prime power triples 素数幂三元组

Prime power triples The smallest number expressible as the sum of a prime square, prime cube, and prime fourth power is 28. In fact, there are exactly four numbers below fifty that can be expressed in such a way: 28 = 22 + 23 + 2433 = 32 + 23 + 2449

Project Euler:Problem 87 Prime power triples

The smallest number expressible as the sum of a prime square, prime cube, and prime fourth power is 28. In fact, there are exactly four numbers below fifty that can be expressed in such a way: 28 = 22 + 23 + 24 33 = 32 + 23 + 24 49 = 52 + 23 + 24 47

SPOJ Problem 2: Prime Generator

嗯..在SPOJ上刷的第二题. 一开始不知道哪错了,后来发现i出现了两遍.. 因为m<10^9,所以用素数筛筛32000以内的数,开一个4000的数组存储就行.然后再从n开始用素数筛,总之效率还行. 代码如下: //0.01s 3.2M #include<cstdio> #include<cstring> #include<cmath> int n,i,j,t,m,k,tot; int a[100005],b[4000]; int prime[40000]; in

素数筛法--SPOJ Problem 2 Prime Generator

质数(prime number)又称素数,除了1和它本身外,不能整除以其他自然数,换句话说就是该数除了1和它本身以外不再有其他的因数:否则称为合数.最小的质数是2. 要判断一个整数N是不是质数很简单,看它是否能被2到sqrt(N)之间的整数整除即可. def isPrime(n): if n%2==0: return False for i in xrange(3,int(math.sqrt(n)+1),2): if n%i==0: return False return True 不过要找出1

SPOJ Python Day2: Prime Generator

2. Prime Generator 任务很简单,生成m到n之间的所有质数.一个比较常见的思路是: 自然数$1, 2, -, N$中的最大的质因子要小于$\sqrt{N}$.所以用m到n中的每一个数去试除1到$\sqrt{n}$中的所有数.能整除就是合数,全不能整除就是质数. 但是这么做会超时.. 一般生成质数有一个常用的算法:筛法 http://zh.wikipedia.org/wiki/%E5%9F%83%E6%8B%89%E6%89%98%E6%96%AF%E7%89%B9%E5%B0%B

SPOJ PON - Prime or Not

题目链接:http://www.spoj.com/problems/PON/ 题目大意:判断N是不是素数,N<264-1. 解题思路:需要用到拉宾-米勒素性判定. (选自数论书籍)合数的拉宾-米勒测试:设n是奇素数,记n-1=2kq,q为奇数.对不被n整除的某个a,如果下述两个条件都成立,则n是合数. a): aq !≡ 1 (mod n),  即a的q次方不与1模n同余 b): 对所有i = 0,1,2,--,k-1, a2^i * q !≡ -1 (mod n) 所以可以打出100个素数表然

SPOJ Prime or Not - 快速乘 - 快速幂

Given the number, you are to answer the question: "Is it prime?" Solutions to this problem can be submitted in C, C++, Pascal, Perl, Python, Ruby, Lisp, Hask, Ocaml, Prolog, Whitespace, Brainf**k and Intercal only. Input t – the number of test c

Spoj PRIME1 - Prime Generator

题意翻译 求给定的两个数之间的素数 Translated by @kaiming 题目描述 Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers! 输入输出格式 输入格式: The input begins with the number t of test cas

Prime Generator(spoj)

原题: Prime Generator Problem code: PRIME1 Peter wants to generate some prime numbers for his cryptosystem. Help him! Your task is to generate all prime numbers between two given numbers! Input The input begins with the number t of test cases in a sing