Codeforces Round #774 (Div. 2)

E. Power Board

题目陈述

有一个n×m(1≤n,m≤106)n\times m(1\le n,m\le10^6)n×m(1≤n,m≤106)的表格,第iii行jjj列的数为iji^jij,求表格中不同的数的个数resresres

输入格式

一行包含两个整数n,mn,mn,m

输出格式

表格中不同的数的个数resresres

  • 容易想到,第一行的所有数字都为111
  • 我们依次考虑,以2,3,5,72,3,5,72,3,5,7这样的质数为基底的次方
  • 对于222而言,我们知道222的次方数为2,4,8,16,32,64,⋯2,4,8,16,32,64,\cdots2,4,8,16,32,64,⋯
  • 我们设基底x=2x=2x=2,我们重新把原来的表格中,行的第一个数为222的整次幂的行,抽取出来做成一个表格。新表格的列数都为mmm,但是其行数是不超过nnn的最大的222的整数次幂,即2⌊log⁡2n⌋2^{\lfloor \log _2n \rfloor}2⌊log2​n⌋。故这个新的表格的第iii行的第一列的数即为2i2^i2i,现在我们来计算新表格中不同的数的个数。
  • 我们把新表格第iii行拓展为长度为i×mi\times mi×m的数列,其中在原本行中没有出现的数。打个比方,比如m=3m=3m=3,我们先忽视nnn的值,对于第一行有2,4,82,4,82,4,8,数列长度为m×1=mm\times 1=mm×1=m;对于第二行有2,4,8,16,32,642,4,8,16,32,642,4,8,16,32,64,数列长度为2m2m2m,原本行中的第iii列对应数列中的第2i2i2i项;对于第三行有2,4,8,16,32,64,128,256,5122,4,8,16,32,64,128,256,5122,4,8,16,32,64,128,256,512,数列长度为3m3m3m,原本行中的第iii列对应数列中的第3i3i3i项;以此类推。
  • 这样,第一行就对应111的倍数,第二行就对应222的倍数,第三行就对应333的倍数。我们用状态压缩来枚举某些行al<aq<⋯<apa_l<a_q<\cdots<a_pal​<aq​<⋯<ap​,计算他们在表格中共同出现的数的数量。则这些行下标的最小公倍数LCMLCMLCM,表示在拓展数列中每LCMLCMLCM次共同出现一次,但是有的行的拓展数列长度可能不够长,所以我们要取行下标最小的那个行first_row,其长度为first_row * m,其除以LCM即是枚举的这些行al<aq<⋯<apa_l<a_q<\cdots<a_pal​<aq​<⋯<ap​在表格中共同出现的数的数量。存在边界条件LCM过大的情况,故需要跟m×x⌊log⁡xn⌋+1m\times x^{\lfloor\log _x n\rfloor}+1m×x⌊logx​n⌋+1取min,毕竟当LCM超过了最大的拓展数列长度m×x⌊log⁡xn⌋m \times x^{\lfloor\log _x n\rfloor}m×x⌊logx​n⌋则就没有共同出现的数字了,那么这个LCM就没有意义了,也是为了防止LCM太大爆long long
  • 上面计算的共同出现的数的数量,就相当于容斥原理中集合与集合的交集,根据容斥原理的奇加偶减原则,便容易写出程序(代码参考jiangly)。
#include <bits/stdc++.h>
#include <bits/extc++.h>
#include <unordered_map>
#include <unordered_set>
using namespace std;
using namespace __gnu_cxx;
using namespace __gnu_pbds;#define debug(x) cerr << #x << ": " << x << '\n';
#define bd cerr << "----------------------" << el;
#define el '\n'
#define cl putchar('\n');
#define pb push_back
#define eb emplace_back
#define x first
#define y second
#define rep(i, a, b) for (int i = (a); i <= (b); i++)
#define lop(i, a, b) for (int i = (a); i < (b); i++)
#define dwn(i, a, b) for (int i = (a); i >= (b); i--)
#define ceil(a, b) (a + (b - 1)) / b
#define ms(a, x) memset(a, x, sizeof(a))
#define INF 0x3f3f3f3f
#define db double
#define all(x) x.begin(), x.end()
#define reps(i, x) for (int i = 0; i < x.size(); i++)
#define cmax(a, b) a = max(a, b)
#define cmin(a, b) a = min(a, b)
#define mpa make_pairtypedef long long LL;
typedef long double LD;
typedef pair<int, int> PII;
typedef pair<db, db> PDD;
typedef vector<int> vci;
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> Set;
typedef tree<PII, null_type, less<PII>, rb_tree_tag, tree_order_statistics_node_update> Tree;struct read
{static const int M = 1 << 21;char buf[M], *S = buf, *P = buf, c, f;inline char getc(){return (S == P && (P = (S = buf) + fread(buf, 1, 1 << 21, stdin), S == P) ? EOF : *S++);}template <typename T>read &operator>>(T &x){for (c = 0; !isdigit(c); c = getc())f = c;for (x = 0; isdigit(c); c = getc())x = x * 10 + (c - '0');return x = (f ^ '-') ? x : -x, *this;}
} fin;constexpr int N = 1e5 + 10, M = 2e6 + 10, B = 66, md = 1e9 + 7;
const double PI = acos(-1), eps = 1e-8;int T, n, m;int main()
{fin >> n >> m;LL res = 1; //计算1vector<bool> flag(n + 1, false);rep(i, 2, n){if(flag[i])continue;LL x = i;int lg = 1;while(x * i <= n){lg ++ ;x *= i;flag[x] = true;}//以x为基底//长度分别为m, 2m, 3mlop(s, 1, 1 << lg){//状态压缩枚举LL LCM = 1;int first_row = -1;lop(i, 0, lg){//第i位表示 x ^ (i + 1)if(s >> i & 1){if(first_row == -1)first_row = i + 1;LCM = min(lg * m + 1LL, lcm(LCM, i + 1LL));// min(最长的那一段m, 所有指数的lcm)//lg * m + 1LL是为了防爆LL}}res += (__builtin_parity(s) ? 1 : -1) * (first_row * m / LCM);//容斥,奇加偶减//第一段的长度m * first_row}}cout << res << el;
}

Codeforces Round #774 (Div. 2)E题题解相关推荐

  1. Codeforces Round #774 (Div. 2) Power Board(数学)

    Codeforces Round #774 (Div. 2) Power Board(数学) 链接 题意:给一个n*m的矩阵,每个元素的值是 i j i^j ij,问有多少个不一样的元素 思路:不过这 ...

  2. Codeforces Round 700 (Div. 2) B题 英雄杀怪兽

    Codeforces Round 700 (Div. 2) B题 链接: https://codeforces.com/contest/1480/problem/B 大致意思: n组数据,每组数据的第 ...

  3. Codeforces Round #198 (Div. 2)A,B题解

    Codeforces Round #198 (Div. 2) 昨天看到奋斗群的群赛,好奇的去做了一下, 大概花了3个小时Ak,我大概可以退役了吧 那下面来稍微总结一下 A. The Wall Iahu ...

  4. Codeforces Round #640 (Div. 4)(ABCDEG题解)

    文章目录 A. Sum of Round Numbers B - Same Parity Summands C - K-th Not Divisible by n D - Alice, Bob and ...

  5. 【记录CF】Codeforces Round #777 (Div. 2) A~C 题解

    目录 杂谈 A. Madoka and Math Dad B. Madoka and the Elegant Gift C. Madoka and Childish Pranks 杂谈 又是一场离谱掉 ...

  6. Educational Codeforces Round 110 div.2 A~F题解

    视频讲解:BV1254y137Rn A. Fair Playoff 题目大意 有 444 位选手参加比赛,第 iii 位选手的水平为 si(1≤si≤100)s_i(1 \leq s_i \leq 1 ...

  7. Codeforces Round #636 (Div. 3) ——A. Candies 题解

    题目链接:https://codeforces.com/contest/1343/problem/A 签到题,for循环暴力枚举即可. 代码如下: #include <bits/stdc++.h ...

  8. Codeforces Round #635 (Div. 2)(A~D)题解

    Codeforces #635 A~D A.Ichihime and Triangle B.Kana and Dragon Quest game C.Linova and Kingdom D.Xeni ...

  9. Codeforces Round #784 (Div. 4)#蒻枸题解

    A B 输出出现次数大于3的 数 else -1 C 每次操作 奇数位+1 or 偶数位 +1 使得最后 只有 偶数 或 只有奇数 所以 只要 奇数位 有奇数&& 有偶数 or 偶数位 ...

最新文章

  1. linux mysql清除数据库所有表_MySQL修复指定数据库下的所有表
  2. Linux下MySQL登录报错1045,linux下 root 登录 MySQL 报错的问题
  3. c语言 listview,C语言 SDK编程之通用控件的使用--ListView
  4. Microsfot.Web.UI.WebControls.TreeView JavaScript控制方法研究(转)
  5. PyTorch 深度学习: 60 分钟极速入门
  6. 如何安装 Linux 下的 Adobe Reader
  7. Android开发之PCM音频流采集| 音频流录制与PCM音频流播放的实现方法
  8. Conway#39;s law(康威定律)
  9. java等待_Java学习:等待唤醒机制
  10. 基于PaddleRec的用户点击率预测
  11. js match函数注意
  12. 微信小程序图片全屏显示
  13. Grasshopper and Rhino: Python Scripting Grasshopper和Rhino:Python脚本 Lynda课程中文字幕
  14. 仿京东首页点击轮播图进入唱片页面
  15. Springboot URL Rewrite
  16. mysql+sqlplus命令找不到_oracle sqlplus命令报command not found
  17. ppt convert to html,powerpoint(ppt)
  18. python为什么胶水语言_Python可以干什么?Python胶水语言由来
  19. 联想ThinkPad E431禁用触摸板功能
  20. Verilog设计遇到了Congestion问题怎么办?

热门文章

  1. 利用meshgrid函数绘制二维高斯函数曲面
  2. 论文笔记 EMNLP 2021|Uncertain Local-to-Global Networks for Document-Level Event Factuality Identificatio
  3. 盘点四个Web3社交项目
  4. 铁路计算机联锁系统的应用,铁路信号计算机联锁系统
  5. 临沂鸿蒙文化城在哪,在桑干河南舜都与鸿蒙寺遗址上
  6. STM32如何将文件放到内部flash里面
  7. HTML/ CSS 入门
  8. 2022N1叉车司机考试题及在线模拟考试
  9. Bilateral Upsample
  10. android 拍照 录视频教程,手机如何录制视频,详细录制教程分享给大家