埃拉托斯特尼筛法(sieve of Eratosthenes ) 是古希腊数学家埃拉托斯特尼发明的计算素数的方法。对于求解不大于n的所有素数,我们先找出sqrt(n)内的所有素数p1到pk,其中k = sqrt(n),依次剔除Pi的倍数,剩下的所有数都是素数。

具体操作如上述 图片所示。

C++实现

  1. #include<iostream>
  2. #include<vector>
  3. using namespace std;
  4. int main() {
  5. int n;
  6. cin >> n;
  7. vector<bool> isprime(n + 5, true);
  8. vector<int> ans;
  9. for (int i = 2; i <= n; i++) {
  10. if (isprime[i]) {
  11. ans.push_back(i);
  12. for (int j = i * i; j <= n; j += i)isprime[j] = false;
  13. }
  14. }
  15. for (auto i : ans)cout << i << " ";
  16. cout << endl;
  17. return 0;
  18. }

整除问题

给定n,a求最大的k,使n!可以被ak整除但不能被a(k+1)整除。

输入描述

两个整数n(2<=n<=1000),a(2<=a<=1000)

输出描述

示例1

输入

555 12

输出

274

  1. #include<iostream>
  2. #include<vector>
  3. #include<map>
  4. using namespace std;
  5. int main() {
  6. int n, a, temp;
  7. int ans = 0x7fffffff;
  8. cin >> n >> a;
  9. vector<bool> isprime(1010, true);
  10. vector<int> prime; //素数列表
  11. map<int, int> primecntnp; //存储n!的质因子的指数
  12. map<int, int> primecnta; //存储a的质因子的指数
  13. for (int i = 2; i <= 1010; i++) { //采用素数筛选出前1010个数中的素数,并将map初始化
  14. if (isprime[i]) {
  15. prime.push_back(i);
  16. primecntnp[i] = primecnta[i] = 0;
  17. for (int j = i * i; j <= 1010; j += i)isprime[j] = false;
  18. }
  19. }
  20. //4! = 24 = 1*2*3*4 = 2*2*2*3
  21. for (int i = 0; i < prime.size(); i++) { //对n!进行因式分解
  22. temp = n;
  23. while (temp) { //按照p、p*p、p*p*p来进行因式分解
  24. primecntnp[prime[i]] += temp / prime[i];
  25. temp /= prime[i];
  26. }
  27. }
  28. for (int i = 0; i < prime.size(); i++) { //对a进行因式分解
  29. temp = a;
  30. while (temp % prime[i] == 0) {
  31. primecnta[prime[i]]++;
  32. temp /= prime[i];
  33. }
  34. if (primecnta[prime[i]] == 0)continue; //a里面不存在的则无法提供
  35. if (primecntnp[prime[i]] / primecnta[prime[i]] < ans)ans = primecntnp[prime[i]] / primecnta[prime[i]];
  36. }//找到最小的指数,便是最大的k值
  37. cout << ans << endl;
  38. return 0;
  39. }
  40. /*
  41. 555 12
  42. 274
  43. */

拓展

Prime Path素数筛与BFS动态规划的综合应用

问题 POJ

Description

The ministers of the cabinet were quite upset by the message from the Chief of Security stating that they would all have to change the four-digit room numbers on their offices.

— It is a matter of security to change such things every now and then, to keep the enemy in the dark.

— But look, I have chosen my number 1033 for good reasons. I am the Prime minister, you know!

— I know, so therefore your new number 8179 is also a prime. You will just have to paste four new digits over the four old ones on your office door.

— No, it’s not that simple. Suppose that I change the first digit to an 8, then the number will read 8033 which is not a prime!

— I see, being the prime minister you cannot stand having a non-prime number on your door even for a few seconds.

— Correct! So I must invent a scheme for going from 1033 to 8179 by a path of prime numbers where only one digit is changed from one prime to the next prime.Now, the minister of finance, who had been eavesdropping, intervened.

— No unnecessary expenditure, please! I happen to know that the price of a digit is one pound.

— Hmm, in that case I need a computer program to minimize the cost. You don’t know some very cheap software gurus, do you?

— In fact, I do. You see, there is this programming contest going on… Help the prime minister to find the cheapest prime path between any two given four-digit primes! The first digit must be nonzero, of course. Here is a solution in the case above.

1033

1733

3733

3739

3779

8779

8179

The cost of this solution is 6 pounds. Note that the digit 1 which got pasted over in step 2 can not be reused in the last step – a new 1 must be purchased.

Input

One line with a positive number: the number of test cases (at most 100). Then for each test case, one line with two numbers separated by a blank. Both numbers are four-digit primes (without leading zeros).

Output

One line for each case, either with a number stating the minimal cost or containing the word Impossible.

Sample Input

  1. 3
  2. 1033 8179
  3. 1373 8017
  4. 1033 1033

Sample Output

  1. 6
  2. 7
  3. 0

问题大意

从一个素数换到另一个素数,每次只能换一个数字(一位)且换后的每次都是素数。求最小次数?

C++代码

  1. #include<iostream>
  2. #include<cstring>
  3. #include<queue>
  4. using namespace std;
  5. const int maxn = 10000;
  6. bool isprime[maxn + 1];
  7. int dp[maxn + 1];
  8. int getNext(int num, int t, int change){
  9. //num : 当前的数,t当前的位置,change是改变位的值
  10. if(t == 0) return num / 10 * 10 + change; //最低位
  11. else if(t == 1) return num /100 * 100 + change * 10 + num % 10;
  12. else if(t == 2) return num /1000 * 1000 + change * 100 + num % 100;
  13. else return change * 1000 + num % 1000;
  14. }
  15. int main(){
  16. fill(isprime+2, isprime + maxn, true);
  17. for(int i = 2; i <= maxn; i++){
  18. if(isprime[i]){
  19. for(int j = i * i; j <= maxn; j += i){
  20. isprime[j] = false;
  21. }
  22. }
  23. }//打表
  24. int T;
  25. cin>>T;
  26. while(T--){
  27. int a, b;
  28. cin>>a>>b;
  29. fill(dp, dp + maxn, 0x3f);
  30. dp[a] = 0; //记录从一个prime跳跃到另一个prime所需的最少次数
  31. queue<int> q;
  32. q.push(a);
  33. while(!q.empty()){
  34. int cur = q.front(); //取出队列的第一个
  35. q.pop();
  36. for(int i = 0; i < 4; i++){
  37. for(int j = 0; j < 10; j++){
  38. if(i == 3 && j == 0) continue; //
  39. int next = getNext(cur, i, j); //替换
  40. if(isprime[next] == false || dp[next] <= dp[cur]) continue;
  41. // 不是素数不行,如果到next已经有更小的那也不用这个变换路径了
  42. dp[next] = dp[cur] + 1;
  43. q.push(next);
  44. }
  45. }
  46. }
  47. cout<<dp[b]<<endl;
  48. }
  49. return 0;
  50. }
  51. /*
  52. 3
  53. 1033 8179
  54. 1373 8017
  55. 1033 1033
  56. */

Prime Path素数筛与BFS动态规划的更多相关文章

  1. POJ 3126 Prime Path 素数筛,bfs

    题目: http://poj.org/problem?id=3126 困得不行了,没想到敲完一遍直接就A了,16ms,debug环节都没进行.人品啊. #include <stdio.h> ...

  2. Prime Path(POJ - 3126)【BFS+筛素数】

    Prime Path(POJ - 3126) 题目链接 算法 BFS+筛素数打表 1.题目主要就是给定你两个四位数的质数a,b,让你计算从a变到b共最小需要多少步.要求每次只能变1位,并且变1位后仍然 ...

  3. Prime Path(素数筛选+bfs)

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 9519   Accepted: 5458 Description The m ...

  4. POJ - 3126 Prime Path 素数筛选+BFS

    Prime Path The ministers of the cabinet were quite upset by the message from the Chief of Security s ...

  5. Prime Path (poj 3126 bfs)

    Language: Default Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 11703   Ac ...

  6. POJ 3216 Prime Path(打表+bfs)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 27132   Accepted: 14861 Desc ...

  7. Prime Path(POJ 3126 BFS)

    Prime Path Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 15325   Accepted: 8634 Descr ...

  8. [POJ268] Prime Distance(素数筛)

    /* * 二次筛素数 * POJ268----Prime Distance(数论,素数筛) */ #include<cstdio> #include<vector> using ...

  9. POJ 3126 - Prime Path - [线性筛+BFS]

    题目链接:http://poj.org/problem?id=3126 题意: 给定两个四位素数 $a,b$,要求把 $a$ 变换到 $b$.变换的过程每次只能改动一个数,要保证每次变换出来的数都是一 ...

随机推荐

  1. 子域名爆破工具:OneForALL

    0x00 简介 OneForAll是一款功能强大的子域收集工具 0x01 下载地址 码云: https://gitee.com/shmilylty/OneForAll.git Github: http ...

  2. Xcode 6.3.1Mac版 V6.4.Beta3免费下载

    Xcode for mac是Mac OS系统以及IOS系统开发者专用于构建 Mac OS X 及 iOS 应用程序的完整工具集 - Xcode 5 的工具经过重新设计,它们的性能更优秀.使用更容易,能 ...

  3. AJ学IOS(07)UI之UITextField代理事件_类似QQ登陆窗口的简单实现

    AJ分享,必须精品 先看效果图: 学习代码 // // NYViewController.m // 05-UITextField事件_UIKit复习 // // Created by apple on ...

  4. Eclipse版本控制

    各版本的区别: 1.Eclipse IDE for Java Developers 是Eclipse的platform加上JDT插件,用来java开发的 2.Eclipse IDE for Java  ...

  5. C. Beautiful Regional Contest

    用前缀和写一直wa.. 思路:让金牌和银牌最少,通多增加铜牌的方式来扩大总奖牌的个数. #include<bits/stdc++.h> using namespace std; map&l ...

  6. H - Hamiltonian Hypercube Gym - 101170H

    规律题 首先我们要知道他的顺序是怎么来的,首先当n等于1时,是0,1 当n=2时,先按照与按顺序在他们前面分别加0,即00,01,在逆序加1,即11,10 构成的顺序为00,01,11,10:往后同理 ...

  7. Flutter Weekly Issue 52

    教程 一个易迁移.兼容性高的 Flutter 富文本方案 复杂业务如何保证Flutter的高性能高流畅度? 插件 flutter_color_models A wrapper for the Dart ...

  8. python 给字典按值排序,同样适合于其他

    sorted_items = sorted(dico.items(),key=lambda x:(-x[1],x[0]))

  9. 常用的python开发工具对比

    一名优秀的Python开发人员都有一套好用的Python开发工具,好的开发工具可以使Python开发人员的工作更高效,以下是几款比较好用的Python开发工具,Python开发人员,尤其是初学者,可以 ...

  10. beego微信网页授权

    beego.Get("MP_verify_Rj3QAbcdU0MRsyS.txt", func(context *context.Context) { context.Respon ...