【BZOJ1101】Zap [莫比乌斯反演]
Zap
Time Limit: 10 Sec Memory Limit: 162 MB
[Submit][Status][Discuss]
Description
对于给定的整数a,b和d,有多少正整数对x,y,满足x<=a,y<=b,并且gcd(x,y)=d。
Input
第一行包含一个正整数n,表示一共有n组询问。接下来n行,每行表示一个询问,每行三个正整数,分别为a,b,d。
Output
输出一个正整数,表示满足条件的整数对数。
Sample Input
4 5 2
6 4 3
Sample Output
2
HINT
1<=n<= 50000, 1<=d<=a,b<=50000
Solution
我们运用莫比乌斯反演,然后推一下式子得到:
我们依旧对于下界分块求解即可。
Code
- #include<iostream>
- #include<string>
- #include<algorithm>
- #include<cstdio>
- #include<cstring>
- #include<cstdlib>
- #include<cmath>
- using namespace std;
- typedef long long s64;
- const int ONE = ;
- int T;
- int n,m,k;
- bool isp[ONE];
- int prime[ONE],p_num;
- int miu[ONE],sum_miu[ONE];
- s64 Ans;
- int get()
- {
- int res=,Q=; char c;
- while( (c=getchar())< || c>)
- if(c=='-')Q=-;
- if(Q) res=c-;
- while((c=getchar())>= && c<=)
- res=res*+c-;
- return res*Q;
- }
- void Getmiu(int MaxN)
- {
- miu[] = ;
- for(int i=; i<=MaxN; i++)
- {
- if(!isp[i])
- prime[++p_num] = i, miu[i] = -;
- for(int j=; j<=p_num, i*prime[j]<=MaxN; j++)
- {
- isp[i * prime[j]] = ;
- if(i%prime[j] == )
- {
- miu[i * prime[j]] = ;
- break;
- }
- miu[i * prime[j]] = -miu[i];
- }
- miu[i] += miu[i-];
- }
- }
- void Solve()
- {
- n=get(); m=get(); k=get();
- if(n > m) swap(n,m);
- int N = n/k, M = m/k; Ans = ;
- for(int i=,j=; i<=N; i=j+)
- {
- j = min(N/(N/i), M/(M/i));
- Ans += (s64)(N/i) * (M/i) * (miu[j] - miu[i-]);
- }
- printf("%lld\n",Ans);
- }
- int main()
- {
- Getmiu(ONE-);
- T=get();
- while(T--)
- Solve();
- }
【BZOJ1101】Zap [莫比乌斯反演]的更多相关文章
- 【题解】Zap(莫比乌斯反演)
[题解]Zap(莫比乌斯反演) 裸题... 直接化吧 [P3455 POI2007]ZAP-Queries 所有除法默认向下取整 \[ \Sigma_{i=1}^x\Sigma_{j=1}^y[(i, ...
- BZOJ1101: [POI2007]Zap(莫比乌斯反演)
1101: [POI2007]Zap Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2951 Solved: 1293[Submit][Status ...
- Bzoj1101: [POI2007]Zap 莫比乌斯反演+整除分块
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1101 莫比乌斯反演 1101: [POI2007]Zap 设 \(f(i)\) 表示 \(( ...
- 1101: [POI2007]Zap(莫比乌斯反演)
1101: [POI2007]Zap Time Limit: 10 Sec Memory Limit: 162 MB Description FGD正在破解一段密码,他需要回答很多类似的问题:对于给定 ...
- bzoj 1101 Zap —— 莫比乌斯反演
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1101 直接莫比乌斯反演. 代码如下: #include<cstdio> #inc ...
- BZOJ 1101 Luogu P3455 POI 2007 Zap (莫比乌斯反演+数论分块)
手动博客搬家: 本文发表于20171216 13:34:20, 原地址https://blog.csdn.net/suncongbo/article/details/78819470 URL: (Lu ...
- BZOJ 1101: [POI2007]Zap( 莫比乌斯反演 )
求 answer = ∑ [gcd(x, y) = d] (1 <= x <= a, 1 <= y <= b) . 令a' = a / d, b' = b / d, 化简一下得 ...
- BZOJ 1101 Zap(莫比乌斯反演)
http://www.lydsy.com/JudgeOnline/problem.php?id=1101 给定a,b,d,求有多少gcd(x,y)==d(1<=x<=a&& ...
- 莫比乌斯反演学习笔记+[POI2007]Zap(洛谷P3455,BZOJ1101)
先看一道例题:[POI2007]Zap BZOJ 洛谷 题目大意:$T$ 组数据,求 $\sum^n_{i=1}\sum^m_{j=1}[gcd(i,j)=k]$ $1\leq T\leq 50000 ...
随机推荐
- c#一些常用的方法集合
是从一个asp.net mvc的项目里看到的.挺实用的. 通过身份证号码获取出生日期和性别 通过身份证号码获取出生日期和性别 #region 由身份证获得出生日期 public static stri ...
- ABP框架设置默认语言
在Global.asax文件中设置 private readonly IAbpWebLocalizationConfiguration _webLocalizationConfiguration; p ...
- 使用IDEA快速搭建Gradle项目
- CentOs 版本名字说明
What images are in this directory CentOS-6.3-x86_64-netinstall.iso This is the network install and r ...
- oracle 数据库字段名与实体类字段名称不匹配的处理方法
之前公司一直都使用sql server 即使数据库字段名称与实体类名称不相同 可以使用诸如: select id as userId from tb_user 这种写法,可换到了oracle 之后坑爹 ...
- Leetcode 672.灯泡开关II
灯泡开关II 现有一个房间,墙上挂有 n 只已经打开的灯泡和 4 个按钮.在进行了 m 次未知操作后,你需要返回这 n 只灯泡可能有多少种不同的状态. 假设这 n 只灯泡被编号为 [1, 2, 3 . ...
- 文本向量化及词袋模型 - NLP学习(3-1)
分词(Tokenization) - NLP学习(1) N-grams模型.停顿词(stopwords)和标准化处理 - NLP学习(2) 之前我们都了解了如何对文本进行处理:(1)如用NLTK文 ...
- React错误总结解决方案(二)
1.React native: Cannot add a child that doesn't have a YogaNode or parent node 该错误一般是因为render方法中注释语句 ...
- mysql insert into select 语法
Insert into Table2(field1,field2,...) select value1,value2,... from Table1 这样就对了
- Manacher算法——最长回文子串
一.相关介绍 最长回文子串 s="abcd", 最长回文长度为 1,即a或b或c或d s="ababa", 最长回文长度为 5,即ababa s="a ...