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 ...
随机推荐
- golang 的 buffered channel 及 unbuffered channel
The channel is divided into two categories: unbuffered and buffered. (1) Unbuffered channelFor unbuf ...
- Scala的apply unapply unapplySeq 语法糖
apply 可以理解为注入 unapply unapplySeq 可以理解为提取 apply 与 unapply 虽然名字相近,但是使用起来区别挺大.apply有点像构造函数unapply主要是结合模 ...
- poj3126
被坑了3个小时,本来以为算法错了,谁知道,竟然是素数筛弄错了 !!! #include <iostream>#include <stdio.h>#include <str ...
- np.Linear algebra学习
转自:https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.linalg.html 1.分解 //其中我觉得可以的就是svd奇异值分解吧 ...
- jenkins openshift 持续集成
参数部分没有 不要照抄,只供参考 需求: CI利用confd+etcd生成配置文件 CI把git的COMMIT 传到openshift的buildconfigs #!/bin/bash echo ec ...
- xcode 报错Failed to load project at xxxx ,incompatible project version
错误原因: 由于工程是低版本的Xcode建立的,在使用高版本的Xcode打开时会出现编译不了工程. 解决方法: 鼠标右击.xcodeproj文件 —>显示包内容 —>打开project.p ...
- [LeetCode] 105. Construct Binary Tree from Preorder and Inorder Traversal_Medium tag: Tree Traversal
Given preorder and inorder traversal of a tree, construct the binary tree. Note:You may assume that ...
- 利用TensorFlow实现多元线性回归
利用TensorFlow实现多元线性回归,代码如下: # -*- coding:utf-8 -*- import tensorflow as tf import numpy as np from sk ...
- 超参数调试、Batch正则化和编程框架
1.调试处理 2.为超参数选择合适的范围 3.超参数在实践中调整:熊猫与鱼子酱 4.正则化网络的激活函数 5.将batch norm拟合进神经网络 6. 为什么Batch Norm会起作用? 7.测试 ...
- Go http server 高并发
先来个段子:[并发处理连接数] 多高? 很高! 到底多高? 没有一亿,都算少了! . . . 然后就没有然后了... “段子 END” 这就是目前中国企业的通病:提个概念,没有答案,最后造成概念也模糊 ...