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. Nginx中的rewrite指令(break,last,redirect,permanent)

    rewite 在server块下,会优先执行rewrite部分,然后才会去匹配location块 server中的rewrite break和last没什么区别,都会去匹配location,所以没必要 ...

  2. Openvpn配置文件详解

    一.vars配置文件 vars配置文件的主要内容如下: cat vars |grep -vE "^#|^$" KEY_DIR定义key生成的目录. KEY_SIZE定义生成私钥的大 ...

  3. mybatis级联

    mybatis中有时候表不能都分成单表进行查询,表之间会有联系,这时候需要将表进行级联 下面讲一下如何将mybatis中 的表进行级联.映射表关系如下 1:创建数据表 DROP TABLE IF EX ...

  4. EditText的一些使用技巧

    1.让EditText不自动获取焦点 将EditText的某个父级控件设置成 android:focusable="true" android:focusableInTouchMo ...

  5. PHP 类名::class含义

    自 PHP 5.5 起,关键词 class 也可用于类名的解析. 使用 ClassName::class 可以获取一个字符串,包含了类 ClassName 的完全限定名称.这对使用了命名空间的类尤其有 ...

  6. 使用MySQL的mysqldump命令备份数据库和把数据库备份文件恢复

    1,备份数据库 mysql -uroot -p123456 db_name > /root/db_name.dump 2,数据库备份文件恢复 mysql -uroot -p123456 db_n ...

  7. [LeetCode] 74. Search a 2D Matrix_Medium tag: Binary Search

    Write an efficient algorithm that searches for a value in an m x n matrix. This matrix has the follo ...

  8. Go vs Erlang - 转

    From http://zhang.hu/go-vs-erlang/ Go vs Erlang 因为 云巴 系统对高并发.低延迟的需求,我们对各个语言.平台做了很多的调研比较工作.这自然就包括致力于开 ...

  9. Node.JS + Mysql数据库

    服务嘛,当然离不开数据库了,你要是见到数据就哭了,我建议你还是看看本文,不要做数据哭啊,哈哈哈 要做 ‘数据酷’嘛,哈哈哈 一 安装 1. wget -i -c http://dev.mysql.co ...

  10. 解决vmvare关闭过慢

    打开虚拟机文件夹下的.vmx文件,将下面的内容添天加进去,保存prefvmx.minVmMemPct = "100" mainMem.useNamedFile = "FA ...