HDU - 4059: The Boss on Mars (容斥 拉格朗日 小小的优化搜索)
pro: T次询问,每次给出N(N<1e8),求所有Σi^4 (i<=N,且gcd(i,N)==1) ;
sol: 因为N比较小,我们可以求出素因子,然后容斥。 主要问题就是求1到P的4次幂和。 我们知道K次幂和是一个K+1次多项式。
这里有公式Pre=P*(P+1)*(2P+1)*(3P^2+3P-1)/30; 在不知道公式的情况下,我们可以用拉格朗日差值法求。
1,下面给出DFS容斥的代码 。
#include<bits/stdc++.h>
#define ll long long
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
const ll Mod=1e9+;
int p[maxn],tot,N,T,ans,Rev=;
void init()
{
ll tN=N; tot=; ans=;
for(int i=;i<=tN/i;i++){
if(tN%i==){
p[++tot]=i;
while(tN%i==) tN/=i;
}
}
if(tN>) p[++tot]=tN;
}
int get(ll y)
{
ll res=y*(y+)%Mod;
res=(y*+)%Mod*res%Mod;
res=(y*y%Mod*%Mod+(y*-)%Mod)*res%Mod;
res=res*Rev%Mod;
return res;
return 1LL*y*(y+)%Mod*(y*%Mod+)%Mod*((3LL*y%Mod*y%Mod+y*%Mod-+Mod)%Mod)%Mod*Rev%Mod;
}
void dfs(int pos,int opt,ll sum)
{
if(pos==tot+){
ll t=1LL*sum*sum%Mod*sum%Mod*sum%Mod;
ll x=get(N/sum);
x=x*t%Mod;
if(opt==) ans=(ans+x)%Mod;
else ans=((ans-x)%Mod+Mod)%Mod;
return ;
}
dfs(pos+,opt,sum);
dfs(pos+,-opt,sum*p[pos]);
}
int main()
{
scanf("%d",&T);
while(T--){
scanf("%d",&N);
init();
dfs(,,1LL);
printf("%d\n",ans);
}
return ;
}
2,这里的DFS还可以小小的优化一下, 想象一下,DFS的过程是遍历一颗二叉树,那么它遍历了所有的节点, 而且遍历的过程是老老实实一步步走下去的,所以还可以优化一下。 假设不操作则加入左儿子,有操作进入右儿子。 由于每次我遍历到叶子节点才进行计算, 所以很多时候,我向左走其实进行了一些没有价值的访问,会浪费一些时间。
而我现在可以在非叶子节点就进行计算,非叶子节点代表的是一直左走代表的叶子(即用节点代表对应的叶子,减少了不必要的访问)。 这样的话,不会存在浪费。 (虽然在此题中未体现出优劣性,但是去年深圳热身赛有一道搜索题就需要这样才能过。
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
const int Mod=1e9+;
int p[maxn],tot,N,T,ans,Rev=;
void init()
{
int tN=N; tot=; ans=;
for(int i=;i*i<=tN;i++){
if(tN%i==){
p[++tot]=i;
while(tN%i==) tN/=i;
}
}
if(tN>) p[++tot]=tN;
}
void dfs(int pos,int opt,int sum) //稍微优化后的DFS
{
int t=1LL*sum*sum%Mod*sum%Mod*sum%Mod;
int y=N/sum;
int x=1LL*y*(y+)%Mod*(y*+)%Mod*(1LL*y*y*%Mod+y*-)%Mod*Rev%Mod;
if(opt==) (ans+=1LL*x*t%Mod)%=Mod;
else ((ans-=1LL*x*t%Mod)+=Mod)%=Mod;
for(int i=pos+;i<=tot;i++){
dfs(i,-opt,sum*p[i]);
}
}
int main()
{
scanf("%d",&T);
while(T--){
scanf("%d",&N);
init();
dfs(,,);
printf("%d\n",ans);
}
return ;
}
3,以及把公式改为拉格朗日差值来求的代码,4次多项式,前缀和为5次多项式,可以通过求前6项来求:(当然这里的拉格朗日还可以优化为O(N),我懒得改了
#include<bits/stdc++.h>
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
const int maxn=;
const int Mod=1e9+;
int X[maxn]={,,,,,,},Y[maxn]={,,,,,,};
int p[maxn],tot,N,T,ans,Rev=;
int qpow(int a,int x)
{
int res=; while(x){
if(x&) res=1LL*res*a%Mod;
x>>=; a=1LL*a*a%Mod;
}
return res;
}
int Lange(int K)
{
int res=;
rep(i,,) {
int tmp=Y[i];
rep(j,,) {
if(j==i) continue;
tmp=1LL*tmp*(K-X[j])%Mod*qpow(X[i]-X[j],Mod-)%Mod;
tmp=(tmp+Mod)%Mod;
}
(res+=tmp)%=Mod;
}
return res;
}
void init()
{
int tN=N; tot=; ans=;
for(int i=;i*i<=tN;i++){
if(tN%i==){
p[++tot]=i;
while(tN%i==) tN/=i;
}
}
if(tN>) p[++tot]=tN;
}
void dfs(int pos,int opt,int sum) //稍微优化后的DFS
{
int t=1LL*sum*sum%Mod*sum%Mod*sum%Mod;
int y=N/sum;
int x=Lange(y);
if(opt==) (ans+=1LL*x*t%Mod)%=Mod;
else ((ans-=1LL*x*t%Mod)+=Mod)%=Mod;
for(int i=pos+;i<=tot;i++){
dfs(i,-opt,sum*p[i]);
}
}
int main()
{
scanf("%d",&T);
while(T--){
scanf("%d",&N);
init();
dfs(,,);
printf("%d\n",ans);
}
return ;
}
HDU - 4059: The Boss on Mars (容斥 拉格朗日 小小的优化搜索)的更多相关文章
- hdu 4059 The Boss on Mars 容斥
题目链接 求出ai^4+a2^4+......an^4的值, ai为小于n并与n互质的数. 用容斥做, 先求出1^4+2^4+n^4的和的通项公式, 显然是一个5次方程, 然后6个方程6个未知数, 我 ...
- 数论 + 容斥 - HDU 4059 The Boss on Mars
The Boss on Mars Problem's Link Mean: 给定一个整数n,求1~n中所有与n互质的数的四次方的和.(1<=n<=1e8) analyse: 看似简单,倘若 ...
- hdu 4059 The Boss on Mars
The Boss on Mars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4059 The Boss on Mars 容斥原理
The Boss on Mars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4059 The Boss on Mars(容斥原理 + 四次方求和)
传送门 The Boss on Mars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- HDU 4059 The Boss on Mars(容斥原理)
The Boss on Mars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu 4059 The Boss on Mars(纳入和排除)
http://acm.hdu.edu.cn/showproblem.php?pid=4059 定义S = 1^4 + 2^4 + 3^4+.....+n^4.如今减去与n互质的数的4次方.问共降低了多 ...
- bzoj4559[JLoi2016]成绩比较 容斥+拉格朗日插值法
4559: [JLoi2016]成绩比较 Time Limit: 20 Sec Memory Limit: 256 MBSubmit: 261 Solved: 165[Submit][Status ...
- hdu 5792(树状数组,容斥) World is Exploding
hdu 5792 要找的无非就是一个上升的仅有两个的序列和一个下降的仅有两个的序列,按照容斥的思想,肯定就是所有的上升的乘以所有的下降的,然后再减去重复的情况. 先用树状数组求出lx[i](在第 i ...
随机推荐
- pod的时区问题
1制作image时进行配置修改 2将宿主机的时区配置文件挂载到pod中(此处注意,宿主机之间需已经完成时间同步) volumeMounts: - name: host-time mountpath: ...
- STL之iterator源码解析
摘要 迭代器是一种行为类似指针的对象,本文介绍iterator与traits的关系,以及对traits内容的补充.包含stl_iterator.h的部分内容,并用c++11对其进行略微改写. iter ...
- 【JavaScript】从 this 指向到 reference 类型
判断"this 指向谁"是个老大难的问题. 网络上有许多文章教我们如何判别,但大多艰涩复杂,难以理解. 那么这里介绍一个非常简单实用的判别规则: 1)在函数[调用]时," ...
- Referenced file contains errors (http://www.springframework.org/...解决
今天打开老项目出现如下错误: Referenced file contains errors (http://www.springframework.org/schema/context/spring ...
- java 字符串转json,json转实体对象、json字符串转换成List、List转String、以及List排序等等...
@RequestMapping(value = "updateInvestorApplyAccountNo", method = RequestMethod.POST) @Resp ...
- 超级简单POI导出Excel实战
在一般的生产管理系统都会将数据通过页面导出到Excel,这里以Java为例通过第三方开源poi进行对Excel的操作,具体操作如下 1.引入jar包依赖 这里我以maven的方式引入jar包,具体依赖 ...
- day50——js补充
day50 前端内容回顾 HTML 标签分类 块级标签:div p h1-h6 form hr br ul li ol table标签 内联标签:span a img label input sele ...
- 【Rust】使用cargo创建项目及cargo源的替换
---------------------------------参考文档------------------------------- https://rustlang-cn.org/office/ ...
- Django开发之登陆和登出
使用django自带的验证模块 1.首先使用python manage.py startapp models为当前项目添加一个应用. 2.在setting.py中INSTALLED_APPS后面添加' ...
- Java 中 Hashtable与HashMap的区别
Map Map是一个以键值对存储的接口.Map下有两个具体的实现,分别是HashMap和HashTable. 区别: 1.HashMap是线程非安全的,HashTable是线程安全的,所以HashMa ...