CodeForces 111B - Petya and Divisors
2015-11-14 21:03
696 查看
Description
Little Petya loves looking for numbers' divisors. One day Petya came across the following problem:
You are given n queries in the form "xiyi".
For each query Petya should count how many divisors of number xi divide none of the numbers xi - yi, xi - yi + 1, ..., xi - 1.
Help him.
Input
The first line contains an integer n (1 ≤ n ≤ 105). Each of the following n lines
contain two space-separated integers xi and yi (1 ≤ xi ≤ 105, 0 ≤ yi ≤ i - 1,
where i is the query's ordinal number; the numeration starts with 1).
If yi = 0 for the query, then the answer to the query will be the number of divisors of the number xi.
In this case you do not need to take the previous numbers x into consideration.
Output
For each query print the answer on a single line: the number of positive integers k such that
![](http://7xjob4.com1.z0.glb.clouddn.com/b00bde834f35e41776d2479679026db9)
Sample Input
Input
Output
没有了固化的算法来解决问题就不会了。
算法思路:
定义:divisor[i] :以i为因子的最后一个数的下标;
①对每个输入的数找出它的所有因子,比较该因子是否在给定范围内也是其它数的因子;
②若不是,则累加计数;
③更新当前数的因子的位置为当前下
4000
标。
#include<iostream>
#include<cstring>
using namespace std;
const int N = 100000 + 5;
int divisor
;
int main()
{
int n;
cin>>n;
int x, y;
memset(divisor, -1, sizeof(divisor));
for(int i = 0; i < n; i++)
{
cin>>x>>y;
int res = 0;
for(int j = 1; j * j <= x; j++)
if(x % j == 0)
{
if(i - y > divisor[j])
res++;
if(j * j != x && i - y > divisor[x / j])
res++;
divisor[j] = divisor[x / j] = i;
}
cout<<res<<endl;
}
}
Little Petya loves looking for numbers' divisors. One day Petya came across the following problem:
You are given n queries in the form "xiyi".
For each query Petya should count how many divisors of number xi divide none of the numbers xi - yi, xi - yi + 1, ..., xi - 1.
Help him.
Input
The first line contains an integer n (1 ≤ n ≤ 105). Each of the following n lines
contain two space-separated integers xi and yi (1 ≤ xi ≤ 105, 0 ≤ yi ≤ i - 1,
where i is the query's ordinal number; the numeration starts with 1).
If yi = 0 for the query, then the answer to the query will be the number of divisors of the number xi.
In this case you do not need to take the previous numbers x into consideration.
Output
For each query print the answer on a single line: the number of positive integers k such that
Sample Input
Input
6 4 0 3 1 5 2 6 2 18 4 10000 3
Output
3 1 1 2 2 22
没有了固化的算法来解决问题就不会了。
算法思路:
定义:divisor[i] :以i为因子的最后一个数的下标;
①对每个输入的数找出它的所有因子,比较该因子是否在给定范围内也是其它数的因子;
②若不是,则累加计数;
③更新当前数的因子的位置为当前下
4000
标。
#include<iostream>
#include<cstring>
using namespace std;
const int N = 100000 + 5;
int divisor
;
int main()
{
int n;
cin>>n;
int x, y;
memset(divisor, -1, sizeof(divisor));
for(int i = 0; i < n; i++)
{
cin>>x>>y;
int res = 0;
for(int j = 1; j * j <= x; j++)
if(x % j == 0)
{
if(i - y > divisor[j])
res++;
if(j * j != x && i - y > divisor[x / j])
res++;
divisor[j] = divisor[x / j] = i;
}
cout<<res<<endl;
}
}
相关文章推荐
- Algorithmic Thinking Week1
- Thinking in Java阅读笔记(一)
- Thinking in Java阅读笔记(二)
- Thinking in Java阅读笔记(二)
- thinking in java 笔记之初始化和清除
- Introduction to Objects
- Operators
- Controlling Execution
- Interfaces
- Type Information
- Thinking——写在前面的话
- 复活JAVA1 - Thinking In Java (1)
- 什么是编程模型
- CSDN 第一篇博文
- 遍身罗绮者 不是养蚕人
- 你所知道的并不是你真的知道 —— 程序语言的精髓
- 对 Thinking in java 4th Edition I/O DirList.java的疑问
- 总裁决策大数据:新思维&技术
- 设计思维的要素:新鲜事物(Unknowns)
- 设计思维的要素:优化愿景(Vision)