G - Non-Prime Factors Kattis - nonprimefactors (筛1-n内的当前数中非素数的个数)

题目链接:

G - Non-Prime Factors

 Kattis - nonprimefactors

题目大意:给你一个数n,然后问你n的因子中非素数的个数。

具体思路:埃筛,把每一个数的因子直接算出来就好了。

AC代码:

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn = 2e6 + 100;
 4 int vis[maxn];
 5 int sto[maxn];
 6 void init()
 7 {
 8     for(int i=2; i<maxn; i++)
 9     {
10         if(vis[i])// 当为非素数的时候,当前的数的非素数因子个数++
11             sto[i]++;
12         for(int j=i*2; j<maxn; j+=i)
13         {
14             vis[j]=1;
15             if(vis[i])// 当i不是因子,当前的数2*j中一定含有i这个非素数因子
16                 sto[j]++;
17         }
18     }
19 }
20 int main()
21 {
22     init();
23     int T;
24     scanf("%d",&T);
25     while(T--)
26     {
27         int n;
28         scanf("%d",&n);
29         printf("%d\n",sto[n]+1);
30     }
31     return 0;
32 }

原文地址:https://www.cnblogs.com/letlifestop/p/10806689.html

时间: 2024-10-17 17:30:23

G - Non-Prime Factors Kattis - nonprimefactors (筛1-n内的当前数中非素数的个数)的相关文章

[CareerCup] 7.7 The Number with Only Prime Factors 只有质数因子的数字

7.7 Design an algorithm to find the kth number such that the only prime factors are 3,5, and 7. 这道题跟之前LeetCode的那道Ugly Number II 丑陋数之二基本没有啥区别,具体讲解可参见那篇,代码如下: class Solution { public: int getKthMagicNumber(int k) { vector<int> res(1, 1); int i3 = 0, i

PAT 1059. Prime Factors (25)

1059. Prime Factors (25) Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1 * p2^k2 *…*pm^km. Input Specification: Each input file contains one test case which gives a positive inte

1059. Prime Factors (25)

时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1* p2^k2 *…*pm^km. Input Specification: Each input file contain

2014辽宁ACM省赛 Prime Factors

问题 L: Prime Factors 时间限制: 1 Sec  内存限制: 128 MB 提交: 36  解决: 28 [提交][状态][论坛] 题目描述 I'll give you a number , please tell me how many different prime factors in this number. 输入 There is multiple test cases , in each test case there is only one line contain

PAT1059. Prime Factors

Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1 * p2^k2 *…*pm^km. Input Specification: Each input file contains one test case which gives a positive integer N in the range of lon

pat1059. Prime Factors (25)

1059. Prime Factors (25) 时间限制 50 ms 内存限制 65536 kB 代码长度限制 16000 B 判题程序 Standard 作者 HE, Qinming Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1 * p2^k2 *…*pm^km. Input Specificatio

A1059. Prime Factors (25)

Given any positive integer N, you are supposed to find all of its prime factors, and write them in the format N = p1^k1 * p2^k2 *…*pm^km. Input Specification: Each input file contains one test case which gives a positive integer N in the range of lon

poj 2689 Prime Distance(大区间筛素数)

http://poj.org/problem?id=2689 题意:给出一个大区间[L,U],分别求出该区间内连续的相差最小和相差最大的素数对. 因为L<U<=2147483647,直接筛素数是不行的,数组就开不了.但是可以根据素数筛的原理.我们先筛出sqrt(2147483647)以内的素数,然后拿这些素数去筛[L,U]之间的素数,即两次素数筛.但是L,U还是很大,但U-L<=1000000,所以进行区间平移,将[L,U]平移为[0,U-L],就能用数组放得下. #include &l

poj 2689 Prime Distance(筛一个区间内的素数(或合数))

: [题意说明] 给你指定的范围[L, U],在这个范围内找出相邻最近和最远的两组质数,若最近或最远值相同,输出较小的那组.其中:1≤L,另U-L≤1000000. [问题分析] 此题与质数有关,显然若是能求出[L, U]之间的质数,然后从前往后扫描一遍即可出需要的结果,但问题是L与U的范围太大,是不可能在规定的时间内实现的. 但这里给我们提供了另一个条件:U-L≤1000000,如果我们只求1000000以内的素数,完全可以在规定的时间实现的!但由于所求的不是1-1000000以内的素数,所以