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 ...
随机推荐
- jmeter+ant输出测试报告
jmeter自己本身可以输出html测试报告的,不过这种自带的测试报告特别简陋,如下图所示,一般我们是不看这种的. 我们可以使用ant来输出更高效.更直观的测试报告. 首先下载安装ant, 我用的是a ...
- 技术预演blog
canal整合springboot实现mysql数据实时同步到redis spring+mysql集成canal springboot整合canal监控mysql数据库 SpringBoot cana ...
- Turbine使用
一.简介 Turbine是聚合服务器发送事件流数据的一个工具,Hystrix的监控中,只能监控单个节点,实际生产中都为集群,因此可以通过Turbine来监控集群下Hystrix的metrics情况 T ...
- JpaRepository 增删改查
Jpa查询 JpaRepository简单查询 基本查询也分为两种,一种是spring data默认已经实现,一种是根据查询的方法来自动解析成SQL. 预先生成方法 spring data jpa 默 ...
- 【C/C++】习题3-1 得分/算法竞赛入门经典
[题目]一个由O和X组成的串,O的得分为目前连续出现的O的个数,X的得分为0.要求统计得分. 我一开始以为要输出表达式,结果好像不需要? [代码] #include <stdio.h> # ...
- 十二. Go并发编程--sync/errGroup
一.序 这一篇算是并发编程的一个补充,起因是当前有个项目,大概の 需求是,根据kafka的分区(partition)数,创建同等数量的 消费者( goroutine)从不同的分区中消费者消费数据,但是 ...
- 第43篇-JNI引用的管理(2)
之前我们已经介绍了JNIHandleBlock,但是没有具体介绍JNIHandleBlock中存储的句柄,这一篇我们将详细介绍对这些句柄的操作. JNI句柄分为两种,全局和局部对象引用: (1)大部分 ...
- Mysql资料 xtrabackup
目录 一.简介 原理 优缺点 二.安装 三.日常使用 备份所有库 增量备份 远程备份 四.参数 一.简介 原理 其实XtraBackup也是基于INNODB的 crash-recovery功能来实现的 ...
- rpm-build方式制作rpm包
目录 一.简介 二.具体操作 一.简介 可以将编译完成的服务打成rpm包放到私有仓库了,用于自定义的各种软件进行安装部署配置. 二.具体操作 1.安装软件,这个命令将构建rpm包 yum -y ins ...
- Python用matplotlib绘图网格线的设置
一.X轴网格线的设置 import matplotlib.pyplot as plt import numpy as np from pylab import mpl mpl.rcParams['fo ...