[math] Codeforces 597A Divisibility
题目:http://codeforces.com/problemset/problem/597/A
1 second
256 megabytes
standard input
standard output
Find the number of k-divisible numbers on the segment [a, b]. In other words you need to find the number of such integer values x that a ≤ x ≤ b and x is divisible by k.
The only line contains three space-separated integers k, a and b(1 ≤ k ≤ 1018; - 1018 ≤ a ≤ b ≤ 1018).
Print the required number.
1 1 10
10
2 -4 4
5
题意:
给一个区间[a,b],问这个区间内有多少个数是k(k>0)的倍数
思路:
首先可以注意到0必定是k的倍数
若用x/k,则得到(0,x](x>0)或[x,0)(x<0)这个区间内有多少个k的倍数,这相当于一个前缀和(注意这里)
所以问[a,b]这个区间有多少个k的倍数时,若a>0且b>0,则ans=b/k-(a-1)/k (a-1是因为a可能是k的倍数,而b/k计算过一遍了,所以从a-1开始算,后面b+1同理),若a<0且b>0,则ans=(b+1)/k-a/k;,否则如果a<=0且b>=0则必定包含了0,所以ans=b/k-a/k+1(这里+1是为了加上0)
#include<bits/stdc++.h>
using namespace std;
int main(){
long long k,a,b,ans;
while(cin>>k>>a>>b){
if(b<)
ans=(b+)/k-a/k;
else if(a>)
ans=b/k-(a-)/k;
else
ans=b/k-a/k+;
printf("%lld\n",ans);
}
}
/**
给一个区间[a,b],问这个区间内有多少个数是k(k>0)的倍数
首先可以注意到0必定是k的倍数
若用x/k,则得到(0,x](x>0)或[x,0)(x<0)这个区间内有多少个k的倍数,这相当于一个前缀和(注意这里)
所以问[a,b]这个区间有多少个k的倍数时,若a>0且b>0,则ans=b/k-(a-1)/k (a-1是因为a可能是k的倍数,而b/k计算过一遍了,所以从a-1开始算,后面b+1同理),若a<0且b>0,则ans=(b+1)/k-a/k;,否则如果a<=0且b>=0则必定包含了0,所以ans=b/k-a/k+1(+1是为了加上0) 下面是测试时的一些样例
1 1 10
2 -4 4
1 -1000000000000000000 1000000000000000000
1000000000000000000 -1000000000000000000 1000000000000000000
4 -2 1
4 -3 4
4 0 1
4 -8 -3
4 -8 -4
4 -8 -5
4 5 8
4 7 8
5 1 10
**/
[math] Codeforces 597A Divisibility的更多相关文章
- CodeForces 597A Divisibility
水题. #include<iostream> #include<cstring> #include<cmath> #include<queue> #in ...
- Codeforces 550C —— Divisibility by Eight——————【枚举 || dp】
Divisibility by Eight time limit per test 2 seconds memory limit per test 256 megabytes input stand ...
- codeforces 630J Divisibility
J. Divisibility time limit per test 0.5 seconds memory limit per test 64 megabytes input standard in ...
- Codeforces 922F Divisibility 构造
Divisibility 我们考虑删数字 首先我们可以发现有一类数很特殊就是大于 n / 2的素数, 因为这些素数的贡献只有1, 并且在n大的时候, 这些素数的个数不是很少, 我们可以最后用这些数去调 ...
- Codeforces 922F Divisibility (构造 + 数论)
题目链接 Divisibility 题意 给定$n$和$k$,构造一个集合$\left\{1, 2, 3, ..., n \right\}$的子集,使得在这个集合中恰好有$k$对正整数$(x, y) ...
- Codeforces 988E. Divisibility by 25
解题思路: 只有尾数为25,50,75,00的数才可能是25的倍数. 对字符串做4次处理,以25为例. a. 将字符串中的最后一个5移到最后一位.计算交换次数.(如果没有找到5,则不可能凑出25,考虑 ...
- HPU周赛题目解析
A - Wilbur and Swimming Pool Time Limit:1000MS Memory Limit:262144KB 64bit IO Format:%I64d & ...
- codeforcess水题100道
之所以在codeforces上找这100道水题的原因是为了巩固我对最近学的编程语言的掌握程度. 找的方式在codeforces上的PROBLEMSET中过的题最多的那些题里面出现的最前面的10个题型, ...
- Codeforces--597A--Divisibility(数学)
DivisibilityCrawling in process... Crawling failed Time Limit:1000MS Memory Limit:262144KB ...
随机推荐
- CoreGraphic
public func UIGraphicsBeginImageContextWithOptions( size: CGSize, opaque: Bool, _ scale: CGFloat) si ...
- CSS——NO.8(代码简写)
*/ * Copyright (c) 2016,烟台大学计算机与控制工程学院 * All rights reserved. * 文件名:text.cpp * 作者:常轩 * 微信公众号:Worldhe ...
- Mac 常见命令行
1. unrar解压rar文件 1.1 安装命令:brew install unrar 1.2 解压文件:unrar x test.rar 2. 创建文件夹:mkdir 文件夹名 3. 删除文件夹: ...
- YiGo环境搭建
软件环境 操作系统:Windows 2000+,Mac OS,AIX,RedHat linux,HP-UX等 JDK/JRE:Oracle JDK/JRE 1.8+,IBM J9 VM 1.8+,Op ...
- Spring Boot 2.x基础教程:使用MyBatis访问MySQL
之前我们已经介绍了两种在Spring Boot中访问关系型数据库的方式: 使用spring-boot-starter-jdbc 使用spring-boot-starter-data-jpa 虽然Spr ...
- PHP实现 3des加密解密
<?php /** * 3des加密 */ class Encrypt{ public function pkcs5_pad($text, $blocksize) { $pad = $block ...
- ubuntu下pip的安装,更新及卸载
在Ubuntu下,不小心uninstall pip了,然后呢,作为小白的我,还是有些着急的,用了一些方法不好使,最后找到了这个方法: 1.安装pip3: sudo apt-get install py ...
- safari坑之 回弹
博客地址: https://www.seyana.life/post/20 今天在使用safari浏览博客的时候, 发现在拉至顶部并产生回弹之后,头部导航隐藏了, 除非在上拉的时候,刚好达到顶部而不超 ...
- Hadoop fs 基础命令
操作hdfs的基本命令 在hdfs中,路径需要用绝对路径 1. 查看根目录 hadoop fs -ls / 2. 递归查看所有文件和文件夹 -lsr等同于-ls -R hadoop fs -lsr / ...
- ajax参数contentType与数据提交方式
使用bootstrapTable时,服务器端无法获取参数(flask,request.form.get方法),检查发现是因为ajax提交的时候,方式是payload,要想用form提交,需要设置con ...