Codeforces Beta Round #85 (Div. 1 Only) B. Petya and Divisors 暴力

本文详细解析了B.Petya和除数问题的解决策略,提供了输入输出样例及代码实现,帮助读者理解如何计算一个数的除数在给定范围内未出现的情况。

B. Petya and Divisors

Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://codeforces.com/contest/111/problem/B

Description

Little Petya loves looking for numbers' divisors. One day Petya came across the following problem:

You are given n queries in the form "xi yi". 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

6
4 0
3 1
5 2
6 2
18 4
10000 3

Sample Output

3
1
1
2
2
22

HINT

 

题意

给你n次询问,每次给你一个x,y

然后让你输出,x这个数中的因子有多少个并没有在这个数的前y个数中出现过

题解:

对于每次询问,我们直接暴力统计因子个数

对于每一个因子,我们都记录一下这个因子最后出现在在什么时候,然后我们再判断是否ans++就好了

代码

#include<iostream>
#include<stdio.h>
#include<map>
using namespace std;

map<int,int> H;
int check(int x,int y,int z)
{

    int flag = 1;
    if(H[z]>=x-y)
        flag = 0;
    H[z]=x;
    //cout<<x<<" "<<y<<" "<<z<<" "<<flag<<endl;
    if(flag==0)
        return 0;
    return 1;
}
int main()
{
    int n;scanf("%d",&n);
    for(int i=1;i<=n;i++)
    {
        int x,y;scanf("%d%d",&x,&y);
        int ans = 0;
        for(int j=1;j*j<=x;j++)
        {
            if(x%j==0)
            {
                if(check(i,y,j))
                    ans++;
                if(x/j!=j)
                {
                    if(check(i,y,x/j))
                        ans++;
                }
            }
        }
        printf("%d\n",ans);
    }
}

 

转载于:https://www.cnblogs.com/qscqesze/p/4981477.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值