codeforces100917 Constant Ratio
来源:互联网 发布:怎么连接23端口 编辑:程序博客网 时间:2024/06/17 16:47
codeforces100917 Constant Ratio 题目链接:http://codeforces.com/gym/100917/problem/C
题面描述:
C. Constant Ratio
time limit per test
1 secondmemory limit per test
256 megabytesinput
standard inputoutput
standard outputGiven an integer n, find out number of ways to represent it as the sum of two or more integers ai with the next property: ratio ai / ai - 1 is the same positive integer for all possible i > 1.
Input
Input consists of one integer n (1 ≤ n ≤ 105).
Output
Print one integer — number of representations.
Examples
input
1
output
0
input
5
output
2
input
567
output
21
Note
In the first sample no such representation exists.
In the second sample there exist two representations:
- 1 1 1 1 1, then q = 1.
- 1 4, then q = 4.
题目大意:
求等比数列的前n项和,看前n项和能否得到n。
题目分析:
可以先找出每一个可以被n整除的数,作为a1,然后再找公比,由于是等比数列,所以时间复杂度不是很高。
代码实现:方法一:
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>#include <cstdlib>#include <cmath>using namespace std;typedef long long LL;int n;bool find(int a1,int a2){ int k=a2/a1; LL ak=a1; LL sum=a1; while(1) { ak*=k; sum+=ak; if (sum==n) return true; if (sum>n) return false; }}int main(){ while(scanf("%d",&n)!=-1) { int ans=0; for(int i=1; i<n; i++) if (n%i==0) { for(int j=i; j<=n; j+=i) { if (find(i,j)) ans++; } } printf("%d\n",ans); } return 0;}
方法二:
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>using namespace std;long long n;long long pow(long long a,long long q){ long long ans=1; while(q--) ans*=a; return ans;}bool find(long long a1,long long a2){ long long q=a2/a1; if(q==1) { if(n%a1==0) return true; else return false; } for(long long k=1; k<=n*n; k++)///注意k的范围给的给定,要使得能够跳出循环 { if(n==(a1*(pow(q,k)-1)/((q-1)))) return true; else if((a1*(pow(q,k)-1)/((q-1)))>n) return false; else continue; }}int main(){ while(scanf("%lld",&n)!=-1) { if(n==1) printf("0\n"); else { long long ans=0; for(long long i=1; i<n; i++)//a1,不带n { if(n%i==0) { for(long long j=i; j<=n; j+=i) { if(find(i,j)) ans++; } } } printf("%lld\n",ans); } } return 0;}
0 0
- codeforces100917 Constant Ratio
- ratio
- ratio
- 【2015-2016 XVI Open CupC】【暴力】Constant Ratio 等差数列前若干项恰好为n
- Constant
- constant
- debt ratio
- 1079 Ratio
- english ratio
- Ratio(P1079)
- C++ ratio
- C++ ratio
- stretch ratio
- HDU6070Dirt Ratio
- 会议排名和ratio
- 什么是aspect ratio
- 什么是Aspect Ratio?
- Aspect Ratio转换
- C语言之字符数据的输入输出
- 字符流和字节流
- 超简单ListView打造的多级展开菜单
- canvas绘图小结
- PTN950设备TND1EG2单板的工作原理和信号流
- codeforces100917 Constant Ratio
- 斯托克斯公式--解决空间闭曲线的积分问题
- spring+springMVC+mybatis基础代码
- Git使用
- LeetCode : Find Minimum in Rotated Sorted Array
- codeVS 1474 十进制转m进制
- 华佳慧科技:Metro1000设备术语分享(一)
- malloc 原理 实现机制 vs free 原理 实现机制
- Ubuntu1系统中下载的中文文件名乱码解决办法