4407: 于神之怒加强版

Time Limit: 80 Sec  Memory Limit: 512 MB
Submit: 1067  Solved: 494
[Submit][Status][Discuss]

Description

给下N,M,K.求
 
 

Input

输入有多组数据,输入数据的第一行两个正整数T,K,代表有T组数据,K的意义如上所示,下面第二行到第T+1行,每行为两个正整数N,M,其意义如上式所示。

Output

如题

Sample Input

1 2
3 3

Sample Output

20

HINT

1<=N,M,K<=5000000,1<=T<=2000

题解:JudgeOnline/upload/201603/4407.rar

Source

命题人:成都七中张耀楠,鸣谢excited上传。

析:首先能看出来是莫比乌斯反演,直接求是单次O(n*sqrt(n)),肯定会TLE,然后进行两次分块,单次时间复杂度是O(n),这样我本以为就能过了,结果还是TLE,实在是没想到好办法,就看了题解,题解是再进行化简,只要一次分块就好,其他的都进行预处理,单次询问复杂度是O(sqrt(n))。盗用一张图。

最后这个F函数是一个积性函数,可以用递推和筛法来求。

代码如下:

#pragma comment(linker, "/STACK:1024000000,1024000000")
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <cstring>
#include <set>
#include <queue>
#include <algorithm>
#include <vector>
#include <map>
#include <cctype>
#include <cmath>
#include <stack>
#include <sstream>
#include <list>
#include <assert.h>
#include <bitset>
#include <numeric>
#define debug() puts("++++")
#define gcd(a, b) __gcd(a, b)
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define fi first
#define se second
#define pb push_back
#define sqr(x) ((x)*(x))
#define ms(a,b) memset(a, b, sizeof a)
#define sz size()
#define pu push_up
#define pd push_down
#define cl clear()
//#define all 1,n,1
#define FOR(i,x,n) for(int i = (x); i < (n); ++i)
#define freopenr freopen("in.txt", "r", stdin)
#define freopenw freopen("out.txt", "w", stdout)
using namespace std; typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int, int> P;
const int INF = 0x3f3f3f3f;
const LL LNF = 1e17;
const double inf = 1e20;
const double PI = acos(-1.0);
const double eps = 1e-8;
const int maxn = 5e6 + 5;
const int maxm = 3e5 + 10;
const LL mod = 1e9 + 7LL;
const int dr[] = {-1, 0, 1, 0};
const int dc[] = {0, -1, 0, 1};
const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
int n, m;
const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
inline bool is_in(int r, int c) {
return r >= 0 && r < n && c >= 0 && c < m;
} LL fast_pow(LL a, int n){
LL res = 1;
while(n){
if(n&1) res = res * a % mod;
a = a * a % mod;
n >>= 1;
}
return res;
} LL f[maxn];
int prime[maxn];
bool vis[maxn]; void Moblus(int k){
int tot = 0;
f[1] = 1;
for(int i = 2; i < maxn; ++i){
if(!vis[i]) prime[tot++] = i, f[i] = fast_pow(i, k) - 1;
for(int j = 0; j < tot && i * prime[j] < maxn; ++j){
int t = i * prime[j];
vis[t] = 1;
if(i % prime[j] == 0){
f[t] = f[i] * fast_pow(prime[j], k) % mod;
break;
}
f[t] = f[i] * f[prime[j]] % mod;
}
}
for(int i = 2; i < maxn; ++i) f[i] = (f[i-1] + f[i]) % mod;
} int main(){
int T, k; scanf("%d %d", &T, &k);
Moblus(k);
while(T--){
scanf("%d %d", &n, &m);
int mmin = min(n, m);
LL ans = 0;
for(int i = 1, det = 1; i <= mmin; i = det + 1){
det = min(n/(n/i), m/(m/i));
ans = (ans + (f[det] - f[i-1]) * (n/i) % mod * (m/i)) % mod;
}
printf("%lld\n", (ans+mod)%mod);
}
return 0;
}

  

BZOJ 4407 于神之怒加强版 (莫比乌斯反演 + 分块)的更多相关文章

  1. BZOJ 4407: 于神之怒加强版 [莫比乌斯反演 线性筛]

    题意:提前给出\(k\),求\(\sum\limits_{i=1}^n \sum\limits_{j=1}^m gcd(i,j)^k\) 套路推♂倒 \[ \sum_{D=1}^n \sum_{d|D ...

  2. BZOJ.4407.于神之怒加强版(莫比乌斯反演)

    题目链接 Description 求\[\sum_{i=1}^n\sum_{j=1}^m\gcd(i,j)^K\ \mod\ 10^9+7\] Solution 前面部分依旧套路. \[\begin{ ...

  3. BZOJ 4407: 于神之怒加强版 莫比乌斯反演 + 线筛积性函数

    Description 给下N,M,K.求     Input 输入有多组数据,输入数据的第一行两个正整数T,K,代表有T组数据,K的意义如上所示,下面第二行到第T+1行,每行为两个正整数N,M,其意 ...

  4. bzoj 4407 于神之怒加强版 (反演+线性筛)

    于神之怒加强版 Time Limit: 80 Sec  Memory Limit: 512 MBSubmit: 1184  Solved: 535[Submit][Status][Discuss] D ...

  5. 【BZOJ-4407】于神之怒加强版 莫比乌斯反演 + 线性筛

    4407: 于神之怒加强版 Time Limit: 80 Sec  Memory Limit: 512 MBSubmit: 241  Solved: 119[Submit][Status][Discu ...

  6. BZOJ 2301 Problem b(莫比乌斯反演+分块优化)

    题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=37166 题意:对于给出的n个询问,每次求有多少个数对(x,y),满 ...

  7. ●BZOJ 4407 于神之怒加强版

    题链: http://www.lydsy.com/JudgeOnline/problem.php?id=4407 题解: 莫比乌斯反演 直接套路化式子 $\begin{align*}ANS&= ...

  8. 【bzoj4407】于神之怒加强版 莫比乌斯反演+线性筛

    题目描述 给下N,M,K.求 输入 输入有多组数据,输入数据的第一行两个正整数T,K,代表有T组数据,K的意义如上所示,下面第二行到第T+1行,每行为两个正整数N,M,其意义如上式所示. 输出 如题 ...

  9. 洛谷 - P4449 - 于神之怒加强版 - 莫比乌斯反演

    https://www.luogu.org/problemnew/show/P4449 \(F(n)=\sum\limits_{i=1}^{n}\sum\limits_{i=1}^{m} gcd(i, ...

随机推荐

  1. polymorphism多态

    [概念] 方法名相同,具体操作根据类不同. eg 有open()方法的ebook, kindle 都会被打开 eg 动物叫声不同 inheritance:只有superclass subclass都有 ...

  2. iOS工程中创建pch文件

    1.新建pch类文件 2.在工程配置中,Build Setting 下搜索"pre"寻找Apple LLVM6.1 - Language下的 Preflx Header 3.点开P ...

  3. 对于devexpress gridview 内插图加加进度条等的一点解读

    如上图,gategory 加了小图标, 其他行内还有计算器,大图片   进度条等 using System; using System.Drawing; using System.Collection ...

  4. (转)Android EditText限制输入字符的5种实现方式

    最近项目要求限制密码输入的字符类型, 例如不能输入中文.   现在总结一下EditText的各种实现方式,  以比较各种方法的优劣. 第一种方式:  设置EditText的inputType属性,可以 ...

  5. JavaScript中双叹号(!!)和单叹号(!)

    转自:JavaScript中双叹号(!!)作用 经常看到这样的例子: var a: var b=!!a; a默认是undefined.!a是true,!!a则是false,所以b的值是false,而不 ...

  6. angular2.0学习笔记2.创建hello world项目

    1.打开终端命令行窗口 2.切换到指定目录 E:\cd myobject 3.创建新项目 ng new angular2-helloworld 4.创建成功后,在angular2-helloworld ...

  7. linux RCU锁机制分析

    openVswitch(OVS)源代码之linux RCU锁机制分析 分类: linux内核  |  标签: 云计算,openVswitch,linux内核,RCU锁机制  |  作者: yuzhih ...

  8. BZOJ3191或洛谷2059 [JLOI2013]卡牌游戏

    BZOJ原题链接 洛谷原题链接 我们可以倒着来\(DP\). 设\(f[i][j]\)表示剩余\(i\)个人,从庄家数起第\(j\)个人的胜率,设当前枚举到第\(k\)张牌,该情况下这一轮淘汰的位置为 ...

  9. BZOJ1178或洛谷3626 [APIO2009]会议中心

    BZOJ原题链接 洛谷原题链接 第一个问题是经典的最多不相交区间问题,用贪心即可解决. 主要问题是第二个,求最小字典序的方案. 我们可以尝试从\(1\to n\)扫一遍所有区间,按顺序对每一个不会使答 ...

  10. iOS最全的常用正则表达式大全

    很多不太懂正则的朋友,在遇到需要用正则校验数据时,往往是在网上去找很久,结果找来的还是不很符合要求.所以我最近把开发中常用的一些正则表达式整理了一下,包括校验数字.字符.一些特殊的需求等等.给自己留个 ...