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

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);
    }
}

 

posted @ 2015-11-20 17:18  qscqesze  阅读(460)  评论(0编辑  收藏  举报