当前位置: 首页 > news >正文

新视野OJ 2005 [Noi2010]能量采集 (数论-gcd)

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2005


题解:

设f[i]表示gcd(x,y)=i 的个数(1<=x<=n,1<=y<=m),那么最后的结果就是,其中n=min(n,m)。

那么现在关键就是求解f[i]了。其中gcd(x,y)=i的倍数为[n/i]*[m/i],但是这个包括了i的倍数,所以-2i-3i-……。

为了避免算术,我们逆过来求就行了。


AC代码:

2005Accepted2052kb 48ms C++/Edit
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <list>
#include <deque>
#include <queue>
#include <iterator>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <cctype>
using namespace std;

#define si1(a) scanf("%d",&a)
#define si2(a,b) scanf("%d%d",&a,&b)
#define sd1(a) scanf("%lf",&a)
#define sd2(a,b) scanf("%lf%lf",&a,&b)
#define ss1(s)  scanf("%s",s)
#define pi1(a)    printf("%d\n",a)
#define pi2(a,b)  printf("%d %d\n",a,b)
#define mset(a,b)   memset(a,b,sizeof(a))
#define forb(i,a,b)   for(int i=a;i<b;i++)
#define ford(i,a,b)   for(int i=a;i<=b;i++)

typedef long long LL;
const int N=100005;
const int INF=0x3f3f3f3f;
const double PI=acos(-1.0);
const double eps=1e-7;


LL f[N];

int main()
{
    LL n,m;
    while(cin>>n>>m)
    {
        mset(f,0);
        if(n>m) swap(n,m);
        LL sum=0;
        for(LL i=n;i>=1;i--)
        {
            f[i]=(n/i)*(m/i);
            for(int j=i+i;j<=n;j+=i)
                f[i]-=f[j];
            sum+=f[i]*(2*i-1);
        }
        cout<<sum<<endl;
    }
    return 0;
}


相关文章:

  • Python 入门教程 17 ---- Introduction to Classes
  • HDU 4288 Coder 【线段树+离线处理+离散化】
  • 近期刷题的c语言总结。
  • 程序员看婚姻
  • Python 入门教程 18 ---- File Input/Output
  • 【职业】致迷茫的大学生们
  • (poj1.3.2)1791(构造法模拟)
  • 微软云技术Windows Azure专题(五):如何将WCF服务部署在Windows Azure上
  • 『Asp.Net 组件』Asp.Net 服务器组件 内嵌JS:让自己的控件动起来
  • 『Asp.Net 组件』第一个 Asp.Net 服务器组件:自己的文本框控件
  • 『Asp.Net 组件』Asp.Net 服务器组件 内嵌图片:自己的图片控件
  • 『Asp.Net 组件』Asp.Net 服务器组件 内嵌CSS:将CSS封装到程序集中
  • 『Asp.Net 组件』Asp.Net 服务器组件 的开发优势和劣势
  • Linux系统中的文件目录介绍——Linux system files in the directory structure is introduced
  • 『开源』字符串匹配引擎
  • [分享]iOS开发-关于在xcode中引用文件夹右边出现问号的解决办法
  • 「面试题」如何实现一个圣杯布局?
  • 【5+】跨webview多页面 触发事件(二)
  • 【React系列】如何构建React应用程序
  • 03Go 类型总结
  • AHK 中 = 和 == 等比较运算符的用法
  • ES2017异步函数现已正式可用
  • Gradle 5.0 正式版发布
  • JAVA 学习IO流
  • Javascript基础之Array数组API
  • Java小白进阶笔记(3)-初级面向对象
  • Laravel5.4 Queues队列学习
  • Leetcode 27 Remove Element
  • leetcode378. Kth Smallest Element in a Sorted Matrix
  • Objective-C 中关联引用的概念
  • Python 反序列化安全问题(二)
  • Windows Containers 大冒险: 容器网络
  • 安卓应用性能调试和优化经验分享
  • 百度贴吧爬虫node+vue baidu_tieba_crawler
  • 半理解系列--Promise的进化史
  • 从地狱到天堂,Node 回调向 async/await 转变
  • 基于Dubbo+ZooKeeper的分布式服务的实现
  • 区块链分支循环
  • 使用Swoole加速Laravel(正式环境中)
  • 详解移动APP与web APP的区别
  • TPG领衔财团投资轻奢珠宝品牌APM Monaco
  • 哈罗单车融资几十亿元,蚂蚁金服与春华资本加持 ...
  • #DBA杂记1
  • #define与typedef区别
  • #控制台大学课堂点名问题_课堂随机点名
  • #快捷键# 大学四年我常用的软件快捷键大全,教你成为电脑高手!!
  • (06)金属布线——为半导体注入生命的连接
  • (react踩过的坑)Antd Select(设置了labelInValue)在FormItem中initialValue的问题
  • (附源码)ssm捐赠救助系统 毕业设计 060945
  • (九)c52学习之旅-定时器
  • (三分钟了解debug)SLAM研究方向-Debug总结
  • (五) 一起学 Unix 环境高级编程 (APUE) 之 进程环境
  • (转)平衡树
  • (转)视频码率,帧率和分辨率的联系与区别
  • .net FrameWork简介,数组,枚举