zjnu1757Excellent (数学公式)

Description

Let us begin with a positive integer N and find the smallest positive integer which doesn't divide N. If we repeat the procedure with the resulting number, then again with the 

new result and so on, we will eventually obtain the number 2 (two). Let us define strength(N) as the length of the resulting sequence. 

For example, for N = 6 we obtain the sequence 6, 4, 3, 2 which consists of 4 numbers, thus strength(6) = 4. 

Given two positive integers A < B, calculate the sum of strengths of all integers between A and B (inclusive), that is, strength(A) + strength(A + 1) + ... + strength(B).

Input

The first and only line of input contains two positive integers, A and B (3 ≤ A < B < 10^17). 

Output

The first and only line of output should contain the requested sum of strengths.

Sample Input

 
 
3 6 100 200

Sample Output

 
 
11

262

题意:一个数按如下方法得到一个序列,即每一次找到最小的不整除它的数,然后再把找到的数当做这个数,一次循环下去,直到变为2,产生的序列组成的长度即为这一个数的强度,现在给你两个数A,B,问[A,B]这个区间内的所有数的强度和是多少。

思路:我们打表可以发现,10^7内的数第一个最小的不整除它的数都很小,最大为47,所以我们可以算出这个区间内第一个不整除的数为2,3,4,5...47的数的个数,然后乘上2,3,..47的强度累加起来就可以了。那么现在要做的就是怎么算区间内第一个不整除的数为x的个数,可以观察得到,要使得第一个不整除的数为x,那么这个数一定要整除1,2,...x-1,即lcm(1,...x-1),但是这个数又不能整除x,所以要在这些数中减去整除x的数的个数,即减去能整除lcm(1,....x)的数的个数。对于一个区间[A,B],能整除k的数的个数是B/k-(A-1)/k,所以就可以算出来了。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<queue>
#include<stack>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
ll zhi[100]={0,2,3,4,5,7,8,9,11,13,16,17,19,23,25,27,29,31,32,37,41};
ll stg[100]={0,1,2,3,2,2,3,2 ,2 ,2 ,3 ,2 ,2 ,2 ,2 ,2 ,2 ,2 ,3 ,2 ,2};
ll num1[20],num2[20];
ll gs[100];
ll gcd(ll a,ll b){
    return b ? gcd(b,a%b) : a;
}
void init()
{
    ll i,j;
    gs[1]=1;
    for(i=2;i<=42;i++){
        gs[i]=gs[i-1]/gcd(gs[i-1],i)*(ll)i;
    }
}

int main()
{
    ll n,m;
    int i,j;
    init();
    while(scanf("%lld%lld",&m,&n)!=EOF)
    {
        ll sum=0;
        for(i=1;i<=20;i++){
            sum+=(n/gs[zhi[i]-1 ]-(m-1)/gs[zhi[i]-1 ]-( n/gs[zhi[i]]-(m-1)/gs[zhi[i] ] ) )*(stg[i]+1);
        }
        printf("%lld\n",sum);
    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值