B - Petya and Divisors(数论)

B - Petya and Divisors(数论)

题意:给定 x i , y i x_i,y_i 计算出 x i x_i 有多少个除数不在 x j , j [ i y i , i 1 ] x_j,j\in[i-y_i,i-1] 出现过。

思路: 用一个数组记录每个除数最后一次出现的位置即可。

AC代码:

#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<iostream>
using namespace std;
const int N=1e5+5;
typedef long long ll;
int p[N];//p[j]记录除数j最后一次出现的位置. 
int jg(int i,int y,int j){
	int f=1;
	if(p[j]>=i-y) //如果最后一次出现的位置在[i-y,i-1]之间 
		f=0;
	p[j]=i;//更新 
	if(!f) return 0;
	return 1;
}
int main(){
	int n;
	scanf("%d",&n);
	for(int i=1;i<=n;i++)
	{
		int x,y,ans=0;
		scanf("%d%d",&x,&y);
		for(int j=1;j*j<=x;j++)
		{
			if(x%j==0)
			{
				if(jg(i,y,j))
				ans++;
				if(j*j!=x) //避免加重 
				{
					if(jg(i,y,x/j))
						ans++;
				} 
			}
		}
		printf("%d\n",ans);
	} 
	return 0;
}
原创文章 201 获赞 165 访问量 1万+

猜你喜欢

转载自blog.csdn.net/weixin_45750972/article/details/105913736
今日推荐