codeforces 495B. Modular Equations 解题报告
题目链接:http://codeforces.com/problemset/problem/495/B
题目意思:给出两个非负整数a,b,求出符合这个等式
的所有x,并输出 x 的数量,如果 x 有无限多个,那么输出 infinity。
想了半个多小时......有个地方想遗漏了。
a mod x == b,等价于 a = k*x + b。设 mul = a - b,那么 k*x = mul,然后就不断枚举 mul 的因子,即 kx = mul。由于 mod 出来的结果为 b,那么 k 或 x 至少有一个比 b 大。
/***************************************
我没有想出来的地方是,求出来两个因子 i、mul/i 只保留大的那个,然后跟 b 比较,大于的话统计个数(累计一次)。但是没有想到 i 和 mul/i 可能两个都比 b 大,那么此时应该是累计两次!
****************************************/
还有一些需要注意的地方,因为 a,b最大可能达到 1e9,枚举因子的时候,平方根以内枚举即可。最后就是输出 infinity 的情况,就是 a = b,根据样例看出来的。此时 kx = 0 。
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std; int main()
{
#ifndef ONLINE_JUDGE
freopen("in.txt", "r", stdin);
#endif // ONLINE_JUDGE
int a, b;
while (scanf("%d%d", &a, &b) != EOF)
{
int ans = ;
int mul = a - b;
if (mul == )
printf("infinity\n");
else
{
bool flag = false;
for (int i = ; i*i <= mul; i++)
{
if (mul % i == )
{
if (i > b)
ans++;
if (mul/i > b && mul/i != i) // 注意
ans++;
}
}
printf("%d\n", ans);
}
}
return ;
}
codeforces 495B. Modular Equations 解题报告的更多相关文章
- Codeforces Round 665 赛后解题报告(暂A-D)
Codeforces Round 665 赛后解题报告 A. Distance and Axis 我们设 \(B\) 点 坐标为 \(x(x\leq n)\).由题意我们知道 \[\mid(n-x)- ...
- Codeforces Round 662 赛后解题报告(A-E2)
Codeforces Round 662 赛后解题报告 梦幻开局到1400+的悲惨故事 A. Rainbow Dash, Fluttershy and Chess Coloring 这个题很简单,我们 ...
- Codeforces Round #277.5 解题报告
又熬夜刷了cf,今天比正常多一题.比赛还没完但我知道F过不了了,一个半小时贡献给F还是没过--应该也没人Hack.写写解题报告吧= =. 解题报告例如以下: A题:选择排序直接搞,由于不要求最优交换次 ...
- codeforces B. Simple Molecules 解题报告
题目链接:http://codeforces.com/problemset/problem/344/B 题目意思:这句话是解题的关键: The number of bonds of an atom i ...
- 【LeetCode】990. Satisfiability of Equality Equations 解题报告(C++ & python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 解题方法 DFS 并查集 日期 题目地址:https://le ...
- codeforces 591A. Wizards' Duel 解题报告
题目链接:http://codeforces.com/problemset/problem/591/A 题目意思:其实看下面这幅图就知道题意了,就是Harry 和 He-Who-Must-Not-Be ...
- codeforces 582A. GCD Table 解题报告
题目链接:http://codeforces.com/problemset/problem/582/A 网上很多题解,就不说了,直接贴代码= = 官方题解: http://codeforces.com ...
- codeforces 581C. Developing Skills 解题报告
题目链接:http://codeforces.com/problemset/problem/581/C 题目意思:给出 n 个数:a1, a2, ..., an (0 ≤ ai ≤ 100).给出值 ...
- codeforces 577B. Modulo Sum 解题报告
题目链接:http://codeforces.com/problemset/problem/577/B 题目意思:就是给出 n 个数(a1, a2, ..., an) 和 m,问能不能从这 n 个数中 ...
随机推荐
- matlab 聚类
目前已知matlab的聚类方法有三种: 一.利用 clusterdata函数对样本数据进行一次聚类,其缺点为可供用户选择的面较窄,不能更改距离的计算方法: 二.层次聚类,该方法较为灵活,需要进行细节了 ...
- 正则中的lastIndex属性
首先大家看下下面的代码 var reg = /\d/; console.log( reg.test("1") ); console.log( reg.test("1&qu ...
- NUGet的诞生与使用
本文引用地址:http://msdn.microsoft.com/zh-cn/magazine/hh547106.aspx NuGet 使用 NuGet 管理项目库 Phil Haack 无论多么努力 ...
- mybatis 多个dao重名,根据namespace解析
在mybatis通过执行sql语句的方式是,用getSqlSession().xxx(param,..)方法来调用, 其中第一个参数就是dao mapper.xml文件的命名空间.id package ...
- 第四天 rxcocoa
HackerNewsReaderDemo HackerNewsAPI.sharedApi.newStories() .observeOn(ConcurrentDispatchQueueSchedule ...
- ubutu之jdk安装
1.jdk下载 http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html 2.解压jdk- ...
- jquery 表单清空
$(':input','#myform') .not(':button, :submit, :reset, :hidden') .val('') .removeAttr('checked') .rem ...
- linux 输出重定向一份到本地文件,屏幕继续输出
ls -al 2>&1 | tee xLog
- iOS开发——多线程篇——多线程介绍
一.进程和线程1.什么是进程进程是指在系统中正在运行的一个应用程序每个进程之间是独立的,每个进程均运行在其专用且受保护的内存空间内 比如同时打开迅雷.Xcode,系统就会分别启动2个进程 通过“活动监 ...
- Interleaving String leetcode
Given s1, s2, s3, find whether s3 is formed by the interleaving of s1 and s2. For example,Given:s1 = ...