Kattis之旅——Number Sets
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 |
Case #1: 9 |
题目大概意思就是——给你一个范围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的更多相关文章
- Kattis之旅——Prime Reduction
A prime number p≥2 is an integer which is evenly divisible by only two integers: 1 and p. A composit ...
- Kattis之旅——Chinese Remainder
Input The first line of input consists of an integers T where 1≤T≤1000, the number of test cases. Th ...
- Kattis之旅——Fractional Lotion
Freddy practices various kinds of alternative medicine, such as homeopathy. This practice is based o ...
- Kattis之旅——Rational Arithmetic
Input The first line of input contains one integer, giving the number of operations to perform. Then ...
- Kattis之旅——Divisible Subsequences
Given a sequence of positive integers, count all contiguous subsequences (sometimes called substring ...
- Kattis之旅——Prime Path
The ministers of the cabinet were quite upset by the message from the Chief of Security stating that ...
- Kattis之旅——Eight Queens
In the game of chess, the queen is a powerful piece. It can attack by moving any number of spaces in ...
- Kattis之旅——Factovisors
The factorial function, n! is defined thus for n a non-negative integer: 0! = 1 n! = n * (n-1)! (n & ...
- Kattis之旅——Inverse Factorial
题目意思就是已知n的阶乘,求n. 当输入的阶乘小于10位数的时候,我们可以用long long将字符串转化成数字,直接计算. 而当输入的阶乘很大的时候,我们就可以利用位数去大概的估计n. //Asim ...
随机推荐
- tp5Auth权限实现
原文地址:https://blog.csdn.net/qq_33257081/article/details/79137190 下面本人为大家讲解一下如何实现auth权限, 第一步,新建Auth.ph ...
- golang 的 buffered channel 及 unbuffered channel
The channel is divided into two categories: unbuffered and buffered. (1) Unbuffered channelFor unbuf ...
- cmd项目目录结构以及配置文件的升级编写
一.项目的目录结构: bin:执行文件夹 config:自定义配置文件 lib:公共的模块或者类文件 src:核心业务逻辑代码 二.配置文件的编写 1)config代码如下 from lib.conf ...
- [django]django缓存
发现搞了全局缓存后,刷新得不到最新数据了. 还好有过期时间 redis常用: https://www.cnblogs.com/fansik/p/5483060.html django-redis缓存: ...
- vue proxyTable 跨域问题。
- sql server 函数的自定义
创建用户定义函数.这是一个已保存 Transact-SQL 或公共语言运行时 (CLR) 例程,该例程可返回一个值.用户定义函数不能用于执行修改数据库状态的操作.与系统函数一样,用户定义函数可从查询中 ...
- Objective-C中NSArray的基本用法示例
NSArray的一些用法 NSArray只允许装OC对象,并且不能装空值,空代表数组元素的结束 #pragma mark - NSArray的基本用法 // 创建一个空数组 NSArray *arra ...
- cocos2dx 游戏plist与png完美切成小图python代码
首先需要一份python的切图程序: #python2.5 unpack_plist.py birdfly #! /usr/lical/bin/python import os,sys from xm ...
- HttpClient超时设置setConnectionTimeout和setSoTimeout
http是基于TCP/IP进行通信的,tcp通过3次握手建立连接,并最终以4次挥手终止通信. 知乎上对三次握手和四次挥手有如下解释: 作者:知乎用户链接:https://www.zhihu.com/q ...
- BCB Access violateion at Address 0000 0003. Read of address 0000 0003
来自网页:(我的电脑做不到) 运行一个程序,莫名出现一个对话框:access violation at address 0000.. read of address000试了几次问题依旧,网上搜了下解 ...