BZOJ 2154 Crash的数字表格 (莫比乌斯反演)

2154: Crash的数字表格

Time Limit: 20 Sec  Memory Limit: 259 MB
Submit: 4958  Solved: 1811
[Submit][Status][Discuss]

Description

今天的数学课上,Crash小朋友学习了最小公倍数(Least Common Multiple)。对于两个正整数a和b,LCM(a, b)表示能同时被a和b整除的最小正整数。例如,LCM(6, 8) = 24。回到家后,Crash还在想着课上学的东西,为了研究最小公倍数,他画了一张N*M的表格。每个格子里写了一个数字,其中第i行第j列的那个格子里写着数为LCM(i, j)。一个4*5的表格如下: 1 2 3 4 5 2 2 6 4 10 3 6 3 12 15 4 4 12 4 20 看着这个表格,Crash想到了很多可以思考的问题。不过他最想解决的问题却是一个十分简单的问题:这个表格中所有数的和是多少。当N和M很大时,Crash就束手无策了,因此他找到了聪明的你用程序帮他解决这个问题。由于最终结果可能会很大,Crash只想知道表格里所有数的和mod 20101009的值。

Input

输入的第一行包含两个正整数,分别表示N和M。

Output

输出一个正整数,表示表格中所有数的和mod 20101009的值。

Sample Input

4 5

Sample Output

122
【数据规模和约定】
100%的数据满足N, M ≤ 10^7。

HINT

 

Source

析:题意很明显就是

到就已经可以A掉本题了,当然还可以断续向下优化。

代码如下:

#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 lowbit(x) -x&x
//#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 = 1e7 + 5;
const int maxm = 2e4 + 10;
const LL mod = 20101009;
const int dr[] = {-1, 1, 0, 0, 1, 1, -1, -1};
const int dc[] = {0, 0, 1, -1, 1, -1, 1, -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 f[maxn];
bool vis[maxn];
int mu[maxn], prime[maxn];

void Moblus(){
  mu[1] = 1;
  int tot = 0;
  for(int i = 2; i <= n; ++i){
    if(!vis[i])  prime[tot++] = i, mu[i] = -1;
    for(int j = 0; j < tot; ++j){
      int t = i * prime[j];
      if(t > n)  break;
      vis[t] = 1;
      if(i % prime[j] == 0)  break;
      mu[t] = -mu[i];
    }
  }
  for(int i = 1; i <= n; ++i)  f[i] = (f[i-1] + mu[i] * (LL)i * i) % mod;
}

inline LL cal(int x){ return 1LL * x * (x+1) / 2 % mod; }

LL solve(int n, int m){
  LL ans = 0;
  for(int i = 1, det; i <= n; i = det + 1){
    det = min(n/(n/i), m/(m/i));
    ans = (ans + (f[det] - f[i-1]) * cal(n/i) % mod * cal(m/i)) % mod;
  }
  return ans;
}

int main(){
  scanf("%d %d", &n, &m);
  Moblus();
  if(n > m)  swap(n, m);
  LL ans = 0;
  for(int i = 1, det; i <= n; i = det + 1){
    det = min(n/(n/i), m/(m/i));
    ans = (ans + (LL)(det-i+1) * (i+det) % mod * solve(n/i, m/i) % mod * 10050505LL) % mod;
  }
  printf("%lld
", (ans+mod)%mod);
  return 0;
}

  

原文地址:https://www.cnblogs.com/dwtfukgv/p/8393825.html