F. Geometrical Progression
http://codeforces.com/problemset/problem/758/F
4 seconds
256 megabytes
standard input
standard output
For given n, l and r find the number of distinct geometrical progression, each of which contains n distinct integers not less than l and not greater than r. In other words, for each progression the following must hold: l ≤ ai ≤ r and ai ≠ aj , where a1, a2, ..., an is the geometrical progression, 1 ≤ i, j ≤ n and i ≠ j.
Geometrical progression is a sequence of numbers a1, a2, ..., an where each term after first is found by multiplying the previous one by a fixed non-zero number d called the common ratio. Note that in our task d may be non-integer. For example in progression 4, 6, 9, common ratio is
.
Two progressions a1, a2, ..., an and b1, b2, ..., bn are considered different, if there is such i (1 ≤ i ≤ n) that ai ≠ bi.
The first and the only line cotains three integers n, l and r (1 ≤ n ≤ 107, 1 ≤ l ≤ r ≤ 107).
Print the integer K — is the answer to the problem.
1 1 10
10
2 6 9
12
3 1 10
8
3 3 10
2
These are possible progressions for the first test of examples:
- 1;
- 2;
- 3;
- 4;
- 5;
- 6;
- 7;
- 8;
- 9;
- 10.
These are possible progressions for the second test of examples:
- 6, 7;
- 6, 8;
- 6, 9;
- 7, 6;
- 7, 8;
- 7, 9;
- 8, 6;
- 8, 7;
- 8, 9;
- 9, 6;
- 9, 7;
- 9, 8.
These are possible progressions for the third test of examples:
- 1, 2, 4;
- 1, 3, 9;
- 2, 4, 8;
- 4, 2, 1;
- 4, 6, 9;
- 8, 4, 2;
- 9, 3, 1;
- 9, 6, 4.
- 题意:在[l,r]区间中找项数为n的等比数列的不同个数;
思路:首先讨论下 n = 1,2的情况,然后,找公比,应为r<=1e7,那么n不会超过23,当d为整数的时候那么d^(n-1)<=r;
从而选出d,然后当d为分数的时候,假设(a/b);那么我们只要枚举(a,b互质)的数,因为若不互质可化成互质,那么(a/b)^(n-1),假设第一个数为x,那么x要是b^(n-1)的
倍数,设x = k*(b)^(n-1);那么a,b必定是刚选出来的那些数中的数,不可能比选出来的数大,因为x = k*(b)^(n-1)<=r&&k*(b)^(n-1)<=n&&(k>=1);
那么枚举d来解k的范围;(l-1)< (a)^(n-1)*k&&k*(b^(n-1))<=r;
1 #include<bits/stdc++.h>
2 using namespace std;
3 typedef long long LL;
4 LL gcd(LL n,LL m){if(m == 0)return n;return gcd(m,n%m);}
5 LL ans[4000];
6 int main(void)
7 {
8 LL n,l,r;LL ask = 0;
9 scanf("%lld %lld %lld",&n,&l,&r);
10 if(n == 1)
11 printf("%lld\n",r-l+1);
12 else if(n == 2)
13 printf("%lld\n",(LL)(r-l+1)*(LL)(r-l));
14 else if(n > 25)
15 printf("0\n");
16 else
17 { int cn = 0;
18 for(int i = 1;i <= 4000;i++)
19 { LL sum = 1;int j;
20 for( j = 0;j < n-1;j++)
21 {
22
23 sum*=(LL)i; if(sum > r)break;
24 }
25 if(j == n-1)
26 {
27 ans[++cn] = sum;
28 }
29 else break;
30 }
31 for(int i = 1;i <= cn;i++)
32 {
33 for(int j = i+1;j <= cn;j++)
34 {
35 if(gcd(i,j) == 1)
36 {
37 LL p = r/ans[j];
38 LL q = (l-1)/ans[i];
39 if(p >= q)
40 ask+=p-q;
41 }
42 }
43 }
44 printf("%lld\n",ask*(LL)2);
45 }
46 return 0;
47 }
F. Geometrical Progression的更多相关文章
- Codeforces Round #392 (Div. 2) F. Geometrical Progression
原题地址:http://codeforces.com/contest/758/problem/F F. Geometrical Progression time limit per test 4 se ...
- Codeforces 758F Geometrical Progression
Geometrical Progression n == 1的时候答案为区间长度, n == 2的时候每两个数字都可能成为答案, 我们只需要考虑 n == 3的情况, 我们可以枚举公差, 其分子分母都 ...
- 分析递归式 Solving Recurrences------GeeksforGeeks 翻译
在上一章中我们讨论了如何分析循环语句.在现实中,有很多算法是递归的,当我们分析这些算法的时候我们要找到他们的的递归关系.例如归并排序,为了排序一个数组,我们把它平均分为两份然后再重复平分的步骤.最后我 ...
- Mysql_以案例为基准之查询
查询数据操作
- Codeforces Round #Pi (Div. 2) C. Geometric Progression map
C. Geometric Progression Time Limit: 2 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/5 ...
- hdu 5278 Geometric Progression 高精度
Geometric Progression Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://bestcoder.hdu.edu.cn/contes ...
- Codeforces 1114 - A/B/C/D/E/F - (Undone)
链接:http://codeforces.com/contest/1114 A - Got Any Grapes? 题意:甲乙丙三个人吃葡萄,总共有三种葡萄:绿葡萄.紫葡萄和黑葡萄,甲乙丙三个人至少要 ...
- POJ3495 Bitwise XOR of Arithmetic Progression
Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 772 Accepted: 175 Description Write ...
- 在 C# 里使用 F# 的 option 变量
在使用 C# 与 F# 混合编程的时候(通常是使用 C# 实现 GUI,F#负责数据处理),经常会遇到要判断一个 option 是 None 还是 Some.虽然 Option module 里有 i ...
随机推荐
- excel-大于0的数值标记红色且标记红色上箭头,小于0的数值标记绿色且标记绿色下箭头,等于0的数值标记黄色且标记右箭头
0.数值是常规的数值: [蓝色]"↑"0;[红色]"↓"0;[黄色]"→"0 [蓝色]"↑"0.0;[红色]" ...
- 1.TwoSum-Leetcode
#include<iostream> #include<algorithm> #include<map> using namespace std; class So ...
- 浅谈Facebook的服务器架构
导读:毫无疑问,作为全球最领先的社交网络,Facebook的高性能集群系统承担了海量数据的处理,它的服务器架构一直为业界众人所关注.CSDN博主yanghehong在他自己最新的一篇博客< Fa ...
- MybatisPlus入门程序
参考资料:MybatisPlus官网 环境搭建 创建数据库 CREATE DATABASE `mybatisplus` USE `mybatisplus` CREATE TABLE `user ...
- C#表格,表格信息、GridView使用。
page: <%@ Control Language="C#" AutoEventWireup="true" CodeFile="test1.a ...
- 零基础学习java------39---------json格式交互,Restful(不懂),静态资源映射,SSM整合(ssm整合思想,application.xml文件详解(声明式事务管理),)
一. json格式交互(知道) 1 . 回顾ajax基本语法 $.ajax({ url:"", // 请求的后台路径 data:{"":"" ...
- 【leetcode】222. Count Complete Tree Nodes(完全二叉树)
Given the root of a complete binary tree, return the number of the nodes in the tree. According to W ...
- webservice--cxf和spring结合
服务端: 实体: package entity; import java.util.Date; /*** 实体 */ public class Pojo { //温度 private String d ...
- D3基础入门四-事件处理
6.5.0版 .on("mouseover", function(e,d) e: {"isTrusted":true} 第二个参考才是数据,但这在不同的环境可能 ...
- Spring Cloud集成RabbitMQ的使用
同步 or 异步 前言:我们现在有一个用微服务架构模式开发的系统,系统里有一个商品服务和订单服务,且它们都是同步通信的. 目前我们商品服务和订单服务之间的通信方式是同步的,当业务扩大之后,如果还继续使 ...