数学分析 + 容斥原理 - URAL 1907 Coffee and Buns
Coffee and Buns
Problem's Link: http://www.bnuoj.com/v3/contest_show.php?cid=6415#problem/H
Mean:
给定两个数a和n,求[1,n]中有多少个x满足:gcd(4*(a+x),a^2+x^2)>1。
analyse:
gcd(4(a+x),a^2+x^2)>1 ----> gcd(a+x,(a+x)^2-2ax)>1 ----> gcd(a+x,2ax)>1 (gcd(a,b)=gcd(b,a%b)
假设a是偶数,那么gcd(a+x,2ax)>1 ----> gcd(a+x,ax)
设最大公约数为g,则g|ax,g|a+x
如果g|a,那么g|x,如果g|x,那么g|a,所以只要x是a任意一个因子的倍数就合法
假设a是奇数,那么有2种情况
1.x是奇数
2.x是a任意一个因子的倍数
所以要求1~maxn中与a,gcd > 1 的个数,就是求1~maxn与某一个num不互素的个数,要用到容斥原理。
Time complexity: O(N)
Source code:
;;
;) ;
;) p);
) ))));
}
/*
*/
数学分析 + 容斥原理 - URAL 1907 Coffee and Buns的更多相关文章
- 容斥原理--计算并集的元素个数 URAL 1091
在计数时,必须注意没有重复,没有遗漏.为了使重叠部分不被重复计算,人们研究出一种新的计数方法,这种方法的基本思想是:先不考虑重叠的情况,把包含于某内容中的所有对象的数目先计算出来,然后再把计数时重复计 ...
- ural 1932 The Secret of Identifier (容斥原理)
标题效果: 计算到n字符串. 精确到只是有一个不同的字符,两个不同的字符.三个不同的字符,四对不同的字符. IDEAS: 枚举状态. dp[i] [j] ...当前串取出 i 状态下的全部字符转化成十 ...
- ural 1091. Tmutarakan Exams(容斥原理)
1091. Tmutarakan Exams Time limit: 1.0 secondMemory limit: 64 MB University of New Tmutarakan trains ...
- URAL - 1091 Tmutarakan Exams (简单容斥原理)
题意:K个不同数组成的集合,每个数都不超过S且它们的gcd>1.求这样的数的个数 分析:从2开始枚举gcd,但这样会发生重复.譬如,枚举gcd=2的集合个数和gcd=3的集合个数,枚举6的时候就 ...
- 2014 Super Training #3 H Tmutarakan Exams --容斥原理
原题: URAL 1091 http://acm.timus.ru/problem.aspx?space=1&num=1091 题意:要求找出K个不同的数字使他们有一个大于1的公约数,且所有 ...
- ural 1091. Tmutarakan Exams 和 codeforces 295 B. Greg and Graph
ural 1091 题目链接:http://acm.timus.ru/problem.aspx?space=1&num=1091 题意是从1到n的集合里选出k个数,使得这些数满足gcd大于1 ...
- URAL 1707. Hypnotoad's Secret(树阵)
URAL 1707. Hypnotoad's Secret space=1&num=1707" target="_blank" style="" ...
- hdu 1695 容斥原理或莫比乌斯反演
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- Ural 1091 Tmutarakan Exams
Tmutarakan Exams Time Limit: 1000ms Memory Limit: 16384KB This problem will be judged on Ural. Origi ...
随机推荐
- HDoj-2095-与众不同
Problem Description In the new year party, everybody will get a "special present".Now it's ...
- Unity骨骼动力学应用
原地址:http://blog.csdn.net/libeifs/article/details/7169794 开发环境 Window7 Unity3D 3.4.1 MB525defy Andro ...
- linux&shell
Linux经常使用命令 登录时显示信息放在/etc/motd和/etc/profile.d/xxx.sh motd放置字符串 profile.d下放置脚本文件 echo echo -e 处理特殊字符. ...
- JSTL核心标签
JSTL 核心标签库标签共有13个,功能上分为4类: 1.表达式控制标签:out.set.remove.catch 2.流程控制标签:if.choose.when.otherwise 3.循环标签:f ...
- MySQL数据库和实例简介
MySQL 单进程多线程,也就是说MySQL实例在系统上表现就是一个进程 该架构类似于SQL Server和Windows版本的Oracle MySQL数据库是由一些列物理文件组成.类似于frm.MY ...
- taro 填坑之路(一)taro 项目回顾
(1)像素写法 PX -- 大写,否则会自动成rem (2)拿取列表第一条数据 let { activity:[firstItem] } = this.state; (3)使用props 需要设置默认 ...
- Reusing dialogs with a dialog pool--一个sql server service broker例子
一个sql server service broker例子 ----------------------------------- USE master GO -------------------- ...
- Android SDK 更新和下载慢怎么办?
博客搬家:因为各种原因,我如今的博客将首发于blog.mojijs.com, 能够百度搜索 "姜哥的墨迹技术博客" , 或者 点击这里 本文地址 http://blog.mojij ...
- excel 获取中文拼音首字母
excel 获取中文拼音首字母 CreateTime--2018年5月31日08:50:42 Author:Marydon 1.情景展示 想要获取姓名的拼音首字母 2.实现方式 通过使用excel ...
- 为啥Android手机总会越用越慢?
转自:http://www.androidchina.net/818.html 根据第三方的调研数据显示,有77%的Android手机用户承认自己曾遭遇过手机变慢的影响,百度搜索“Android+卡慢 ...