codeforces 111B Petya and Divisors

题目:

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.

就是给你n组数,每一组数由xi,yi组成,对于每一组数,求出在xi的因子中有多少个因子不是前y组数xj的因子;

分析:

更新每一个出现过的因子的最近的位置;

代码:

#include<iostream>
#include<algorithm>
#include<cmath>
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
#include<queue>
#include<deque>
#define MOD 1000000000;
//#define DEBUG  //todo
using namespace std;    int nn;
int ans;
int n,x,y,f[110000],cnt=0;

void ini()
{
    cnt=0;
    cin>>n;
}
void work()
{
    int temp;
    for(int i=1;i<=n;i++){
        cin>>x>>y;
        temp=sqrt(x);
        cnt=0;
        for(int j=1;j<=temp;j++){
            if(x%j==0){
                if(i-f[j]>y){
                    cnt++;
                }
                if(i-f[x/j]>y && j!=x/j){
                    cnt++;
                }
                f[j]=i; f[x/j]=i;
            }
        }
        cout<<cnt<<endl;
    }
}
int main()
{
    ini();
    work();
#ifdef DEBUG
    cin>>nn;
#endif
    return 0;
}

 

posted on 2013-12-05 22:00  uestc小田  阅读(244)  评论(0编辑  收藏  举报

导航