题目:Petya and Divisors
传送门:

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

http://codeforces.com/problemset/problem/112/D

分析:

  很容易想到读入x[i]、y[i],寻找x[i]的因数,判断一下是不是x[i-y[i]]、x[i-y[i]+1]...x[i-1]的某个数因数;但这样会超时;考虑以下两个优化:(1)寻找x[i]因数时循环范围只需要从j∈[0,sqrt(x[i])],但循环体内同时判断两个因数j、x[i]/j(注意当j*j==x[i]的情况);(2)把子问题“判断一下因数j是不是x[i-y[i]]、x[i-y[i]+1]...x[i-1]的某个因数”转变为问题“因数j最后一次位置出现在不在[i-y[i],i-1]”求解,并更新因数j最后一次位置。

代码:

#include<cstdio>
int n,last[];
int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
scanf("%d",&n);
for(int i=,x,y,ans;i<=n;++i){
scanf("%d%d",&x,&y);
ans=;
for(int j=;j*j<=x;++j)
if(x%j==){
if(last[j]<i-y)++ans;last[j]=i;
if(j*j==x)continue;
if(last[x/j]<i-y)++ans;last[x/j]=i;
}
printf("%d\n",ans);
}
return ;
}

codeforces 111B/112D Petya and Divisors的更多相关文章

  1. 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 ...

  2. B. Petya and Divisors 解析(思維)

    Codeforce 111 B. Petya and Divisors 解析(思維) 今天我們來看看CF111B 題目連結 題目 略,請看原題 前言 看了別人的解答就豁然開朗 @copyright p ...

  3. CodeForces 111B - Petya and Divisors 统计..想法题

    找每个数的约数(暴力就够了...1~x^0.5)....看这约数的倍数最后是哪个数...若距离大于了y..统计++...然后将这个约数的最后倍数赋值为当前位置...好叼的想法题.... Program ...

  4. Educational Codeforces Round 12 F. Four Divisors 求小于x的素数个数(待解决)

    F. Four Divisors 题目连接: http://www.codeforces.com/contest/665/problem/F Description If an integer a i ...

  5. codeforces 111C/112E Petya and Spiders

    题目: Petya and Spiders传送门: http://codeforces.com/problemset/problem/111/C http://codeforces.com/probl ...

  6. codeforces 111A/112C Petya and Inequiations

    题目:Petya and Inequiations传送门: http://codeforces.com/problemset/problem/111/A http://codeforces.com/p ...

  7. 【Codeforces 111C】Petya and Spiders

    Codeforces 111 C 题意:给\(n\times m\)的网格,每个点上有一个蜘蛛,每个蜘蛛可以向上.下.左.右走一步或者不动,问最多能存在多少没有蜘蛛的点. 思路1: 首先因为\(n\) ...

  8. Codeforces 1082 G - Petya and Graph

    G - Petya and Graph 思路: 最大权闭合子图 对于每条边,如果它选了,那么它连的的两个点也要选 边权为正,点权为负,那么就是求最大权闭合子图 代码: #pragma GCC opti ...

  9. CF刷题-Codeforces Round #481-G. Petya's Exams

    题目链接:https://codeforces.com/contest/978/problem/G 题目大意:n天m门考试,每门考试给定三个条件,分别为:1.可以开始复习的日期.2.考试日期.3.必须 ...

随机推荐

  1. Python笔记(十八)_私有属性、实例属性、类属性

    私有属性 如果要让内部属性不被外部访问,可以把属性的名称前加上两个下划线__,就变成了一个私有属性,只有内部可以访问,外部不能直接访问或修改. 这样就确保了外部代码不能随意修改对象内部的状态,这样通过 ...

  2. ALDS1_1_3_D Areas on the Cross-Section Diagram 遇见了几个有意思的语法问题

    Your task is to simulate a flood damage. For a given cross-section diagram, reports areas of flooded ...

  3. Naive Bayes Algorithm And Laplace Smoothing

    朴素贝叶斯算法(Naive Bayes)适用于在Training Set中,输入X和输出Y都是离散型的情况.如果输入X为连续,输出Y为离散,我们考虑使用逻辑回归(Logistic Regression ...

  4. [LeetCode] 4. Median of Two Sorted Arrays(想法题/求第k小的数)

    传送门 Description There are two sorted arrays nums1 and nums2 of size m and n respectively. Find the m ...

  5. css负边距布局

    三行三列的布局 代码结构 <div class="container"> <ul> <li>1</li> <li>2&l ...

  6. 如何配置JedisPool的参数

    转自:http://blog.csdn.net/huahuagongzi99999/article/details/13631579 如何配置Pool的参数 JedisPool的配置参数很大程度上依赖 ...

  7. [Linux] 017 网络命令与挂载命令

    1. 网络命令:write 命令名称:write 命令所在路径:/usr/bin/write 执行权限:所有用户 语法:write [用户名] 功能描述:给用户发信息,以 Ctrl-d 保存结束 范例 ...

  8. SQL server 查看什么语句在使用临时表

    SQL server 查询那些语句在使用临时表 最近在日常的性能测试工作中发现,数据库端的IO读写比较大,有规律的2-8M的波动,数据库的版本为 SQL server 2008 sp3. 这些IO操作 ...

  9. MySQL-第五篇视图

    1.视图看上去像是表,但它又不是,它并不能存储数据.视图只是一个或者多个表的逻辑显示.使用视图的好处: 1>可以限制对数据的访问 2>可以使复杂的查询变得简单 3>提供了数据的独立性 ...

  10. Emacs中的前进后退jump-tree

    Emacs中的前进后退jump-tree */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #8394 ...