HDU 1695 GCD 欧拉函数+容斥定理
输入a b c d k求有多少对x y 使得x在a-b区间 y在c-d区间 gcd(x, y) = k 此外a和c一定是1
由于gcd(x, y) == k 将b和d都除以k 题目转化为1到b/k 和1到d/k 2个区间 如果第一个区间小于第二个区间 讲第二个区间分成2部分来做1-b/k 和 b/k+1-d/k
第一部分对于每一个数i 和他互质的数就是这个数的欧拉函数值 全部数的欧拉函数的和就是答案
第二部分能够用全部数减去不互质的数 对于一个数i 分解因子和他不互质的数包括他的若干个因子 这个用容斥原理 奇加偶减
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef __int64 LL;
const int maxn = 100010;
LL phi[maxn];
LL sum[maxn], p[maxn][33];
void phi_table(int n)
{
memset(sum, 0, sizeof(sum));
memset(phi, 0, sizeof(phi));
phi[1] = 1;
for(int i = 2; i <= n; i++)
{
if(!phi[i])
for(int j = i; j <= n; j += i)
{
if(!phi[j])
phi[j] = j;
phi[j] = phi[j] / i * (i-1);
p[j][sum[j]++] = i;
}
phi[i] += phi[i-1];
}
} void dfs(int id, LL num, LL cnt, int a, LL& ans, int x)
{
if(id == sum[x])
{
if(cnt == 0)
return;
if(cnt&1)
ans += a/num;
else
ans -= a/num;
return;
}
dfs(id+1, num*p[x][id], cnt+1, a, ans, x);
dfs(id+1, num, cnt, a, ans, x);
}
LL cal(int x, int a)
{
LL ans = 0;
dfs(0, 1, 0, a, ans, x);
return ans;
}
int main()
{
phi_table(100000);
int cas = 1;
int T;
scanf("%d", &T);
while(T--)
{
int a, b, k;
scanf("%d %d %d %d %d", &a, &a, &b, &b, &k);
if(!k)
{
printf("Case %d: %d\n", cas++, 0);
continue;
}
if(a > b)
swap(a, b);
a /= k;
b /= k;
LL ans = phi[a];
for(int i = a+1; i <= b; i++)
ans += a-cal(i, a);
printf("Case %d: %I64d\n", cas++, ans);
}
return 0;
}
HDU 1695 GCD 欧拉函数+容斥定理的更多相关文章
- HDU 1695 GCD 欧拉函数+容斥定理 || 莫比乌斯反演
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- HDU 1695 GCD (欧拉函数,容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- hdu 1695 GCD (欧拉函数+容斥原理)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- hdu 1695 GCD 欧拉函数 + 容斥
http://acm.hdu.edu.cn/showproblem.php?pid=1695 要求[L1, R1]和[L2, R2]中GCD是K的个数.那么只需要求[L1, R1 / K] 和 [L ...
- HDU 1695 GCD 欧拉函数+容斥原理+质因数分解
链接:http://acm.hdu.edu.cn/showproblem.php?pid=1695 题意:在[a,b]中的x,在[c,d]中的y,求x与y的最大公约数为k的组合有多少.(a=1, a ...
- hdu 1695 GCD(欧拉函数+容斥)
Problem Description Given 5 integers: a, b, c, d, k, you're to find x in a...b, y in c...d that GCD( ...
- hdu 6390 欧拉函数+容斥(莫比乌斯函数) GuGuFishtion
http://acm.hdu.edu.cn/showproblem.php?pid=6390 题意:求一个式子 题解:看题解,写代码 第一行就看不出来,后面的sigma公式也不会化简.mobius也不 ...
- hdu1695(莫比乌斯)或欧拉函数+容斥
题意:求1-b和1-d之内各选一个数组成数对.问最大公约数为k的数对有多少个,数对是有序的.(b,d,k<=100000) 解法1: 这个能够简化成1-b/k 和1-d/k 的互质有序数对的个数 ...
- HDU 2588 GCD (欧拉函数)
GCD Time Limit: 1000MS Memory Limit: 32768KB 64bit IO Format: %I64d & %I64u Submit Status De ...
随机推荐
- Promise 异步执行的同步操作
Promise 是用来执行异步操作的. 但有时一个异步操作需要等其他的异步操作完成,这时候就可以使用then来做. function loadImageAsync(url) { return new ...
- 关于 linux ssh 的配置.
一.禁止root用户远程登录: # cd /etc/ssh # vi sshd_config 将 permitRootLogin 后面的值改成 no 如下图: 然后再重启sshd服务即可,如下: # ...
- undefined和null的区别
在javascript中undefined和null几乎是没有区别的 undefined==null;//true; 区别: null是一个表示“无”的对象,转为数值为0:或者说没有对象,此处 ...
- 前端--关于javascript基础
首先javascript不是浏览器的附属品,只能说它大多数的运行环境是在浏览器中的,但又不仅仅局限于浏览器中.它是一门真正的程序设计语言,在这方面它和java.c.c++.c#是等同的,只不过它不直接 ...
- C# 让textbox 只能输入数字的方法
使用textBox控件的KeyPress事件 private void textBox_KeyPress(object sender, KeyPressEventArgs e) { if (e.Key ...
- Volley的三种基本用法StringRequest的Get和post用法以及JsonObjectRequest
首先做出整个应用的全局请求队列 package com.qg.lizhanqi.myvolleydemo; import android.app.Application; import com.and ...
- Android SQLite的使用2(非原创)
1.数据库的增.删.改.查:execSQL方法 public void insertAction() {//添加信息 db.execSQL("insert into Emp(name,sal ...
- struts2的工作机制
struts2的工作机制 原文:http://eoasis.iteye.com/blog/642586 概述 本章讲述Struts2的工作原理. 读者如果曾经学习过Struts1.x或者有过Strut ...
- netCDF
NetCDF started in 1989 most used in geoscience community array-oriented self-describing header, desc ...
- block中无法使用C数组变量
在Objective-C的block中无法使用C数组,即使我们不对C数组做任何改变,编译的时候也会报错: #include <stdio.h> int main() { const cha ...