acdream 1148 GCD SUM 莫比乌斯反演 ansx,ansy
GCD SUM
Problem Description
执行如下程序:
long long ans = 0,ansx = 0,ansy
= 0;
for(int i = 1; i <= N; i ++)
for(int j = 1; j <= M; j
++)
if(gcd(i,j) == 1) ans ++,ansx += i,ansy += j;
cout << ans
<< " " << ansx << " " << ansy << endl;
Input
多组数据,每行两个数N,M(1 <= N,M <= 100000)。
Output
Sample Input
5 5
1 3
Sample Output
19 55 55
3 3 6
Hint



#include<iostream>
#include<stdio.h>
#include<cstring>
#include<cstdlib>
using namespace std;
typedef long long LL; const int maxn = 1e5+;
bool s[maxn];
int prime[maxn],len = ;
int mu[maxn];
LL hxl [maxn];
int sum1[maxn];
void init()
{
memset(s,true,sizeof(s));
mu[] = ;
for(int i=;i<maxn;i++)
{
if(s[i] == true)
{
prime[++len] = i;
mu[i] = -;
}
for(int j=;j<=len && (long long)prime[j]*i<maxn;j++)
{
s[i*prime[j]] = false;
if(i%prime[j]!=)
mu[i*prime[j]] = -mu[i];
else
{
mu[i*prime[j]] = ;
break;
}
}
}
for(int i=;i<maxn;i++)
sum1[i] = sum1[i-]+mu[i];
hxl[] = mu[];
for(int i=;i<maxn;i++){
hxl[i] = i*mu[i]+hxl[i-];
}
}
int main()
{
init();
int n,m;
while(scanf("%d%d",&n,&m)>)
{
LL sum = ;
LL ansi = ,ansj = ;
int a = n;
int b = m;
if(a>b) swap(a,b);
for(int i=,la = ;i<=a;i++,i = la+)
{
la = min(a/(a/i),b/(b/i));
sum = sum + ((LL)(a/i))*(b/i)*(sum1[la]-sum1[i-]);
ansi = ansi +(hxl[la]-hxl[i-])*(((LL)(n/i+)*(n/i))/)*(m/i);
ansj = ansj +(hxl[la]-hxl[i-])*(((LL)(m/i+)*(m/i))/)*(n/i);
}
printf("%lld %lld %lld\n",sum,ansi,ansj);
}
return ;
}
acdream 1148 GCD SUM 莫比乌斯反演 ansx,ansy的更多相关文章
- 【BZOJ2820】YY的GCD(莫比乌斯反演)
[BZOJ2820]YY的GCD(莫比乌斯反演) 题面 讨厌权限题!!!提供洛谷题面 题解 单次询问\(O(n)\)是做过的一模一样的题目 但是现在很显然不行了, 于是继续推 \[ans=\sum_{ ...
- 【BZOJ2818】Gcd(莫比乌斯反演)
[BZOJ2818]Gcd(莫比乌斯反演) 题面 Description 给定整数N,求1<=x,y<=N且Gcd(x,y)为素数的 数对(x,y)有多少对. Input 一个整数N Ou ...
- 【HDU1695】GCD(莫比乌斯反演)
[HDU1695]GCD(莫比乌斯反演) 题面 题目大意 求\(a<=x<=b,c<=y<=d\) 且\(gcd(x,y)=k\)的无序数对的个数 其中,你可以假定\(a=c= ...
- spoj 7001. Visible Lattice Points GCD问题 莫比乌斯反演
SPOJ Problem Set (classical) 7001. Visible Lattice Points Problem code: VLATTICE Consider a N*N*N la ...
- 【HDU4947】GCD Array (莫比乌斯反演+树状数组)
BUPT2017 wintertraining(15) #5H HDU- 4947 题意 有一个长度为l的数组,现在有m个操作,第1种为1 n d v,给下标x 满足gcd(x,n)=d的\(a_x\ ...
- SPOJ PGCD 4491. Primes in GCD Table && BZOJ 2820 YY的GCD (莫比乌斯反演)
4491. Primes in GCD Table Problem code: PGCD Johnny has created a table which encodes the results of ...
- P2257 YY的GCD (莫比乌斯反演)
[题目链接] https://www.luogu.org/problemnew/show/P2257 // luogu-judger-enable-o2 /* -------------------- ...
- 【洛谷2257】YY的GCD(莫比乌斯反演)
点此看题面 大致题意: 求\(\sum_{x=1}^N\sum_{y=1}^MIsPrime(gcd(x,y))\). 莫比乌斯反演 听说此题是莫比乌斯反演入门题? 一些定义 首先,我们可以定义\(f ...
- 洛谷P2257 YY的GCD(莫比乌斯反演)
传送门 原来……莫比乌斯反演是这么用的啊……(虽然仍然不是很明白) 首先,题目所求如下$$\sum_{i=1}^n\sum_{j=1}^m[gcd(i,j)=prim]$$ 我们设$f(d)$表示$g ...
随机推荐
- 前端新手分析 AJAX执行顺序,数据走向
我是一名前端的newer 在刚学习AJAX和eJS的时候,对于顺序上面有很大迷惑,现在稍微清楚了一点, 理解不对的地方,还请各位大牛帮助给我指导一下. 总的 服务器和客户端的顺序 一. 除了必要的 ...
- UISearchController的使用
- (void)addSearchController { _searchController = [[UISearchController alloc] initWithSearchResultsC ...
- Spark on Yarn
Spark on Yarn 1. Spark on Yarn模式优点 与其他计算框架共享集群资源(eg.Spark框架与MapReduce框架同时运行,如果不用Yarn进行资源分配,MapReduce ...
- Script to set the Purchase Order Status to ‘OPEN’(将采购订单重新打开)
Business Requirement: The finance user requests the IT team to change the PO status to OPEN as they ...
- 衣明志是个SB
面试碰到衣明志,问了些傻逼问题,尼玛就是一不折不扣的蠢驴. 这个人太能装了,而且水平也不咋地.
- equals 与 == 的区别和用法(C# & Java)【转】
转至http://www.cnblogs.com/beeone/archive/2011/04/25/2026674.html public class TestString { public sta ...
- 关于 ActiveMQ
今天玩了下 ActiveMQ,希望实现服务器的消息可以通知到各个客户终端. 安装: 1.安装 ActiveMQ 之前必须安装 Java 的 jdk , 可以从此下载: http://www.ora ...
- 10 个迅速提升你 Git 水平的提示【转】
转自:https://www.oschina.net/translate/10-tips-git-next-level 最近我们推出了两个教程:熟悉Git的基本功能和让你在开发团队中熟练的使用Git ...
- Swoole 遇上 PHP会是怎样的结果呢
一直想写点Swoole的东西,毕竟它重新定义了php,却一直不知道怎么下手写 Swoole涉及的知识点非常多,互为表里,每次想写都发现根本理不出一个头绪 Swoole是一个php的扩展,它的核心目的就 ...
- JavaEE基础(九)
1.面向对象(多态的概述及其代码体现) A:多态(polymorphic)概述 事物存在的多种形态 B:多态前提 a:要有继承关系. b:要有方法重写. c:要有父类引用指向子类对象. C:案例演示 ...