Gym - 100917C . Constant Ratio

C. Constant Ratio
time limit per test
1.0 s
memory limit per test
256 MB
input
standard input
output
standard output

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


1.比赛时的代码。用等比公式 比较朴素的跑了一遍。62ms

  • formula
  • formula

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#define inf 0x3f3f3f
#define ms(x) memset(x,0,sizeof(x))
#define ll long long
using namespace std;
ll pod(int a, int n)
{
    if(n == 0) return 1;
    int x = pod(a,n/2);
    long long ans = (long long )x*x;
    if(n%2 == 1) ans = ans *a;
    return (ll)ans;
}
int main()
{
    int n;
    scanf("%d",&n);
    int ans =0;
    int flag=1;
    ll up,down,l;
    for(int i=2; i<n; i++) //q
    {
        for(int j=1; j<n/2; j++) //a1
        {
            if(j+j*i>n) {flag =0;break;}             //这是个比较重要的剪枝,当 前两项(a1 + a1*q) 大于n时即可跳出。
            for(int k=1; k<=n/i+1; k++) //n          //范围减小到n/i+1
            {
                l = (ll)pod(i,k);
                up = (ll)j*(1-l);
                down = 1-i;
                if(up/down == n) ans++;
                if(up/down > n )
                {
                    flag=0;
                    break;
                }
            }
        }
    }
    for(int k=1; k<n; k++)
        if(n%k==0) ans++;
    printf("%d\n",ans);
    return 0;
}

2.  还有种优化的方法              Sn 是整数, a1也必是整数。判断一下a1。    31ms.

  • formula


    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <queue>
    #define inf 0x3f3f3f
    #define ms(x) memset(x,0,sizeof(x))
    #define ll long long
    using namespace std;
    ll pod(int a, int n)
    {
        if(n == 0) return 1;
        int x = pod(a,n/2);
        long long ans = (long long )x*x;
        if(n%2 == 1) ans = ans *a;
        return (ll)ans;
    }
    int main()
    {
        int n;
        scanf("%d",&n);
        int ans =0;
        int flag=1;
        ll up,down,l;
        for(int i=2; i<n; i++) //q
        {
            for(int j=1; j<n/2; j++) //a1
            {
                if(j+j*i>n) {flag =0;break;}//这是个比较重要的剪枝,当 前两项(a1 + a1*q) 大于n时即可跳出。
                if(n%j==0)    // 加上这句话 变成31ms
                for(int k=1; k<=n/i+1; k++) //n          //范围减小到n/i+1
                {
                    l = (ll)pod(i,k);
                    up = (ll)j*(1-l);
                    down = 1-i;
                    if(up/down == n) ans++;
                    if(up/down > n )
                    {
                        flag=0;
                        break;
                    }
                }
            }
        }
        for(int k=1; k<n; k++)
            if(n%k==0) ans++;
        printf("%d\n",ans);
        return 0;
    }
    

    3.   将公式变形 得到

    Sn(q-1)  = a1(q^n -1)

    #include <iostream>
    #include <cstring>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <queue>
    #define inf 0x3f3f3f
    #define ms(x) memset(x,0,sizeof(x))
    #define ll long long
    using namespace std;
    int main()
    {
        int n;
        scanf("%d",&n);
        int ans =0;
        for(int i=2; i<=n; i++) //q
        {
            ll R = (ll)n * (i-1);
            for(long long qn = (ll)i*i; qn-1 <= R; qn*=i)
            {
                //if(R%(qn-1)==0) ans ++;
                ans += R%(qn-1)==0;
            }
        }
        for(int k=2; k<=n; k++)
            ans+= n%k==0;          //竟然有这种操作。。。超省时
        printf("%d\n",ans);
        return 0;
    }
    




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值