徐州赛区网络预赛 D Easy Math
比赛快结束的适合看了一下D题,发现跟前几天刚刚做过的HDU 5728 PowMod几乎一模一样,当时特兴奋,结果一直到比赛结束都一直WA。回来仔细一琢磨才发现,PowMod这道题保证了n不含平方因子,而Easy Math却没有。只要加一条特判,这道题就过了。
AC代码贴在下面。求莫比乌斯函数前缀和的那一部分是我当时在网上临时找的代码,向那位仁兄道谢。
#include<bits/stdc++.h>
using namespace std;
#define rep(i,a,b) for(int i=a;i<=b;++i)
#define ms(arr,a) memset(arr,a,sizeof arr)
#define debug(x) cout<<"< "#x" = "<<x<<" >"<<endl
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int Maxn = 1e7 * 2;
const int mod = 2333333;
int pcnt = 0, prime[1300000]; // 质数
short mu[Maxn]; // 莫比乌斯函数值
bool vis[Maxn];
void init() {
mu[1] = 1;
for (int i = 2; i < Maxn; i++) {
if (vis[i] == 0) {
mu[i] = -1;
prime[++pcnt] = i;
}
for (int j = 1; j <= pcnt && i * prime[j] < Maxn; j++) {
vis[i * prime[j]] = 1;
if (i % prime[j] != 0)
mu[i * prime[j]] = -mu[i];
else {
mu[i * prime[j]] = 0;
break;
}
}
}
for (int i = 2; i < Maxn; i++) mu[i] += mu[i - 1]; // 函数前缀和
}
struct Hash {
long long key;
int value, next;
} node[mod];
int cnt = 0, Head[mod] = {0};
void Insert(long long N, int v) { // 记忆
int ha = N % mod;
node[++cnt].key = N;
node[cnt].value = v;
node[cnt].next = Head[ha];
Head[ha] = cnt;
}
int M(long long N) {
if (N < Maxn) return mu[N];
int ha = N % mod;
for (int i = Head[ha]; i != 0; i = node[i].next) {
if (node[i].key == N) // 如果已经计算过
{return node[i].value;}
}
int ans = 0;
for (long long i = 2, j; i <= N; i = j + 1) {
j = N / (N / i); // 分块加速
ans += (j - i + 1) * M(N / i);
}
Insert(N, 1 - ans); // 做记忆
return 1 - ans;
}
ll n,m;
long long fac[30];int cntfac;
bool init_fac(ll x)
{
cntfac=0;
for(int i=1;prime[i]<sqrt(x);++i)
if(x%prime[i]==0){fac[++cntfac]=prime[i];x/=prime[i];if(x%prime[i]==0)return false;}
if(x>1)fac[++cntfac]=x;
return true;
}
ll calc(ll n,ll m,int k)
{
//debug(n);
if(m==0)return 0;
if(n==1)
{
if(m==0)return 0;
else {return M(m);}
}
return -calc(n/fac[k],m,k-1)+calc(n,m/fac[k],k);
}
int main()
{
//freopen("Input.txt","r",stdin);
//freopen("1.txt","w",stdout);
init();
while(~scanf("%lld%lld",&m,&n))
{
if(init_fac(n))printf("%lld\n",calc(n,m,cntfac));
else printf("0\n");
}
//freopen("CON","w",stdout);
//system("start Output.txt");
}
徐州赛区网络预赛 D Easy Math的更多相关文章
- [ACM-ICPC 2018 徐州赛区网络预赛][D. Easy Math]
题目链接:Easy Math 题目大意:给定\(n(1\leqslant n\leqslant 10^{12}),m(1\leqslant m\leqslant 2*10^{9})\),求\(\sum ...
- ACM-ICPC 2018 徐州赛区网络预赛(8/11)
ACM-ICPC 2018 徐州赛区网络预赛 A.Hard to prepare 枚举第一个选的,接下来的那个不能取前一个的取反 \(DP[i][0]\)表示选和第一个相同的 \(DP[i][1]\) ...
- 计蒜客 1460.Ryuji doesn't want to study-树状数组 or 线段树 (ACM-ICPC 2018 徐州赛区网络预赛 H)
H.Ryuji doesn't want to study 27.34% 1000ms 262144K Ryuji is not a good student, and he doesn't wa ...
- ACM-ICPC 2018 徐州赛区网络预赛 I. Characters with Hash
Mur loves hash algorithm, and he sometimes encrypt another one's name, and call him with that encryp ...
- ACM-ICPC 2018 徐州赛区网络预赛 B(dp || 博弈(未完成)
传送门 题面: In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl n ...
- ACM-ICPC 2018 徐州赛区网络预赛 B. BE, GE or NE
In a world where ordinary people cannot reach, a boy named "Koutarou" and a girl named &qu ...
- ACM-ICPC 2018 徐州赛区网络预赛 H. Ryuji doesn't want to study
262144K Ryuji is not a good student, and he doesn't want to study. But there are n books he should ...
- ACM-ICPC 2018 徐州赛区网络预赛 F. Features Track
262144K Morgana is learning computer vision, and he likes cats, too. One day he wants to find the ...
- ACM-ICPC 2018 徐州赛区网络预赛 D 杜教筛 前缀和
链接 https://nanti.jisuanke.com/t/31456 参考题解 https://blog.csdn.net/ftx456789/article/details/82590044 ...
随机推荐
- js检查数据类型
在实际工作中我们经常遇到要检测传入的参数类型是什么.也许第一时间想的的是typeof ,但这个也只是能检测个别的一些类型.如果要检测null,Array这些类型呢? 所以我们可以封装一个方法可以更加方 ...
- Redis 笔记(五)—— HASH 常用命令
添加和删除键值对的散列操作 命令 用例和描述 HMGET HMGET key-name key [key ...] —— 从散列里面获取一个或多个键的值 HMSET HMSET key-name ke ...
- C语言变长数组
#include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct Variable ...
- Python中关于字符串你应该知道这些...
# Python中字符串的常见用法### 定义:带有双引号/单引号/三引号### 双引号:适用于所写的字符串里没有双引号的.例如:"凡是“辛苦”必是礼物"报错### 单引号:适用 ...
- buuctf misc wp 01
buuctf misc wp 01 1.金三胖 2.二维码 3.N种方法解决 4.大白 5.基础破解 6.你竟然赶我走 1.金三胖 root@kali:~/下载/CTF题目# unzip 77edf3 ...
- 安装JDK后,未设置Path,也能执行java.exe的原因
安装JDK时,自动将java.exe复制到C:\Windows\System32下
- First Training
B B - Local Extrema CodeForces - 888A You are given an array a. Some element of this array ai is a l ...
- mysql datetime类型 按格式在页面输出
mysql datetime类型对应java Date类型 java.util.Date类型会显示时间戳 java.sql.Date 只显示年月日不显示时分秒 只需要重写get方法 就能按格式输出 ...
- Python——详解__slots__,property和私有方法
本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是Python专题的第11篇文章,我们来聊聊面向对象的一些进阶使用. __slots__ 如果你看过github当中一些大牛的代码,你会 ...
- 基于Lua的游戏服务端框架简介
基于Lua的游戏服务端框架简介 [转]https://gameinstitute.qq.com/community/detail/106396 基于lua的游戏服务端框架简介 1. 引言 笔者目前在参 ...