ZOJ_3224 Mathematics(数论)

Mathematics

Time Limit: 1000 ms
Memory Limit: 32768 KB
Problem Description

Mathematics is always attractive. For example, Fourier Transform can split a normal function into parts with Sin function and Cos function, which shows magical properties. Log function can change a number in a huge range to a very small range. And Z-Transform can transform a complex signal into a new function to simplify the analysis.

Navi likes mathematics very much (Although he does not major in math). Especially he likes the Log function. Because with different base number chosen, a number n will be change to different number via function Log. What’s more, he is currently considering numbers satisfying a special condition, which is, for a given number a, the number n is divisible by [ L o g a n ] [Log_an] [Logan], where [ n ] [n] [n] means the greatest integer less then or equal to n.

Now Navi has generated some numbers and stored them in array a. He wants to find out the number of numbers n in interval [ A , B ] [A, B] [A,B] satisfying [ L o g a i n ] [Log_{a_i}n] [Logain] is positive and n is divisible by [ L o g a i n ] [Log_{a_i}n] [Logain], for all a i a_i ai.

Input

There will be no more than 20 cases.
The first line of each case will be three integers N, A, B ( 1 < = N < = 500 1 <= N <= 500 1<=N<=500, 5 < = A < = B < = 2 31 − 1 5 <= A <= B <= 2^{31} - 1 5<=A<=B<=2311). The second line contains N numbers a 1 . . . a N a_1...a_N a1...aN, 2 < = a i < = 10000 2 <= a_i <= 10000 2<=ai<=10000.

Output

One line per case, the number of numbers in [A, B] satisfying the condition above.

Sample Input

1 6 6
2
2 5 14
2 3

Sample Output

1
2

题意

n个数 a i a_i ai,求区间 [ a , b ] [a,b] [a,b]满足对于所有 a i a_i ai都有 ⌊ l o g a i n ⌋ ∣ n \left\lfloor{log_{a_i}n}\right\rfloor | n logainn的整数的数量。

题解:

区间很大,但对于每个 a i a_i ai,最多可将按 ⌊ l o g a i n ⌋ \left\lfloor{log_{a_i} n}\right\rfloor logain的值区间分为30+段,每段为 [ a i , a i 2 ) , [ a i 2 , a i 3 ) . . . . . [a_i,a_i^2),[a_i^2,a_i^3)..... [ai,ai2),[ai2,ai3).....,所以对于 a i a_i ai求出其分段,对于每段求出n个 a i a_i ai分段的lcm,求出段区间内有多少个数能整除lcm即可。

#include<stdio.h>
#include<iostream>
#include<cstdlib>
#include<cmath>
#include<algorithm>
#include<cstring>
#include<map>
#include<vector>
#include<queue>
#include<iterator>
#define dbg(x) cout<<#x<<" = "<<x<<endl;
#define INF 0x3f3f3f3f
#define eps 1e-7
 
using namespace std;
typedef long long LL;   
typedef pair<int, int> P;
const int maxn = 1020;
const int mod = 998244353;
struct node{
    int x;
    LL pos;
}p[maxn*30];
int c[maxn], d[42];
bool cmp(node a, node b);
LL gcd(LL a, LL b);
LL solve(LL l, LL r, LL lc);

int main()
{
    int top, n, i, j, k;
    LL x, ans, a, b;
    while(~scanf("%d %lld %lld", &n, &a, &b))
    {
        top = 0;
        ans = 0;
        memset(d, 0, sizeof(d));
        for(i=0;i<n;i++){
            scanf("%d", &c[i]);
            x = 1;
            for(j=1;j<=32 && x<=b;j++)
            {
                x *= c[i];
                p[top].pos = x;
                p[top].x = j;
                top++;
            }
        }
        p[top].pos = 1e13, p[top].x = 50;
        top++;
        sort(p, p+top, cmp);
        for(i=0;i<top;){
            for(k=i;k<top && p[k].pos == p[i].pos;k++)
                d[p[k].x]++, d[p[k].x-1]--;
            int num = 0;
            x = 1;
            for(j=1;j<=33;j++)
                if(d[j]){
                    num+=d[j];
                    x = x/gcd(x, j)*j;
                }
            if(num == n){
                ans += solve(max((LL)a, p[i].pos), min((LL)b, p[k].pos-1), x);
            }
            i = k;
        }
        printf("%lld\n", ans);
    }
    return 0;
}

LL gcd(LL a, LL b)
{
    return b==0?a:gcd(b, a%b);
}

bool cmp(node a, node b)
{
    return a.pos < b.pos;
}

LL solve(LL l, LL r, LL lc)
{
    if(l>r)return 0LL;
    return r/lc-(l-1)/lc;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值