• CodeChef Sereja and GCD


    Sereja and GCD

     
    Problem code: SEAGCD
     

    All submissions for this problem are available.

    Read problems statements in Mandarin Chinese and Russian.

    In this problem Sereja is interested in the number of arrays of integers, A1, A2, ..., AN, with 1 ≤ Ai ≤ M, such that the greatest common divisor of all of its elements is equal to a given integer D.

    Find the sum of answers to this problem with D = L, D = L+1, ..., D = R, modulo 109+7.

    Input

    The first line of the input contains an integer T - the number of test cases. T tests follow, each containing a single line with the values of N, M, L, R.

    Output

    For each test case output the required sum, modulo 109+7.

    Constraints

    • 1T10
    • 1LRM

    Subtasks

    • Subtask #1: 1N, M10 (10 points)
    • Subtask #2: 1N, M1000 (30 points)
    • Subtask #3: 1N, M107 (60 points)

    Example

    Input:
    2
    5 5 1 5
    5 5 4 5
    
    Output:
    3125
    2
    
    

    对于一个 d , 1~m中有 m/d个数是 d的倍数, 自然就是有 (m/d)^n种排列方法。

    然而 , 这些排列当中,元素必须包含 d , 只要它减去那些只包含它的倍数的序列即可得出结果。

    #include <iostream>
    #include <cstdio>
    using namespace std ;
    typedef long long LL;
    const int mod = 1e9+7;
    const int N = 10000100;
    LL A[N] ;
    LL q_pow( LL a , LL b ) {
        LL res = 1 ;
        while( b ) {
            if( b&1 ) res =  res * a  % mod ;
            a = a * a % mod ;
            b >>= 1;
        }
        return res ;
    }
    int main() {
    //    freopen("in.txt","r",stdin);
        int _ ; cin >> _ ;
        while( _-- ) {
            LL n , m , l , r ;
            cin >> n >> m >> l >> r ;
            for( int d = m ; d >= l ; --d ) {
                if( d == m || m/d != m/(d+1) ) A[d] = q_pow( m/d , n );
                else A[d] = A[d+1] ;
            }
            for( int i = m ; i >= l ; --i ) {
                for( int j = i + i ; j <= m ; j += i ) {
                    A[i] =( ( A[i] - A[j] ) % mod + mod ) % mod ;
                }
            }
            LL ans = 0 ;
            for( int i = l ; i <= r ; ++i )
                ans = ( ans + A[i] ) % mod ;
            cout << ans << endl ;
        }
    }
    View Code
  • 相关阅读:
    week4:周测错题
    小程序1:登录/注册小程序
    小程序2:实现一个购物车
    day26:装饰器&面向对象当中的方法&property
    day25:7个魔术方法&5个关于类的魔术属性
    day24:多态&魔术方法__new__&单态模式
    day23:单继承&多继承&菱形继承&__init__魔术方法
    day22:面向对象封装对象操作&类操作&面向对象删除操作
    day21:正则函数&模块和包(import)
    APP探索之iAPP
  • 原文地址:https://www.cnblogs.com/hlmark/p/4296796.html
Copyright © 2020-2023  润新知