当前位置:网站首页>(newcoder 15079)无关(容斥原理)
(newcoder 15079)无关(容斥原理)
2022-07-29 03:28:00 【AC__dream】
题目链接:登录—专业IT笔试面试备考平台_牛客网

样例1输入:
1 10 4
2 3 5 7样例1输出:
1样例2输入:
2 10 4
2 3 5 7样例2输出:
0分析:求区间[L,R]内与集合A无关的正整数的个数我们可以转化为求[1,R]内与集合A无关的正整数的个数减去[1,L-1]内与集合A无关的正整数的个数
下面给出区间[1,n]中与集合A无关的正整数个数的求法
设性质Ai为可以被ai整除
则我们要求的问题就是N((1-A1)(1-A2)……(1-An))
由容斥原理得:
直接套公式求解即可:
下面是代码,注意剪枝:
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
long long a[1003];
long long lcm(long long a,long long b)
{
return a/__gcd(a,b)*b;
}
long long solve(long long n,int k)
{
long long ans=0;
for(int i=0;i<1<<k;i++)//暴力枚举所有情况
{
long long sign=1,t=1;
for(int j=0;j<k;j++)
{
if(i>>j&1)
{
sign*=-1;
t=lcm(t,a[j]);
if(t>n) break;//注意剪枝
}
}
ans+=sign*(n/t);
}
return ans;
}
int main()
{
long long l,r;
int k;
cin>>l>>r>>k;
for(int i=0;i<k;i++)
cin>>a[i];
printf("%lld\n",solve(r,k)-solve(l-1,k));
return 0;
} 边栏推荐
- 深入C语言(3)—— C的输入输出流
- RTP send and receive h265
- KNN method predicts pregnancy, KNN principle simple code
- STC MCU drive 1.8 'TFT SPI screen demonstration example (including data package)
- How to judge stun protocol
- 【C】 Array
- Practical application cases of digital Twins - smart energy
- 军品研制过程-转阶段
- MYCAT read / write separation configuration
- Mathematical modeling -- analytic hierarchy process model
猜你喜欢

机器学习【Numpy】

Configure vscade to realize ROS writing

A case of gradually analyzing the splitting of classes -- colorful ball collisions

Makefile details

Understanding of p-type problems, NP problems, NPC problems, and NP hard problems in natural computing

Machine learning based on deepchem

Self study notes on Apache file management -- mapping folders and configuring Apache virtual machines based on single IP and multi domain names

for_each用法示例

STC MCU drive 1.8 'TFT SPI screen demonstration example (including data package)

Flask creation process day05-06 creation project
随机推荐
How to realize shortcut keys for interface scaling by vscade
Example analysis of while, repeat and loop loops in MySQL process control
A simple and general method to obtain the size of function stack space
July 28, 2022 Gu Yujia's study notes
Singleton mode (hungry and lazy)
Kubernetes-1.24.x feature
Sleuth+Zipkin 来进行分布式服务链路的追踪
Web uploader cannot upload multiple files
Multi level wavelet CNN for image restoration
Tencent cloud logs in with PEM
Introduction and advanced MySQL (13)
STC MCU drive 1.8 'TFT SPI screen demonstration example (including data package)
Introduction and advanced MySQL (XIV)
How dare you write a resume that is proficient in concurrent programming? Why do you use a two-way linked list in AQS?
Military product development process - transition phase
年内首个“三连跌” 95号汽油回归“8元时代“
Exness: dove resolution helped gold rebound, and the focus turned to U.S. GDP
C obtains JSON format data asynchronously from the web address
How close can QA be to business code Direct exposure of defects through codediff
Suffix automata (SAM) board from Jly