[牛客练习赛29D]禁止动规
description
newcoder
你在一个无限长的数轴上,一开始你在原点
本来你只有两种操作:向左dp,以及向右dp
然而由于禁止dp
于是你只能另寻出路
万幸的是,dp之神随机给了你n个变量,既\(x_1,x_2, ... , x_n\),每个变量的值在\([1,m]\)之间,且是整数
每次你可以选择一个变量\(x_i\),然后向左走\(x_i\)个单位,或者向右走\(x_i\)个单位
问走到原点右侧1单位距离的概率是多大?
既随机给定n个变量后,存在至少一种从数轴上的0点走到1点的方案的概率
设答案为\(w\),那么你只需要输出\(w\times m^n\)在模\(2^{64}\)意义下的值
注意:
- 一个变量可以选多次,也可以不选
- 可以走到负半轴
data range
\]
solution
终于学会杜教筛.jpg
我们知道合法方案一定存在两个数\(x_j,x_k\)互质。
考虑容斥减掉\(x_i\)全部为\(k\)的倍数的方案,那么答案即为
\]
数论分块+杜教筛求\(\mu(i)\)的前缀和即可。
时间复杂度为\(O(n^{\frac{2}{3}})\)。
Code
#include<bits/stdc++.h>
#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<iomanip>
#include<cstring>
#include<complex>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cassert>
#include<ctime>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#define F "a"
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define RG register
using namespace std;
typedef unsigned long long ll;
typedef pair<int,int> PI;
typedef vector<int>VI;
//typedef long long ll;
typedef long double dd;
const dd eps=1e-6;
const int mod=1e4;
const int N=2e7+10;
const dd pi=acos(-1);
const int inf=2147483647;
const ll INF=1e18+1;
const ll P=100000;
inline ll read(){
RG ll data=0,w=1;RG char ch=getchar();
while(ch!='-'&&(ch<'0'||ch>'9'))ch=getchar();
if(ch=='-')w=-1,ch=getchar();
while(ch<='9'&&ch>='0')data=data*10+ch-48,ch=getchar();
return data*w;
}
inline void file(){
srand(time(NULL)+rand());
freopen(F".in","r",stdin);
freopen(F".out","w",stdout);
}
int pri[N],mu[N];bool vis[N];
inline void sieve(){
vis[1]=mu[1]=1;
for(RG int i=2;i<N;i++){
if(!vis[i])pri[++pri[0]]=i,mu[i]=-1;
for(RG int j=1;j<=pri[0]&&1ll*i*pri[j]<N;j++){
vis[i*pri[j]]=1;mu[i*pri[j]]=mu[i]*mu[pri[j]];
if(i%pri[j]==0){mu[i*pri[j]]=0;break;}
}
}
for(RG int i=2;i<N;i++)mu[i]+=mu[i-1];
}
inline ll poww(ll a,ll b){
RG ll ret=1;
for(;b;b>>=1,a=a*a)
if(b&1)ret=ret*a;
return ret;
}
ll n,m,ans;
map<ll,ll>premu;
ll getmu(ll n){
if(n<N)return mu[n];
if(premu.find(n)!=premu.end())return premu[n];
ll &res=premu[n];res=1;
for(RG ll i=2,j;i<=n;i=j+1)j=n/(n/i),res-=getmu(n/i)*(j-i+1);
return res;
}
int main()
{
n=read();m=read();sieve();
for(RG ll i=1,j;i<=m;i=j+1)
j=m/(m/i),ans+=(getmu(j)-getmu(i-1))*poww(m/i,n);
printf("%llu\n",ans);return 0;
}
[牛客练习赛29D]禁止动规的更多相关文章
- 【并查集缩点+tarjan无向图求桥】Where are you @牛客练习赛32 D
目录 [并查集缩点+tarjan无向图求桥]Where are you @牛客练习赛32 D PROBLEM SOLUTION CODE [并查集缩点+tarjan无向图求桥]Where are yo ...
- 牛客练习赛31 B 赞迪卡之声妮莎与奥札奇 逻辑,博弈 B
牛客练习赛31 B 赞迪卡之声妮莎与奥札奇 https://ac.nowcoder.com/acm/contest/218/B 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 2621 ...
- 牛客练习赛31 D 神器大师泰兹瑞与威穆 STL,模拟 A
牛客练习赛31 D 神器大师泰兹瑞与威穆 https://ac.nowcoder.com/acm/contest/218/D 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 26214 ...
- 最小生成树--牛客练习赛43-C
牛客练习赛43-C 链接: https://ac.nowcoder.com/acm/contest/548/C 来源:牛客网 题目描述 立华奏是一个刚刚开始学习 OI 的萌新. 最近,实力强大的 ...
- 牛客练习赛28-B(线段树,区间更新)
牛客练习赛28 - B 传送门 题目 qn姐姐最好了~ qn姐姐给你了一个长度为n的序列还有m次操作让你玩, 1 l r 询问区间[l,r]内的元素和 2 l r 询问区间[l,r]内的 ...
- 牛客练习赛26:D-xor序列(线性基)
链接:牛客练习赛26:D-xor序列(线性基) 题意:小a有n个数,他提出了一个很有意思的问题:他想知道对于任意的x, y,能否将x与这n个数中的任意多个数异或任意多次后变为y 题解:线性基 #inc ...
- [堆+贪心]牛客练习赛40-B
传送门:牛客练习赛40 题面: 小A手头有 n 份任务,他可以以任意顺序完成这些任务,只有完成当前的任务后,他才能做下一个任务 第 i 个任务需要花费 x_i 的时间,同时完成第 i 个任务的时间不 ...
- 牛客练习赛 29 E 位运算?位运算!(线段树)
题目链接 牛客练习赛29E 对$20$位分别建立线段树.首先$1$和$2$可以合起来搞(左移右移其实是等效的) 用个lazy标记下.转移的时候加个中间变量. $3$和$4$其实就是区间$01$覆盖操 ...
- 牛客练习赛22C Bitset
牛客练习赛22C 一共有 n个数,第 i 个数是 xi xi 可以取 [li , ri] 中任意的一个值. 设 ,求 S 种类数. 感觉二进制真是一个神奇的东西. #include <iost ...
随机推荐
- crontab执行PHP
在stackoverflow上看到一个问题:http://stackoverflow.com/questions/14015543/crontab-php-wget-or-curl 有三种通过cron ...
- Java String 字符串类细节探秘
一. 字符串基本知识要点 字符串类型String是Java中最常用的引用类型.我们在使用Java字符串的时候,通常会采用两种初始化的方式:1. String str = "Hello Wor ...
- WPF DataGridRow Event
CM(Caliburn.Micro)框架绑定DataGridRow事件 <DataGrid.ItemContainerStyle> <Style TargetType="D ...
- eclipse报这个错误org.eclipse.swt.SWTError: No more handles (eclipse 和 TeamViewer 冲突)
错误: org.eclipse.swt.SWTError: No more handles at org.eclipse.swt.SWT.error(SWT.java:4387) a ...
- 一些窍门 drawable
java.lang.Object android.graphics.drawable.DrawableKnown Direct Subclasses BitmapDrawable, C ...
- 基于Spring的最简单的定时任务实现与配置(二)
接上一篇,原本我以为我实现的方式很简单了,在准备写(一)的时候,就去查了查别人是怎么实现定时任务的.不查还好,这一查,发现还有更简单的.所以就会有这篇文章. 本文主要是讨论,在完成Spring 项目搭 ...
- 不老的神器--namp,awvs
要会使用的工具 NESSUS nmap awvs hydra burpsuit 工具的话,都有文档,应该多使用 -h 多看官方文档,就会用了. 1.namp基本用法 -iL <inputfile ...
- TPO-16 C1 Reserve the room for a rehearsal
TPO-16 C1 Reserve the room for a rehearsal 第 1 段 1.Listen to a conversation between a Student and a ...
- Consul 简介
Consul包含很多组件,总体来数,Consul是一种服务发现和配置工具. 服务发现:一个客户端提供自己的服务,例如api服务,或者mysql服务,另一个客户端就可以利用Consul通过DNS或者ht ...
- 【system.folder】使用说明
对象:system.folder 说明:提供一系列针对文件夹的操作 目录: 方法 返回 说明 system.folder.exists(folderPath) [True | False] 检测指定文 ...