You start with a sequence of consecutive integers. You want to group them into sets.

You are given the interval, and an integer P. Initially, each number in the interval is in its own set.

Then you consider each pair of integers in the interval. If the two integers share a prime factor which is at least P, then you merge the two sets to which the two integers belong.

How many different sets there will be at the end of this process?

Input

One line containing an integer C, the number of test cases in the input file.

For each test case, there will be one line containing three single-space-separated integers A, B, and P. A and B are the first and last integers in the interval, and P is the number as described above.

Output

For each test case, output one line containing the string "Case #X: Y" where X is the number of the test case, starting from 1, and Y is the number of sets.

Limits

Small dataset

1 <= C <= 10

1 <= A <= B <= 1000

2 <= P <= B

Large dataset

1 <= C <= 100

1 <= A <= B <= 1012

B <= A + 1000000

2 <= P <= B

 Sample Input 1 Sample Output 1
2
10 20 5
10 20 3
Case #1: 9
Case #2: 7

题目大概意思就是——给你一个范围A到B,范围中每个数就是一个集合,再给你一个素数P,如果这个范围的两个数有大于或者等于P的素数因子,那么合并两个数所在的集合作为一个集合。

并查集。

#include <iostream>
#include <algorithm> using namespace std; typedef long long ll; static bool test_prime(ll p)
{
if (p < ) return false;
for (ll i = ; i * i <= p; i++)
if (p % i == )
return false;
return true;
} static int parent[]; static int root(int x)
{
if (parent[x] < )
return x;
else
return parent[x] = root(parent[x]);
} static void merge(int a, int b)
{
a = root(a);
b = root(b);
if (a == b) return;
if (parent[a] > parent[b])
swap(a, b);
parent[a] += parent[b];
parent[b] = a;
} int main()
{
int cases;
cin >> cases; for (int cas = ; cas < cases; cas++)
{
ll A, B, P;
cin >> A >> B >> P; for (ll i = A; i <= B; i++)
parent[i - A] = -;
for (ll i = P; i <= B - A; i++)
if (test_prime(i))
{
ll t = B - B % i;
while (t - i >= A)
{
merge(t - A, t - i - A);
t -= i;
}
}
ll ans = ;
for (ll i = A; i <= B; i++)
if (parent[i - A] < )
ans++;
cout << "Case #" << cas + << ": " << ans << "\n";
}
return ;
}

Kattis之旅——Number Sets的更多相关文章

  1. Kattis之旅——Prime Reduction

    A prime number p≥2 is an integer which is evenly divisible by only two integers: 1 and p. A composit ...

  2. Kattis之旅——Chinese Remainder

    Input The first line of input consists of an integers T where 1≤T≤1000, the number of test cases. Th ...

  3. Kattis之旅——Fractional Lotion

    Freddy practices various kinds of alternative medicine, such as homeopathy. This practice is based o ...

  4. Kattis之旅——Rational Arithmetic

    Input The first line of input contains one integer, giving the number of operations to perform. Then ...

  5. Kattis之旅——Divisible Subsequences

    Given a sequence of positive integers, count all contiguous subsequences (sometimes called substring ...

  6. Kattis之旅——Prime Path

    The ministers of the cabinet were quite upset by the message from the Chief of Security stating that ...

  7. Kattis之旅——Eight Queens

    In the game of chess, the queen is a powerful piece. It can attack by moving any number of spaces in ...

  8. Kattis之旅——Factovisors

    The factorial function, n! is defined thus for n a non-negative integer: 0! = 1 n! = n * (n-1)! (n & ...

  9. Kattis之旅——Inverse Factorial

    题目意思就是已知n的阶乘,求n. 当输入的阶乘小于10位数的时候,我们可以用long long将字符串转化成数字,直接计算. 而当输入的阶乘很大的时候,我们就可以利用位数去大概的估计n. //Asim ...

随机推荐

  1. zookeeper(百度百科http://baike.baidu.com/view/3061646.htm?fr=aladdin)

    ZooKeeper是Hadoop的正式子项目,它是一个针对大型分布式系统的可靠协调系统,提供的功能包括:配置维护.名字服务.分布式同步.组服务等.ZooKeeper的目标就是封装好复杂易出错的关键服务 ...

  2. echart图表

    http://echarts.baidu.com/tutorial.html#5%20%E5%88%86%E9%92%9F%E4%B8%8A%E6%89%8B%20ECharts

  3. 利用 SPL 快速实现 Observer 设计模式

    目录: 1.什么是 SPL 2.SplSubject 和 SplObserver 接口 3.为什么使用 SplObjectStorage 类 4.模拟案例 5.结束语 6.下载资源 什么是 SPL S ...

  4. 微服务——RestTemplate

    GET请求: 第一种:getForEntity: 此方法返回的是ResponseEntity,该对象是Spring对HTTP请求响应的封装. RestTemplate rt = new RestTem ...

  5. [sh]getopt参数解析

    https://www.cnblogs.com/FrankTan/archive/2010/03/01/1634516.html sh参数处理方法 * 手工处理方式 * getopts #好像不支持长 ...

  6. python 根据字符串动态的生成变量名并且赋值

    Python 动态的创建变量 一.子符串的形式 这是在今天的一个项目中,发现需要动态的创建很多变量.每个变量对应的值的来源都相同.在网上看了些资料,研究出了这个动态创建变量的牛逼方法. 所用的方法就是 ...

  7. 2018-2019-1 20189221 《Linux内核原理与分析》第八周作业

    2018-2019-1 20189221 <Linux内核原理与分析>第八周作业 实验七 编译链接过程 gcc –e –o hello.cpp hello.c / gcc -x cpp-o ...

  8. selenium及webdriver的原理【转】

    selenium与webdriver整合后,形成的新的测试工具叫做selenium2.x.在selenium1时间,selenium使用javascript来达到测试自动化的目标. 1. seleni ...

  9. ntpdate 正确的做法

    网上很多搜到的资料都是过时的,主要是时间服务器不能用,不管是国内的还是国外的 1. yum -y install ntp ntpdate 2. 在/etc/crontab中,加入: 0 0 0 * 1 ...

  10. 线程间操作无效: 从不是创建控件“button1”的线程访问它。

    .net2后是不能跨线程访问控件的.,窗体上的控件是当前线程创建的,当用户异步执行一个方法:在该方法中给窗体上的控件赋值,记住:当执行一个异步委托的时候,其实 就是开了一个线程去执行那个方法,这样就会 ...