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 ...
随机推荐
- vue中$emit 和$on 和$set的用法
1.$set的用法:给 student对象新增 age 属性 data () { return { student: { name:"里斯'} } } 直接给student赋值不会触发视图更 ...
- 实时Cartographer测试(1) - rplidar
1.rplidar实时测试 参考文献:http://www.cnblogs.com/liangyf0312/p/8028441.html 修改USB转串口权限 yhexie@ubuntu:~$ cd ...
- HTML5-CSS3-JavaScript(1)
之前大致总结过HTML5的发展. 这里贴出之前的随笔:http://www.cnblogs.com/jiangxiaobo/p/5199924.html 我们就从HTML5的基础总结起.希望可以提高自 ...
- [LeetCode] 422. Valid Word Square_Easy
Given a sequence of words, check whether it forms a valid word square. A sequence of words forms a v ...
- node操作 windows的appdata本地缓存文件
const os = require('os'); const path = require("path"); const fs = require("fs") ...
- mac date
格式化UTC为可读格式 mbp:~ gavin$ date -r 1546848158 2019年 1月 7日 星期一 16时02分38秒 CST 获取当前 UTC mbp:~ gavin$ date ...
- .Net Core资源
官网:https://dotnet.github.io/ 1.开发环境 vs2015安装: .net core sdk : https://download.microsoft.com/downloa ...
- gedit 没有preference项,使preference回归,并用命令行设置行号,解决centos7下中文乱码,text wrapping等问题
1. 最简单的,使preference选项回来: gsettings set org.gnome.settings-daemon.plugins.xsettings overrides '@a{sv} ...
- centos执行-查看,复制,删除-命令的脚本
==================================================================================================== ...
- UVA 10256 The Great Divide(点在多边形内)
The Great Divid [题目链接]The Great Divid [题目类型]点在多边形内 &题解: 蓝书274, 感觉我的代码和刘汝佳的没啥区别,可是我的就是wa,所以贴一发刘汝佳 ...