hdu 4235 容斥原理模板题
题目大意:
输入样例个数T,每个样例输入三个数a,b,n,求[a,b]之间与n互素的个数
基本思路:
互斥,想想这个:AUBUC=A+B+C-A∩B-A∩C-B∩C+A∩B∩C
fac存的是n的素因数que里存的是素因数的乘机以及符号,然后最后n/que得出的就是最后和n非互素的个数
代码如下:
#include<cstdio>
#include<cstdlib>
#include<cstring> using namespace std; typedef long long ll;
const int maxn = 1000000+10;
ll que[maxn],fac[maxn],num; void divid(ll n)
{
num=0;
for(ll i=2;i*i<=n;i++)
{
if(n%i==0)
{
while(n%i==0) n/=i;
fac[num++]=i;
}
}
if(n!=1) fac[num++]=n;
}
ll solve(ll n)
{
int k,t=0;
ll ans=0;
que[t++]=-1;
for(ll i=0;i<num;i++)
{
k=t;
for(ll j=0;j<k;j++)
{
que[t++]=-1*que[j]*fac[i];
}
}
for(ll i=1;i<t;i++) ans+=n/que[i];
return ans;
} int main()
{
int T;
scanf("%d",&T);
int cas=0;
while(T--)
{
ll a,b,n;
scanf("%I64d%I64d%I64d",&a,&b,&n);
divid(n);
ll res=b-solve(b)-(a-1-solve(a-1));
printf("Case #%d: %I64d\n",++cas,res);
}
return 0;
}
hdu 4235 容斥原理模板题的更多相关文章
- HDU 2138 Miller-Rabin 模板题
求素数个数. /** @Date : 2017-09-18 23:05:15 * @FileName: HDU 2138 miller-rabin 模板.cpp * @Platform: Window ...
- HDU 2586 (LCA模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2586 题目大意:在一个无向树上,求一条链权和. 解题思路: 0 | 1 / \ 2 3 ...
- HDU 1392 凸包模板题,求凸包周长
1.HDU 1392 Surround the Trees 2.题意:就是求凸包周长 3.总结:第一次做计算几何,没办法,还是看了大牛的博客 #include<iostream> #inc ...
- HDU 2082 母函数模板题
找单词 Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status De ...
- HDU 2087 kmp模板题
s为主串 t为模板串 求t的nextt 加const #include<stdio.h> #include<string.h> #include<algorithm> ...
- 【网络流#3】hdu 1532 - Dinic模板题
输入为m,n表示m条边,n个结点 记下来m行,每行三个数,x,y,c表示x到y的边流量最大为c 这道题的模板来自于网络 http://blog.csdn.net/sprintfwater/articl ...
- poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题
poj 1251 && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...
- 最长回文 HDU - 3068 manacher 模板题
题意:找串的最长回文字串(连续) 题解:manacher版题 一些理解:首位加上任意两个字符是为了判断边界. 本算法主要是为了 1.省去奇偶分类讨论. 2.防止形如aaaaaaa的串使得暴力算法蜕化为 ...
- Saving Princess claire_(hdu 4308 bfs模板题)
http://acm.hdu.edu.cn/showproblem.php?pid=4308 Saving Princess claire_ Time Limit: 2000/1000 MS (Jav ...
随机推荐
- luogu P1047 校门外的树 x
P1047 校门外的树 题目描述 某校大门外长度为L的马路上有一排树,每两棵相邻的树之间的间隔都是1米.我们可以把马路看成一个数轴,马路的一端在数轴0的位置,另一端在L的位置:数轴上的每个整数点,即0 ...
- 5 October
POJ2676 Sudoku 位运算 + 搜索.更好的优化方法:方案数最小的空格先填. 把某一位 置为 0:a &=~ (1<<n) 把某一位 置为 1:a |= (1<&l ...
- 使用CFStringTransform将汉字转换为拼音
之前做通讯录相关的一些App时,有一个比较常用的算法是将汉字转换成拼音.当时采用的做法是:将各个拼音段的首个汉字(按Unicode排序)做成两个数组,一个数组存拼音,另一个数组存拼音对应首个汉字的Un ...
- rem布局及响应式布局
流式布局(100%布局)(可能:做的页面不是很美观)效果与弹性布局一样 不设置宽高:表示宽 auto 最外层:保证100% 等比例缩放的算法: 320/10 已知的一个比例 = 已知的宽/x ...
- 20150722---点击按钮使指定的控件可见部分平移(JS)
前段代码: <div id="out" style=" width:400px;overflow:hidden;"> <div id=&quo ...
- Delphi 文件转换Base64
uses EncdDecd; function FileToBase64(FileName: string): string; var MemoryStream: TMemoryStream;beg ...
- EL表达式(三)自定义 EL 表达式
自定义EL函数(静态方法): 编写步骤: 1.编写一个Java类,提供一个静态方法 import java.util.List; public class GetLength { public sta ...
- (appium+python)UI自动化_02_appium启动手机app
前提:需先安装配置好appium+python自动化环境,已配置好环境的小伙伴可以参考以下步骤启动Android app,具体步骤如下: 一.USB连接手机 (1)手机USB连接电脑 (2)手机打开开 ...
- poj2431Expedition
A group of cows grabbed a truck and ventured on an expedition deep into the jungle. Being rather poo ...
- 前端webpack & vue
地址 : https://blog.csdn.net/jiang7701037