BZOJ2301: [HAOI2011]Problem b(莫比烏斯反演)

Description

對于給出的n個詢問,每次求有多少個數對(x,y),滿足a≤x≤b,c≤y≤d,且gcd(x,y) = k,gcd(x,y)函數為x和y的最大公約數。

Input

第一行一個整數n,接下來n行每行五個整數,分別表示a、b、c、d、k

Output

共n行,每行一個整數表示滿足要求的數對(x,y)的個數?

Sample Input

2
2 5 1 5 1
1 5 1 5 2

Sample Output

14
3

解題思路:

和1101一樣,最后二維容斥一下就好了。

代碼:

#include<cstdio>
#include<cstring>
#include<algorithm>
typedef long long lnt;
const int N=50010;
int prime[N];
int miu[N];
lnt s[N];
bool vis[N];
int cnt;
int T;
void gtp(void)
{miu[1]=1;for(int i=2;i<N;i++){if(!vis[i]){prime[++cnt]=i;miu[i]=-1;}for(int j=1;j<=cnt&&prime[j]*i<N;j++){vis[prime[j]*i]=true;if(i%prime[j]==0){miu[i*prime[j]]=0;break;}miu[prime[j]*i]=-miu[i];}}for(int i=1;i<N;i++)s[i]=s[i-1]+miu[i];return ;
}
lnt query(lnt a,lnt b,lnt d)
{a/=d;b/=d;lnt c=std::min(a,b);lnt ans=0;for(int k=1,u;k<=c;k=u+1){u=std::min(a/(a/k),b/(b/k));ans+=(s[u]-s[k-1])*(a/k)*(b/k);}return ans;
}
int main()
{gtp();scanf("%d",&T);while(T--){lnt a,b,c,d,k;scanf("%lld%lld%lld%lld%lld",&a,&b,&c,&d,&k);a--,c--;lnt ans=query(b,d,k)+query(a,c,k)-query(a,d,k)-query(b,c,k);printf("%lld\n",ans);}return 0;
}

?

轉載于:https://www.cnblogs.com/blog-Dr-J/p/10161311.html

本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/249981.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/249981.shtml
英文地址,請注明出處:http://en.pswp.cn/news/249981.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

Js正則表達式數字或者帶小數點的數字

function chk() {var patrn /^\d(\.\d)?$/;var result true;$("input[typetext]").each(function () {if (!patrn.exec(this.value)) {alert("請輸入正確的數字&#xff01;");result false;}})return result;}轉載于:https://www.cnblogs.com/smzd/p/…

FastJson/spring boot: json輸出

1.引入FastJson依賴包 <!-- FastJson --><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.15</version></dependency>pom.xml參考 <project xmlns"http://maven.apa…

safari 調試iPhone web頁面

safari設置-打開Safari偏好者設置&#xff0c;選中“高級菜單”&#xff0c;在頁面最下方看到“在菜單中顯示開發菜單”的復選框&#xff0c;在復選框內打鉤&#xff0c;這樣設置完畢就能在Safari菜單中看到開發菜單了iPhone 設置-打開iPhone手機設置app 選擇Safari&#xff0c…

new函數

使用new函數是另一種創建變量的方式。創建一個未命名的T類型變量&#xff0c;初始化為T類型的零值&#xff0c;并返回其地址。例如&#xff1a; p : new(int)使用new函數創建變量和取其地址的普通局部變量沒有不同&#xff0c;只是不需要引入聲明時的一個名字&#xff0c;有語法…

軟件項目管理

目 錄 前言 2 如何做業務調研&#xff1f; 2.1 調研工作如何組織&#xff1f; 2.2 調研準備階段容易犯哪些錯誤&#xff1f; 2.3 調研準備階段容易犯哪些錯誤&#xff1f;) 2.4 調研準備階段容易犯哪些錯誤&#xff1f; 2.5 現場調研階段容易犯哪些錯誤&#xff1f; 2.…

Python 列表元組字典集合

列表&#xff08;list&#xff09; 有序性&#xff0c;可存儲任意類型的值通過偏移存取&#xff0c;支持索引來讀取元素&#xff0c;第一個索引為0 &#xff0c;倒數第一個索引為-1可變性 &#xff0c;支持切片、合并、刪除等操作可通過索引來向指定位置插入元素可通過pop()方法…

ios兼容問題

滑動卡頓&#xff1a; -webkit-overflow-scrolling:touch; 轉載于:https://www.cnblogs.com/smzd/p/7891722.html

postgresql 高可用 etcd + patroni 之二 patroni

os: centos 7.4 postgresql: 9.6.9 etcd: 3.2.18 patroni: 1.4.4 patroni etcd 是在一個postgrsql 開源大會上 亞信的一個哥們講解的高可用方案。 依然是基于 postgreql stream replication。 ip規劃 192.168.56.101 node1 master 192.168.56.102 node2 slave 192.168.56.103 …

vue對象偵測

http://blog.csdn.net/yihanzhi/article/details/74200618 數組&#xff1a;this.$set(this.arr,index,value) 轉載于:https://www.cnblogs.com/smzd/p/8390626.html

Laravel 5.4 migrate時報錯: Specified key was too long error

Laravel 5.4默認使用utf8mb4字符編碼&#xff0c;而不是之前的utf8編碼。因此運行php artisan migrate 會出現如下錯誤&#xff1a; [Illuminate\Database\QueryException] SQLSTATE[42000]: Syntax error or access violation: 1071 Specified key was too long; max key leng…

springboot工具類

ClassPathResource 在類路徑下讀取資源 public final String getPath() public boolean exists() public InputStream getInputStream() WebUtils 獲取web資源工具類 public static String getRealPath(ServletContext servletContext, String path) public static Object g…

MySQL中事物的詳解

1. 事物的定義及特性 事務是一組操作數據庫的SQL語句組成的工作單元&#xff0c;該工作單元中所有操作要么同時成功&#xff0c;要么同時失敗。事物有如下四個特性&#xff0c;ACID簡稱“酸性”。 1&#xff09;原子性&#xff1a;工作單元中所有的操作要么都成功&#xff0c;要…

記了老是忘記那就寫下來吧宏任務微任務

宏任務&#xff1a;script 定時器 微任務&#xff1a;promiss process.nexttick new Promise(function(resolve){console.log(3);//此為同步程序resolve();//同步 是否異步 由內部函數決定console.log(4); }).then(function(){ //。then 異步console.log(5); });async function…

SPRING自定義注入CONTROLLER變量

問題描述 在SpringMVC中默認可以注入Model&#xff0c;ModelAndView&#xff0c;RequestParam&#xff0c;PathVariable 等&#xff0c;那么這個是怎么實現的&#xff0c;以及怎么注入一個自定義的參數呢 HandlerMethodArgumentResolver 在SpringMVC中有一個接口HandlerMethod…

進程,線程

import os, timeif __name__ __main__:print(the calling process id:%d % os.getpid())# 創建進程pid os.fork()if pid 0:# 子進程print(the child pid is %d % os.getpid())time.sleep(3)elif pid > 0:# 父進程os.wait() # 等待子進程終止print([%d]bye-bye % os.getpi…

livebos--iframe使用

新建一個方法。建一個參數&#xff0c;iframe控件&#xff0c;虛擬列。然后使用以下信息 <% livebos languagejavascript %>var url LB_ObjURI("Lb_lbOrganization",0,[],["NoTitle"]);var v {"edit" : "url ", "view"…

單行溢出 和多行溢出

/*單行溢出*/.one_txt_cut{overflow: hidden;white-space: nowrap;text-overflow: ellipsis;}.txt_cut{overflow : hidden;text-overflow: ellipsis;display: -webkit-box;-webkit-line-clamp: 2;-webkit-box-orient: vertical;}轉載于:https://www.cnblogs.com/smzd/p/8491583…

Spring方法注入 @Lookup注解使用

情景分析 在Spring的諸多應用場景中bean都是單例形式&#xff0c;當一個單利bean需要和一個非單利bean組合使用或者一個非單利bean和另一個非單利bean組合使用時&#xff0c;我們通常都是將依賴以屬性的方式放到bean中來引用&#xff0c;然后以Autowired來標記需要注入的屬性。…

Jupyter配置步驟

Jupyter是基于瀏覽器的可交互式開發工具&#xff0c;在數據科學界非常受歡迎&#xff0c;它功能齊全&#xff0c;使用方便&#xff0c;是一款數據分析和建模挖掘的利器。 本文簡介Jupyter的配置和使用過程 一、修改添加國內鏡像 通常我會先安裝Anaconda&#xff0c;再安裝Jupyt…