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 ...
随机推荐
- SQL server 表之间的关系生成图
选择数据库名->数据库关系鼠标右键->新建数据库关系图 按着ctrl选择要添加的表 点击添加
- [转]关于安装hadoop中出现的的 $HADOOP_HOME is deprecated 的解决方法
当前用户的.bash_profile在/home/用户/下,系统的.bash_profile在/etc/skel目录下; 默认可能是隐藏的:有人会问了,隐藏的我怎么打开它,一个简单的办法,直接使用vi ...
- yii添加行的增删改查
效果图: 控制器: <?phpnamespace backend\controllers;use Yii;use yii\web\Controller;use backend\models\Zh ...
- 在linux下的firefox中安装flashplayer
将 libflashplayer.so 复制进 ~/.mozilla/plugins/ 若目标目录不存在,请自行创建
- 【sublime】解决汉字输入的办法——InputHelper;在sublime中输入汉字==》InputHelper方法
手工安装办法: cd ~/.config/sublime-text-2/Packages #如果是sublime3就改一下 git clone https://github.com/xgenvn/I ...
- 最懂中文的H5前端框架amazeUI
Amaze UI 是一个轻量级(所有 CSS 和 JS gzip 后 100 kB 左右)的前端框架, 基于开源社区流行前端框架编写 amazeUI的网址:http://amazeui.org/get ...
- js默认比较第一个数字大小
解决办法:先把数字用Number转化:num=Number(num);然后再比较 和parseInt区别
- 怎样查出SQLServer的性能瓶颈
怎样查出SQLServer的性能瓶颈 --王成辉翻译整理,转贴请注明出自微软BI开拓者[url]www.windbi.com[/url]--原帖地址 如果你曾经做了很长时间的DBA,那么你会了解到SQ ...
- 爱普生 RS330 打印机墨水连供装置墨盒吸墨复位方法
芯片复位方法: 先按充墨键(墨水灯按键),一下一下按,把墨车按停到右侧换墨盒的位置为止(就是右侧框框正中位置), 全程带电操作,停到换墨盒的位置后再按住芯片复位键(墨盒芯片上面白色的小按键)5秒以上再 ...
- Java中main方面面试题
1.不用main方法如何定义一个类? 不行,没有main方法我们不能运行Java类. 在Java 7之前,你可以通过使用静态初始化运行Java类.但是,从Java 7开始就行不通了. 2.main() ...