URAL - 1091 Tmutarakan Exams (简单容斥原理)
题意:K个不同数组成的集合,每个数都不超过S且它们的gcd>1。求这样的数的个数
分析:从2开始枚举gcd,但这样会发生重复。譬如,枚举gcd=2的集合个数和gcd=3的集合个数,枚举6的时候就重复了,所以对于6,10这种质因子个数为2的,要减去。而对于4,8,9这样同一质因子出现超过1次的,不用考虑(相当于莫比乌斯函数值为0)。
因为K和S不大,先预处理出组合数以及每个数对应的质因子个数。然后按容斥计算答案。
#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
const int maxn = 55;
int C[maxn][maxn];
int cnt[maxn];
bool isprime(int n)
{
if(n<2) return false;
for(int i=2;i*i<=n;++i){
if(n%i==0) return false;
}
return true;
}
void pre()
{
for(int i=2;i<maxn;++i){
if(isprime(i)){
cnt[i]=1;
}else{
int tmp = i;
for(int j=2;j*j<=tmp;++j){
if(tmp%j==0){
int t = 0;
while(tmp%j==0) tmp/=j,t++;
if(t>1){ //莫比乌斯函数值为0,不必考虑
cnt[i] = 0;
break;
}
cnt[i]++;
}
}
if(cnt[i]==0) continue;
if(tmp>1) cnt[i]++;
}
}
C[1][0] = C[1][1] = 1;
for(int i=2;i<maxn;++i){
C[i][0] = 1;
for(int j=1;j<=i;++j){
C[i][j] = C[i-1][j]+C[i-1][j-1];
}
}
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
pre();
int K,S;
while(scanf("%d %d",&K, &S)==2){ //枚举公约数
int res=0;
for(int i=2;i<=S;++i){
if(!cnt[i]) continue;
int num = S/i;
int tmp = C[num][K];
if(cnt[i]&1) res += tmp;
else res -= tmp;
}
if(res>10000) res=10000;
printf("%d\n",res);
}
return 0;
}
URAL - 1091 Tmutarakan Exams (简单容斥原理)的更多相关文章
- ural 1091. Tmutarakan Exams(容斥原理)
1091. Tmutarakan Exams Time limit: 1.0 secondMemory limit: 64 MB University of New Tmutarakan trains ...
- Ural 1091 Tmutarakan Exams
Tmutarakan Exams Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Origi ...
- ural 1091. Tmutarakan Exams 和 codeforces 295 B. Greg and Graph
ural 1091 题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1091 题意是从1到n的集合里选出k个数,使得这些数满足gcd大于1 ...
- ural 1091. Tmutarakan Exams(容斥)
http://acm.timus.ru/problem.aspx? space=1&num=1091 从1~s中选出k个数,使得k个数的最大公约数大于1,问这种取法有多少种. (2<=k ...
- 1091. Tmutarakan Exams
1091. Tmutarakan Exams Time limit: 1.0 secondMemory limit: 64 MB University of New Tmutarakan trains ...
- 容斥原理--计算并集的元素个数 URAL 1091
在计数时,必须注意没有重复,没有遗漏.为了使重叠部分不被重复计算,人们研究出一种新的计数方法,这种方法的基本思想是:先不考虑重叠的情况,把包含于某内容中的所有对象的数目先计算出来,然后再把计数时重复计 ...
- F - Tmutarakan Exams URAL - 1091 -莫比乌斯函数-容斥 or DP计数
F - Tmutarakan Exams 题意 : 从 < = S 的 数 中 选 出 K 个 不 同 的 数 并 且 gcd > 1 .求方案数. 思路 :记 录 一 下 每 个 数 的 ...
- 2014 Super Training #3 H Tmutarakan Exams --容斥原理
原题: URAL 1091 http://acm.timus.ru/problem.aspx?space=1&num=1091 题意:要求找出K个不同的数字使他们有一个大于1的公约数,且所有 ...
- Tmutarakan Exams URAL - 1091(莫比乌斯函数 || 容斥)
题意: 求1 - s 中 找出k个数 使它们的gcd > 1 求这样的k个数的对数 解析: 从每个素数的倍数中取k个数 求方案数 然后素数组合,容斥一下重的 奇加偶减 莫比乌斯函数的直接套模 ...
随机推荐
- css图标
一.介绍 采用这种字体,我们可以避免网站制作中采用好多图片,一方面解决了浏览器的兼容性问题.另一方面,这些字体都是矢量字体,我们只要在调整这些图标时,将他们的字体大小以及颜色,我们就可以解决很多不是图 ...
- 用Python获取Linux资源信息的三种方法
方法一:psutil模块 #!usr/bin/env python # -*- coding: utf-8 -*- import socket import psutil class NodeReso ...
- Spring中Adivisor和Aspect的差别(自我理解)
在AOP中有几个概念: - 方/切 面(Aspect):一个关注点的模块化,这个关注点实现可能另外横切多个对象.事务管理是J2EE应用中一个非常好的横切关注点样例. 方面用Spring的Advisor ...
- Spring_day02--Spring的aop操作
Spring的aop操作 1 在spring里面进行aop操作,使用aspectj实现 (1)aspectj不是spring一部分,和spring一起使用进行aop操作 (2)Spring2.0以后新 ...
- Qt slot中获取sender
调用sender();函数 例如获取一个QRadioButton QRadioButton *rb = qobject_cast<QRadioButton *>(sender());
- kafka基础概念
kafka介绍 kafka is a distributed, partitiononed,replicated commited logservice. kafka是一个分布式的.易扩展的.安全性高 ...
- 160530、memcached集群(spring集成的配置)
第一步:在linux机或windows机上安装memcached服务端(server) linux中安装memcached:centos中命令 yum -y install memcached 如果没 ...
- 跟着包子学 css (一)
1.在每次 写css之前 都应该先将浏览器的默认 样式 给清除掉 *{padding:0; margin:0;} h1,h2,h3,h4,h5,h6{font-weight:normal; font- ...
- c# 项目文件,C#viual studio使用方法
一.项目文件 1)Properties节点下主要存放的是当前程序集相关的信息,如版本号.标题等.双击”Properties“,打开如下项目属性窗口,可以设置项目相关的一些参数. 2)引用 节点主要列出 ...
- Zipline Risk and Performance Metrics
Risk and Performance Metrics 风险和性能指标 The risk and performance metrics are summarizing values calcula ...