CodeChef SEAGCD 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 ;
    }
}

时间: 2024-10-05 18:53:52

CodeChef SEAGCD Sereja and GCD的相关文章

CodeChef SEALCM Sereja and LCM(矩阵快速幂)

Sereja and LCM Problem code: SEALCM Submit All Submissions All submissions for this problem are available. Read problems statements in Mandarin Chinese and Russian. In this problem Sereja is interested in number of arrays A[1], A[2], ..., A[N] (1 ≤ A

Maximum number, GCD condition (codechef March Challenge 2014)

题目 : http://acm.bnu.edu.cn/v3/problem_show.php?pid=40489 最近做到的一道蛮有意思的题目(codechef现在的题目确实很赞了) 题意 :中文题面 (cc的一大好处就是有中文翻译,嘿嘿) 区间Max = max{a_i|gcd(a_i, g) > 1 && x <= i <= y} 做法 : 一开始我是用分块做的,复杂的O(m * sqrt(n) * C) C 是所含不同素数的个数, C大概最大只有6或7吧 然后裸裸的

●CodeChef Sereja and Game

题链: https://www.codechef.com/problems/SEAGM题解: 概率dp,博弈论 详细题解:http://www.cnblogs.com/candy99/p/6504340.html 本体的先手胜与不胜(第一问)以及胜的概率(第二问)都是通过dp完成的,记忆化搜索实现. 定义了一个非常妙的dp状态:(第一问) dp[g][c]表示当前选的数的gcd为g,且选了c个数时当前操作的人胜还是不胜. 有了这个状态,配合预处理的cnt[g]数组(表示有cnt[g]个数为g的倍

CodeChef Gcd Queries

Gcd Queries Problem code: GCDQ Submit All Submissions All submissions for this problem are available. Read problems statements in Mandarin Chinese and Russian. You are given an array A of integers of size N. You will be given Q queries where each que

CodeChef Dynamic GCD

嘟嘟嘟vjudge 我今天解决了一个历史遗留问题! 题意:给一棵树,写一个东西,支持一下两种操作: 1.\(x\)到\(y\)的路径上的每一个点的权值加\(d\). 2.求\(x\)到\(y\)路径上所有点权的gcd. 树上路径操作自然能想到树剖,但问题在于区间加操作不好维护. 因此我们先考虑序列上的操作. 求gcd,方法除了辗转相除,还有更相减损之术啊!这个有一个非常好的性质,就是两数的gcd等于其中一个数和两数只差的gcd.两数之差,就让我们想到了差分.这样就能从区间修改变成了只修改连个点了

Codechef July Challenge 2014部分题解

Dish Owner(并查集) 链接:http://www.codechef.com/JULY14/problems/DISHOWN/ 题意分析:本题主要操作就是给出0 x y时,拥有第x道菜的厨师与拥有第y道菜的厨师pk,谁拥有的所有菜的其中一道菜(不一定是x或y)的分值比较高谁就获胜,并赢得loser的所有菜.即比较的是每个人分值最高的菜,所以对于非loser的人来说,他的分值最高的菜是不变的.综合题意用并查集易解. #include <cstdio> const int Maxn=100

Codechef LOCAUG17

做完题目很少有写题解的习惯,强行PO一组吧. 比赛链接:https://www.codechef.com/LOCAUG17 PRINCESS 给定字符串s,问s是否存在长度大于1的回文子串. 解:分两种情况.设n=|s|. 1. 存在回文子串长度为奇数.则存在2<=i<n,使得s[i-1]==s[i+1]. 2. 存在回文子串长度为偶数.则存在1<=i<n,使得s[i]==s[i+1]. 时间复杂度O(n). ALATE 给定长度为n的数组a[1..n].维护两种操作: 1. 给定

codechef Cutting Recipes题解

Cutting Recipes The chef has a recipe he wishes to use for his guests, but the recipe will make far more food than he can serve to the guests. The chef therefore would like to make a reduced version of the recipe which has the same ratios of ingredie

【CodeChef】Small factorials(BigInteger笔记)

You are asked to calculate factorials of some small positive integers. Input An integer t, 1<=t<=100, denoting the number of testcases, followed by t lines, each containing a single integer n, 1<=n<=100. Output For each integer n given at inpu