徐州赛区网络预赛 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 ...
随机推荐
- iOS - scrollView与headerView的视差滚动实现思路
假设场景:viewController里面有一个scrollView,该scrollView有一个headerView.现在需要将scrollView的滚动contentOffset与headerVi ...
- C++中的字符串切片操作
string str = "hello"; str.substr(0,2); //输出"he", 表示[0,2)
- JS/Jquery遍历JSON对象、JSON数组、JSON数组字符串、JSON对象字符串
JS遍历JSON对象 JS遍历JSON对象 <script> var obj = { "goodsid": "01001", "goods ...
- 整数逆序输出 Python
输入形式:123 输出形式:321 输入形式:120 输出形式:21 (整数不能以0打头) 输入形式:-123 输出形式:-321 代码: a=int(input()) b=0 if a<0 ...
- php--一些有用的Laravel辅助函数
str_start()/str_finish() 将指定值添加到字符串的开头/结尾(当不是以该值开头/结尾时) blank() 判断给定的值是否为「空」 collect() 根据给定的数组创建一个集合 ...
- word多级列表应用
- "视频播放器"组件:<video-player> —— 快应用组件库H-UI
 <import name="video-player" src="../Common/ui/h-ui/media/c_video_player"> ...
- Linux学习6-安装Python3.6
前言 今天我们来学习一下如何在Linux环境下安装Python3.6吧! 一:下载Python3.6(我下载的是python3.6.8) 地址:https://www.python.org/downl ...
- Apache SkyWalking
Apache SkyWalking 什么是 SkyWalking SkyWalking 是观察性分析平台和应用性能管理系统. 提供分布式追踪.服务网格遥测分析.度量聚合和可视化一体化解决方案. 支持J ...
- 转载:URL链接中的不同用处
,井号:表示网页中的一个位置,被称之为锚点,常用于某个网页间不同位置的跳转,简单的说就是在一个网页中,URL 不变的情况下,通过添加"#buy"的字符在 URL 最后可以跳转到当前 ...