题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2005

  题意:题目转换后的模型就是求Σ(gcd(x,y)), 1<=x<=n, 1<=y<=m。。

  容易想到n^2logn的方法,ΣΣ(gcd(x,y)*2-1),但是这里会超时,因此我们需要优化。我们令f[d]表示(x,y),1<=x<=n, 1<=y<=m的所有对数中gcd(x,y)=d的个数,那么容易求出所有对数中(x,y)的约数为d的个数为(n/d)*(m/d),然后减去f[i*d],i>=2就行了...

 //STATUS:C++_AC_16MS_2052KB
#include <functional>
#include <algorithm>
#include <iostream>
//#include <ext/rope>
#include <fstream>
#include <sstream>
#include <iomanip>
#include <numeric>
#include <cstring>
#include <cassert>
#include <cstdio>
#include <string>
#include <vector>
#include <bitset>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <list>
#include <set>
#include <map>
using namespace std;
//#pragma comment(linker,"/STACK:102400000,102400000")
//using namespace __gnu_cxx;
//define
#define pii pair<int,int>
#define mem(a,b) memset(a,b,sizeof(a))
#define lson l,mid,rt<<1
#define rson mid+1,r,rt<<1|1
#define PI acos(-1.0)
//typedef
typedef __int64 LL;
typedef unsigned __int64 ULL;
//const
const int N=;
const int INF=0x3f3f3f3f;
const int MOD=,STA=;
const LL LNF=1LL<<;
const double EPS=1e-;
const double OO=1e15;
const int dx[]={-,,,};
const int dy[]={,,,-};
const int day[]={,,,,,,,,,,,,};
//Daily Use ...
inline int sign(double x){return (x>EPS)-(x<-EPS);}
template<class T> T gcd(T a,T b){return b?gcd(b,a%b):a;}
template<class T> T lcm(T a,T b){return a/gcd(a,b)*b;}
template<class T> inline T lcm(T a,T b,T d){return a/d*b;}
template<class T> inline T Min(T a,T b){return a<b?a:b;}
template<class T> inline T Max(T a,T b){return a>b?a:b;}
template<class T> inline T Min(T a,T b,T c){return min(min(a, b),c);}
template<class T> inline T Max(T a,T b,T c){return max(max(a, b),c);}
template<class T> inline T Min(T a,T b,T c,T d){return min(min(a, b),min(c,d));}
template<class T> inline T Max(T a,T b,T c,T d){return max(max(a, b),max(c,d));}
//End LL f[N];
int n,m; int main(){
freopen("in.txt","r",stdin);
int i,j,low;
LL ans;
scanf("%d%d",&n,&m);
low=Min(n,m);
ans=;
for(i=low;i>;i--){
f[i]=(LL)(n/i)*(m/i);
for(j=i+i;j<=low;j+=i)f[i]-=f[j];
ans+=f[i]*(i*-);
}
printf("%lld\n",ans);
return ;
}

Bzoj-2005 能量采集 gcd,递推的更多相关文章

  1. BZOJ 2005 能量采集

    Description 栋栋有一块长方形的地,他在地上种了一种能量植物,这种植物可以采集太阳光的能量.在这些植物采集能量后,栋栋再使用一个能量汇集机器把这些植物采集到的能量汇集到一起. 栋栋的植物种得 ...

  2. BZOJ 2005 能量采集(容斥原理)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=2005 题意:给定n和m,求 思路:本题主要是解决对于给定的t,有多少对(i,j)满足x= ...

  3. bzoj 2005 能量采集 莫比乌斯反演

    我们要求的是∑ni=1∑mj=1(2×gcd(i,j)−1) 化简得2×∑ni=1∑mj=1gcd(i,j)−n×m 所以我们现在只需要求出∑ni=1∑mj=1gcd(i,j)即可 ∑ni=1∑mj= ...

  4. 【BZOJ】1002: [FJOI2007]轮状病毒 递推+高精度

    1002: [FJOI2007]轮状病毒 Description 给定n(N<=100),编程计算有多少个不同的n轮状病毒. Input 第一行有1个正整数n. Output 将编程计算出的不同 ...

  5. BZOJ 3329: Xorequ(数位dp+递推)

    传送门 解题思路 可以把原式移项得\(x\)^\(2x\)=\(3x\),而\(x+2x=3x\),说明\(x\)二进制下不能有两个连续的\(1\).那么第一问就是一个简单的数位\(dp\),第二问考 ...

  6. 洛谷 1447 [NOI2010]能量采集——容斥/推式子

    题目:https://www.luogu.org/problemnew/show/P1447 1.容斥原理 求 f [ i ] 表示 gcd==i 的对数,先 f [ i ] = (n/i) * (m ...

  7. bzoj2005 能量采集 gcd 容斥

    ans = sigma_x(sigma_y(gcd(x,y) * 2 - 1)),1<=x<=n,1<=y<=m 枚举x,y,O(nmlogn),超时 换个角度,枚举d = g ...

  8. BZOJ 3930: [CQOI2015]选数 递推

    3930: [CQOI2015]选数 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/pro ...

  9. BZOJ 1177 [Apio2009]Oil(递推)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1177 [题目大意] 给出一个矩阵,从中选出3个k*k且不相交的矩阵,使得其总和最大 [ ...

随机推荐

  1. Java 程序检查远程服务器状态

    通常我们以命令的方式判断远程服务器是否正常运行有两种方式,ping 或 telnet 一个远程端口.假设我们要检查的远程服务器都是 Linux 系统. 从 JDK 1.5 以后, InetAddres ...

  2. linux bash_profile和.bashrc区别

    经常在一些技术类的文章中提到修改bash_profile和.bashrc这两个文件,也算是使用频率比较高的两个文件吧,但实现同样一个功能,有的教程里说修改bash_profile这个文件,有的教程里却 ...

  3. dtp--eclipse的安装数据源管理的一个插件的安装方法

    1.  下载eclipse dtp 插件 http://download.eclipse.org/datatools/updates/1.11 help——>install new softwa ...

  4. 思科模拟器软件教程---教你如何划分Vlan

    方法/步骤 1.打开Cisco Packet Tracer,点击[交换机],选择第三个图标2960交换机,按住鼠标左键拖动到工作区.这里有很多类型的交换机,但是我们比较常用的是这个. 2.我们选择[终 ...

  5. 通过dbms_xplan.display_cursor识别低效的执行计划

    dbms_xplan.display_cursor定义: function display_cursor(sql_id           varchar2 default  null,        ...

  6. WCF 传输的序列化

    // 注意: 使用“重构”菜单上的“重命名”命令,可以同时更改代码和配置文件中的接口名“IService”.[ServiceContract]public interface IService{ [O ...

  7. unite

    列出某个集合里的项目,比如file,buffer等 :United file——列出文件 :United buffer——列出buffer :United file_rec——递归列出文件 进入Uni ...

  8. RabbitMQ消息队列(一): Detailed Introduction 详细介绍(转)

    1. 历史 RabbitMQ是一个由erlang开发的AMQP(Advanced Message Queue )的开源实现.AMQP 的出现其实也是应了广大人民群众的需求,虽然在同步消息通讯的世界里有 ...

  9. default parameter value for ‘color’ must be a compile-time constant

    定义了一个函数,函数有一个参数是Color类型的可选参数,想要设置其默认值为Color.Black http://stackoverflow.com/questions/2804395/c-sharp ...

  10. 代理服务器squid

    http://www.baidu.com/s?wd=squid%E4%BB%A3%E7%90%86%E6%9C%8D%E5%8A%A1%E5%99%A8&f=12&rsp=0& ...