当前位置:网站首页>(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语言(4)——switch的定义与使用
- 基于单片机烟雾温湿度甲醛监测设计
- A simple and general method to obtain the size of function stack space
- Naive Bayes -- continuous data
- STC MCU drive 1.8 'TFT SPI screen demonstration example (including data package)
- 通过递归实现多级联动
- Leetcode 1331 array sequence number conversion [map] the leetcode path of heroding
- Does domestic ERP have a chance to beat sap?
- During the year, the first "three consecutive falls" of No. 95 gasoline returned to the "8 Yuan era"“
- Learn exkmp again (exkmp template)
猜你喜欢

暴力递归到动态规划 01 (机器人移动)

Learn more than 4000 words, understand the problem of this pointing in JS, and handwrite to realize call, apply and bind

Rdkit: introduce smiles code, smart code and Morgan fingerprint (ECFP)

Practical application cases of digital Twins - smart energy

Ten thousand words detailed Google play online application standard package format AAB

Exness: dove resolution helped gold rebound, and the focus turned to U.S. GDP

Asynchronous callback future mode of concurrent mode

Mathematical modeling -- analytic hierarchy process model

CUDA GDB prompt: /tmp/tmpxft**** cudafe1.stub. c: No such file or directory.

How to solve the time zone problem in MySQL timestamp
随机推荐
正则表达绕过waf
Division and description of military technical documents
Rdkit I: using rdkit to screen the structural characteristics of chemical small molecules
Calculation of array serial number of force deduction questions (daily question 7/28)
Simple understanding of CDN, SDN and QoS
CUDA GDB prompt: /tmp/tmpxft**** cudafe1.stub. c: No such file or directory.
军品三大基线(功能基线、分配基线、产品基线)及基线包含的文件
MOS管 —— 快速复苏应用笔记(贰)[参数与应用]
3.2 model saving and loading
makefile详解
2022-07-28 study notes of group 4 self-cultivation class (every day)
Minesweeping simple version
逐步分析类的拆分之案例——五彩斑斓的小球碰撞
Learn exkmp again (exkmp template)
Design of smoke temperature, humidity and formaldehyde monitoring based on single chip microcomputer
Anti vulnerability · benefit from uncertainty --- management?
Score addition and subtraction of force deduction and brushing questions (one question per day 7/27)
基于单片机烟雾温湿度甲醛监测设计
VISO fast rendering convolution block
A simple and general method to obtain the size of function stack space