数学分析 + 容斥原理 - 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 ...
随机推荐
- ant design pro (十二)advanced UI 测试
一.概述 原文地址:https://pro.ant.design/docs/ui-test-cn UI 测试是项目研发流程中的重要一环,有效的测试用例可以梳理业务需求,保证研发的质量和进度,让工程师可 ...
- Tags Used In OpenERP 7.0
In OpenERP 7.0. the form view of each object has been redesigned so that the object the user is work ...
- oracle 存储过程 示例
oracle 存储过程 示例 CreationTime--2018年9月4日09点49分 Author:Marydon 1.情景展示 对VIRTUAL_QRCODELOG表的静态二维码,动态二维码 ...
- Column count of mysql.user is wrong. Expected 43, found 42. Created with MySQL 50518, now running 50641. Please use mysql_upgrade to fix this error.
出现问题: Column count of mysql.user is wrong. Expected 43, found 42. Created with MySQL 50518, now runn ...
- Percona Toolkit工具集介绍
部署mysql工具是一个非常重要的部分,所以工具的可靠性和很好的设计非常重要.percona toolkit是一个有30多个mysql工具的工具箱.兼容mysql,percona server,mar ...
- 多对多关系数据库表 java描述
多对多关系 需要建立一张新表存放它们的对应数据 sql语句 create table teacher( id int primary key, name ), ,) ); create table s ...
- 【LeetCode】100. Same Tree (2 solutions)
Same Tree Given two binary trees, write a function to check if they are equal or not. Two binary tre ...
- 微软的SQLHelper类(含完整中文注释)
using System; using System.Data; using System.Xml; using System.Data.SqlClient; using System.Collect ...
- freemarker,*.ftl文件在eclipse中正确的打开方式
解决方式一:下载eclipse相关的freemarker插件 解决方式二:本文重点介绍的,原生解决方式,方法特点无需下载插件,和eclipse编辑html和jsp文件一模一样 步骤: 1:window ...
- JAVA中的抽象类与接口
抽象类 abstract class 包含抽象方法的类,叫抽象类.而抽象的概念就是抽象出共同属性:成员变量和方法.所以抽象类可以有private等多种权限的成员变量和非abstract的成员方法.当然 ...