【BZOJ 3994】[SDOI2015] 约数个数和

相关链接

题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=3994
数据生成器:http://paste.ubuntu.com/23992676/
神犇题解:https://blog.sengxian.com/solutions/bzoj-3994

解题报告

这题要用到一个结论: $d(xy) = \sum\limits_{i|x} {\sum\limits_{j|y} {[\gcd (i,j) = 1]} } $
这个结论似乎没有办法从意义上去推导,证明也只能是展开后发现刚好相等
但这个结论前不久集训的时候就考过一次,然而做这题的时候一点印象都没有
我要是下一次还记不住这个结论就直播吃键盘!

好了现在开始说正解
知道上面的结论以后,答案就成了 $\sum\limits_{k = 1}^n {\mu (k)\sum\limits_{i = 1}^{\frac{n}{k}} {d(i)\sum\limits_{j = 1}^{\frac{m}{k}} {d(j)} } } $
然后设$f(i)$为除数函数的前缀和,答案就变成了: $\sum\limits_{k = 1}^n {\mu (k)f(\frac{n}{k})f(\frac{m}{k})} $
于是直接下底函数分块就可以啦!

Code

除数函数可以线筛,不过偷懒写的欧拉筛法

#include<bits/stdc++.h>
#define LL long long
using namespace std;

const int N = 50000+9; 

int n,m,tot,pri[N],mu[N],d[N],sum[N],f[N];
bool vis[N]; LL vout;

inline int read() {
	char c=getchar(); int f=1,ret=0;
	while (c<'0'||c>'9') {if(c=='-')f=-1;c=getchar();}
	while (c<='9'&&c>='0') {ret=ret*10+c-'0';c=getchar();}
	return ret * f;
}

inline void prework() {
	mu[1] = 1;
	for (int i=2;i<N;i++) {
		if (!vis[i]) pri[++tot] = i, mu[i] = -1;
		for (int j=1;j<=tot&&pri[j]*i<N;j++) {
			vis[i*pri[j]] = 1;
			if (i % pri[j]) mu[i*pri[j]] = -mu[i];
			else {mu[i*pri[j]] = 0; break;}
		}
	}
	for (int i=1;i<N;i++) 
		for (int j=i;j<N;j+=i) 
			d[j]++;
	for (int i=1;i<N;i++) {
		f[i] = f[i-1] + d[i];
		sum[i] = sum[i-1] + mu[i];
	}
}	

int main() {
	prework();
	for (int T=read();T;T--,vout=0) {
		n = read(); m = read();
		if (m > n) swap(n, m);
		for (int l=1,r;l<=m;l=r+1) {
			r = min(n / (n / l), m / (m / l));
			vout += (LL)(sum[r] - sum[l-1]) * f[n/l] * f[m/l];
		}
		printf("%lld\n",vout);
	}
	return 0;
}

34 thoughts to “【BZOJ 3994】[SDOI2015] 约数个数和”

  1. Hi there just wanted to give you a quick heads up and let
    you know a few of the images aren’t loading properly. I’m not sure why but I think its
    a linking issue. I’ve tried it in two different web browsers
    and both show the same results.

  2. Thanks for another informative blog. The place else may just I am getting that type of info written in such an ideal way?
    I have a venture that I’m simply now running on, and I have been on the look out for such
    information.

  3. I loved as much as you will receive carried out right
    here. The sketch is attractive, your authored subject matter stylish.
    nonetheless, you command get bought an nervousness over that you wish be delivering the following.

    unwell unquestionably come further formerly again as exactly the same nearly very often inside case you shield
    this increase.

  4. Hello there, You have done a fantastic job. I’ll certainly digg it and personally suggest to my friends.

    I am confident they will be benefited from this web site.

  5. Good day! This is my first comment here so I just wanted to give a quick shout
    out and say I genuinely enjoy reading your articles.
    Can you suggest any other blogs/websites/forums that cover the same topics?
    Thank you so much!

  6. Good post. I learn something new and challenging on websites I stumbleupon every day.
    It’s always interesting to read through articles from other authors and use a little something from their websites.

  7. I’m curious to find out what blog platform you happen to be utilizing?
    I’m having some small security problems with my
    latest blog and I’d like to find something more
    risk-free. Do you have any recommendations?

  8. I don’t even know how I stopped up right here, however I believed
    this submit used to be good. I do not recognise who you are but definitely you are going to a
    famous blogger should you aren’t already. Cheers!

  9. Howdy! This is my first visit to your blog! We are a collection of volunteers and starting a new project in a community in the
    same niche. Your blog provided us valuable information to work on.
    You have done a extraordinary job!

  10. Today, while I was at work, my cousin stole my iphone and tested to see if it can survive a 30 foot drop, just so she can be a youtube sensation. My iPad is now destroyed and she has 83 views. I know this is completely off topic but I had to share it with someone!

  11. Hey! I’m at work surfing around your blog from my new iphone!
    Just wanted to say I love reading through your blog and look forward to all your posts!
    Carry on the outstanding work!

  12. Wonderful blog! I found it while browsing on Yahoo News. Do you
    have any suggestions on how to get listed in Yahoo
    News? I’ve been trying for a while but I never seem to get
    there! Appreciate it

  13. Spot on with this write-up, I actually think
    this website needs a lot more attention. I’ll probably be back again to read more, thanks for the
    info!

  14. My brother recommended I might like this web site. He was
    entirely right. This post actually made my day. You can not imagine simply
    how much time I had spent for this info! Thanks!

  15. It’s a pity you don’t have a donate button! I’d most
    certainly donate to this fantastic blog! I guess for now i’ll
    settle for bookmarking and adding your RSS feed to my Google account.
    I look forward to fresh updates and will share this blog with my Facebook group.
    Talk soon!

  16. Thank you for every other informative site. The place else could I am getting that type of info written in such an ideal approach?
    I have a mission that I’m just now working on, and I’ve been at the look out for such info.

  17. Wonderful beat ! I wish to apprentice while you amend your website, how could i subscribe for a blog site?
    The account aided me a acceptable deal. I had been tiny bit acquainted of this your broadcast provided bright clear
    idea

  18. When I originally commented I clicked the “Notify me when new comments are added” checkbox and now each time a comment is added I get three e-mails with the same comment. Is there any way you can remove people from that service? Thank you!

  19. It’s hard to come by experienced people about this topic, however, you
    seem like you know what you’re talking about! Thanks

Leave a Reply

Your email address will not be published. Required fields are marked *