uva 11246 - K-Multiple Free set(数论)

题目链接:uva 11246 - K-Multiple Free set

题目大意:给定n,k。求一个元素不大于n的子集,要求该子集的元素尽量多,并且不含两个数满足a?k=b.

解题思路:容斥原理,f(i)=(?1)inki,取f函数的和即可。

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;
typedef long long ll;

ll solve (ll n, ll k) {
    ll ans = 0, sign = 1;
    while (n) {
        ans += sign * n;
        n /= k;
        sign *= -1;
    }
    return ans;
}

int main () {
    int cas;
    scanf("%d", &cas);
    while (cas--) {
        ll n, k;
        scanf("%lld%lld", &n, &k);
        printf("%lld\n", solve(n, k));
    }
    return 0;
}

uva 11246 - K-Multiple Free set(数论),布布扣,bubuko.com

时间: 2024-10-27 10:52:25

uva 11246 - K-Multiple Free set(数论)的相关文章

UVA 11246 - K-Multiple Free set(数论推理)

UVA 11246 - K-Multiple Free set 题目链接 题意:一个{1..n}的集合,求一个子集合,使得元素个数最多,并且不存在有两个元素x1 * k = x2,求出最多的元素个数是多少 思路:推理一下, 一开始n个 先要删除k倍的,删除为{k, 2k, 3k, 4k, 5k, 6k...},会删掉多余的k^2,因此在加回k^2倍的数 然后现在集合中会出现情况的只有k^2的倍数,因此对k^2倍的数字看成一个新集合反复做这个操作即可,因此最后答案为n - n / k + n /

UVA 11256 - Repetitive Multiple(数论)

UVA 11256 - Repetitive Multiple 题目链接 题意:找出一个最小值满足: 是n的倍数, 是重复数字(根据题目中的定义) 思路:如果是重复数字,形式必然是100010001这类形式乘上一个对应位数的数字,所以可以枚举这样形式的数字,和n取gcd,如果剩下的数字位数满足小于位数,那么就乘上一个数字使得等于最小满足位数,这样不断记录最小值即可 代码: #include <cstdio> #include <cstring> #include <algor

UVA 10791 Minimum Sum LCM (数论)

LCM (Least Common Multiple) of a set of integers is defined as the minimum number, which is a multiple of all integers of that set. It is interesting to note that any positive integer can be expressed as the LCM of a set of positive integers. For exa

uva 12119 - The Bells are Ringing(数论+枚举)

题目链接:uva 12119 - The Bells are Ringing 题目大意:有三个钟,分别间隔t1,t2,t3秒响一次,0时刻同时响,给定M,问有没又满足的三个数,最小公倍数为M.并且t3-t1<=25 解题思路:因为M为t1,t2,t3的最小公倍数,所以ti一定为M的因子,所以只要枚举因子判断即可. #include <cstdio> #include <cstring> #include <algorithm> using namespace st

uva 11256 - Repetitive Multiple(gcd+暴力)

题目链接:uva 11256 - Repetitive Multiple 题目大意:给定一个数n,要求找到最小的k,使得k?n为题目中定义的重复数字. 解题思路:枚举k?n的循环节长度,比如当前枚举为2,那么一次判断u=1001,1001001,1001001001 ...,取d = gcd(n,u), 那么k = u / d, a = n / d (因为n?k=u?a)并且保证a的长度为2,所以k和a要同时扩大相应倍数.枚举过程中为何k. #include <cstdio> #include

UVA 10548 - Find the Right Changes(数论)

UVA 10548 - Find the Right Changes 题目链接 题意:给定a,b,c,表示货物的价值,求由A货物和B货物组成C货物有几种方法,判断有无解和是否有无限多种 思路:扩展欧几里得求通解,去计算上限和下限就能判断 代码: #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> using namespace std; const long l

UVA 10375 Choose and divide(数论)

The binomial coefficient C(m,n) is defined as m! C(m,n) = -------- n!(m-n)! Given four natural numbers p, q, r, and s, compute the the result of dividing C(p,q) by C(r,s). The Input Input consists of a sequence of lines. Each line contains four non-n

uva 10773 - Back to Intermediate Math(数论)

题目链接:uva 10773 - Back to Intermediate Math 题目大意:有一天河,宽d,水流速度v,船速u,问说垂直过河和最快过河的时间差,如果不能过河输出"can't determine". 解题思路:将u的速度分解成水平方向和竖直方向的两个速度,使水平方向速度恰好为v,船即可垂直过河,速度为竖直方向速度. #include <cstdio> #include <cstring> #include <cmath> const

UVa 11997 K Smallest Sums 优先队列&amp;&amp;打有序表&amp;&amp;归并

UVA - 11997 K Smallest Sums Time Limit: 1000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status You're given k arrays, each array has k integers. There are k^k ways to pick exactly one element in each array and calculate the sum o