Facebook Hacker Cup 2015 Round 1 Homework(附带测试数据)

题目描述:

Homework10 points

Your first-grade math teacher, Mr. Book, has just introduced you to an amazing new concept — primes! According to your notes, a prime is a positive integer greater than 1 that is divisible by only 1 and itself.

Primes seem fun, but without giving you and your 6-year-old colleagues time to consider their implications, he‘s promptly gone on to define another term: primacity. He explains that the primacity of an integer is
the number of distinct primes which divide it. For example, the primacity of 12 is 2 (as it‘s divisible by primes 2 and 3), the primacity of 550 is 3 (as it‘s divisible by primes 2, 5, and 11), and the primacity of 7 is 1 (as the only prime it‘s divisible
by is 7).

Following his lesson, Mr. Book has given you homework with some rather mean questions of the following form: Given 3 integers AB, and K, how many integers in the
inclusive range [AB] have a primacity of exactly K?

Mr. Book probably expects his little homework assignment to take you and your classmates the rest of the year to complete, giving him time to slack off and nap during the remaining math classes. However, you want
to learn more things from him instead! Can you use the skills you‘ve learned in your first-grade computer science classes to finish Mr. Book‘s homework before tomorrow‘s math class?

Input

Input begins with an integer T, the number of homework questions. For each question, there is one line containing 3 space-separated integers: AB, and K.

Output

For the ith question, print a line containing "Case #i: " followed by the number of integers in the inclusive range [AB] with a primacity of K.

Constraints

1 ≤ T ≤ 100

2 ≤ A ≤ B ≤ 107

1 ≤ K ≤ 109

Explanation of Sample

In the first test case, the numbers in the inclusive range [5, 15] with primacity 2 are 6, 10, 12, 14, and 15. All other numbers in this range have primacity 1.

Example input ·

Example output ·

5
5 15 2
2 10 1
24 42 3
1000000 1000000 1
1000000 1000000 2
Case #1: 5
Case #2: 7
Case #3: 2
Case #4: 0
Case #5: 1

解题思路:

这个题很水,打个表就过了。这个表记录的是某个数有多少个质因数。表的处理方式类似于筛素数。

题目代码:

#include <set>
#include <map>
#include <queue>
#include <math.h>
#include <vector>
#include <string>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <iostream>
#include <cctype>
#include <algorithm>

#define eps 1e-10
#define pi acos(-1.0)
#define inf 107374182
#define inf64 1152921504606846976
#define lc l,m,tr<<1
#define rc m + 1,r,tr<<1|1
#define zero(a) fabs(a)<eps
#define iabs(x)  ((x) > 0 ? (x) : -(x))
#define clear1(A, X, SIZE) memset(A, X, sizeof(A[0]) * (min(SIZE,sizeof(A))))
#define clearall(A, X) memset(A, X, sizeof(A))
#define memcopy1(A , X, SIZE) memcpy(A , X ,sizeof(X[0])*(SIZE))
#define memcopyall(A, X) memcpy(A , X ,sizeof(X))
#define max( x, y )  ( ((x) > (y)) ? (x) : (y) )
#define min( x, y )  ( ((x) < (y)) ? (x) : (y) )
using namespace std;

const int M = 10000007;

int f[M];
void pri()
{
    int t = 0;
    for(int i = 2; i <= M; i++)
    {
       if(!f[i])
       {
           f[i]++;
           for(int j=2;i*j<=M;j++)
           {
               f[i*j]++;
           }
       }
       //printf("%d %d\n",f[i] ,i);
    }

}

int main()
{
    //freopen("data.txt","w",stdout);
    pri();
    int t,case1=1;
    while(scanf("%d",&t)!=EOF)
    {
        while(t--)
        {
            int a,b,k,ans;
            scanf("%d%d%d",&a,&b,&k);
            ans=0;
            for(int i=a;i<=b;i++)
            {
                if(f[i]==k)ans++;
            }
            printf("Case #%d: %d\n",case1++,ans);
        }
    }
    return 0;
}

题目最终测试数据:

链接: http://pan.baidu.com/s/1yGmqa 

密码: c5t1

时间: 2024-10-21 07:09:45

Facebook Hacker Cup 2015 Round 1 Homework(附带测试数据)的相关文章

Facebook Hacker Cup 2015 Round 1 Autocomplete (附带测试数据)

题目描述: Autocomplete25 points Since you crave state-of-the-art technology, you've just purchased a phone with a great new feature: autocomplete! Your phone's version of autocomplete has some pros and cons. On the one hand, it's very cautious. It only a

Facebook Hacker Cup 2015 Round 1 --- Homework

给一个区间,求该区间内 质因子个数等于k的数 的个数. 暴力预处理一下啦 #include<cstdio> #include<cstring> using namespace std; const int maxn=10000010; bool pri[maxn]; int cnt[maxn]; void init() { memset(pri,1,sizeof pri); memset(cnt,0,sizeof cnt); for(int i=2;i<=10000000;i

Facebook Hacker Cup 2015 Round 1 Winning at Sports (附带测试数据)

题目描述: Winning at Sports25 points In the game of Sports, the object is have more points than the other team after a certain amount of time has elapsed. Scores are denoted by two hyphen-separated integers. For example, scores may include 3-2, 4-1, or 1

Facebook Hacker Cup 2015 Round 1--Corporate Gifting(树形动态规划)

原题:https://www.facebook.com/hackercup/problems.php?pid=759650454070547&round=344496159068801 题意:给定一颗有根树,在树上下层的节点要给上层节点礼物,根节点的礼物则给慈善会,但是给礼物有个条件就是你不能送你的父节点已经送出的礼物.问满足要求的最少花费. 题解:这个题卡了一段时间,类似于染色问题,可以用树形动态规划求解.因为已知节点个数为N,则我们单个节点的最大花费不会超过log2(N) = 18. 1.

Facebook Hacker Cup 2015 Round 1--Winning at Sports(动态规划)

原题:https://www.facebook.com/hackercup/problems.php?pid=688426044611322&round=344496159068801 题意:你和一个朋友玩足球游戏,分数从0-0开始,最终你总是赢,并且你主要有两种方式赢,第一种stressFree方式你肯定要进第一个球并且总是比你的朋友分数高,第二种stressFull方式除了你的朋友达到最终分数时,在游戏中你不可能比你的朋友分数高.给出一个比分,请分别输出在两种情况下你能赢的方式. 题解:类似

Facebook Hacker Cup 2015 Round 1--Homework(筛选法求素数)

题意:给定A,B,K(A<=B)三个数,问在[A,B]范围内的数素数因子个数为K的个数. 题解:典型的筛选法求素数.首先建立一个保存素数因子个数的数组factorNum[],以及到n为止含有素数因子个数为k的二维数组sumNum[n][k]. factorNum可以由筛选法确定,初始化数组为0. 1. 从小到大遍历给定最大范围内的数,若遍历到数n时,factorNum[n]=0则说明这个数是素数(前面没有它的因子). 2. 然后通过增加n的倍数,来筛选出最大范围内含有素数因子n的数. sumNu

Facebook Hacker Cup 2015 Round 1--Autocomplete(字典树新建与查询)

题意:给定N个字符串,让你依次先输入到手机的字典中,再打印出来,打印的时候我们只需要输出字符串的前缀或者全部字符串,要求此前缀不是以往任何字符串的前缀. 题解:典型的字典树,可以利用结构体数组方便的新建与查询,速度比链表更快.只需在插入字符串时统计最长相同的前缀即可. 代码如下: #include<cstdio> #include<cstring> #include<algorithm> using namespace std; #define maxN 1000005

Facebook Hacker Cup 2015 Round 1 --- Autocomplete

Since you crave state-of-the-art technology, you've just purchased a phone with a great new feature: autocomplete! Your phone's version of autocomplete has some pros and cons. On the one hand, it's very cautious. It only autocompletes a word when it

Facebook Hacker Cup 2015 Round 1 --- Winning at Sports

In the game of Sports, the object is have more points than the other team after a certain amount of time has elapsed. Scores are denoted by two hyphen-separated integers. For example, scores may include 3-2, 4-1, or 10-0. The first number is how many