hdu4059 The Boss on Mars(差分+容斥原理)
题意:
求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和。
知识点:
差分:
一阶差分: 设
则 为一阶差分。
二阶差分:
n阶差分: 且可推出
性质: 1.
2.
差分序列:
给你一列数 a[i][1],a[i][2],a[i][3],a[i][4],a[i][5]……
那么a[i][j]=a[i-1][j+1]-a[i-1][j], 即后一行是上一行相邻两项的差(第一行除外)。
如果给你一个多项式, 比如 f(x)=(x+1)*(x+2)*……*(x+p),即多项式最高项指数为p。
则得到的差分序列有如下性质:
1. f(0),f(1)…f(p)组成多项式的第一行,后面的差分序列可以由上一行推出。第p+1行以后差分序列的值都为0。
2.我们这里要用的差分序列是其第0行对角线的数。 我们设他们为c0、c1、c2、……cp; 则:
第n项的值:f(n)=c0*C(n,0)+c1*C(n,1)+c2*C(n,2)+……+cp*C(n,p);
前n项的值:Sum(n)=c0*C(n+1,1)+c1*C(n+1,2)+c2*C(n+1,3)+……+cp*C(n+1,p+1);
把求前n项和组合公式给化简出来Sum(n)=(n^5)/5+(n^4)/2+(n^3)/3-n/30
=(n*(n+1)*(2n+1)*(3*n*n+3*n-1))/30
题解:反面考虑,容斥原理,sum(n)=1^4+2^4+…n^4=(n*(n+1)*(2n+1)*(3*n*n+3*n-1))/30,减掉与n不互质的数4次方,将n质因子分解后减掉一个因子的倍数的4次方结果,加上两个因子乘积的倍数的4次方结果,减去……以此类推。
其中还涉及逆元,因为MOD为素数,用费马小定理求。
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
typedef long long LL;
const LL MOD=1e9+7;
const LL NN=1e8+5;
const int N=1e4+5;
const LL ni=233333335; //30 mod MOD 的逆
LL n,syz[15],ans;
int ycnt; LL mutisum(LL n)
{
LL ans1=1;
//long long 范围<18,446,744,073,709,551,616 约10^20 30*MOD> LL范围,此法不可,实力被坑
//LL mod=30*MOD;
//ans1=(((((n*(n+1))%mod)*(2*n+1))%mod)*((3*n*n+3*n-1)%mod))%mod;
ans1=(((((((n*(n+1))%MOD)*(2*n+1))%MOD)*((3*n*n+3*n-1)%MOD))%MOD)*ni)%MOD;
//ans1/=30;
return ans1%MOD;
} int prime[N];
bool vis[N];
int pcnt;
void is_prime()
{
pcnt=0;
memset(vis,0,sizeof(vis));
for(int i=2;i<N;i++)
{
if(!vis[i])
{
prime[pcnt++]=i;
for(int j=i+i;j<N;j+=i)
vis[j]=1;
}
}
} void fenjie(LL n1)
{
ycnt=0;
for(int i=0;i<pcnt&&prime[i]<=n1;i++)
{
if(n1%prime[i]==0)
syz[ycnt++]=prime[i];
while(n1%prime[i]==0)
n1/=prime[i];
}
if(n1>1)
syz[ycnt++]=n1;
} void dfs(int c,int cur,int j,LL ans1) //dfs(c,1,0,1);
{
if(cur==c+1)
{
LL nn=(n-1)/ans1,as1=ans1%MOD;
if(c&1)
ans-=(((((((mutisum(nn)*as1)%MOD)*as1)%MOD)*as1)%MOD)*as1)%MOD;
else
ans+=(((((((mutisum(nn)*as1)%MOD)*as1)%MOD)*as1)%MOD)*as1)%MOD;
ans%=MOD;
return;
}
for(;j<ycnt;j++)
{
dfs(c,cur+1,j+1,ans1*syz[j]);
}
} void test()
{
for(int i=0;i<ycnt;i++)
cout<<syz[i]<<' ';
cout<<endl; } int main()
{
int t;
scanf("%d",&t);
is_prime();
while(t--)
{
scanf("%lld",&n);
if(n==1)
{
printf("1\n");
continue;
}
fenjie(n);
ans=mutisum(n-1);
for(int c=1;c<=ycnt;c++)
dfs(c,1,0,1);
if(ans<0)
ans=(ans+MOD)%MOD;
printf("%lld\n",ans);
// test();
}
}
Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u
Description
Due to no moons around Mars, the employees can only get the salaries per-year. There are n employees in ACM, and it’s time for them to get salaries from their boss. All employees are numbered from 1 to n. With the unknown reasons, if the employee’s work number is k, he can get k^4 Mars dollars this year. So the employees working for the ACM are very rich.
Because the number of employees is so large that the boss of ACM must distribute too much money, he wants to fire the people whose work number is co-prime with n next year. Now the boss wants to know how much he will save after the dismissal.
Input
Output
Sample Input
4
5
Sample Output
354
Hint
Case1: sum=1+3*3*3*3=82 Case2: sum=1+2*2*2*2+3*3*3*3+4*4*4*4=354
hdu4059 The Boss on Mars(差分+容斥原理)的更多相关文章
- HDU 4059 The Boss on Mars(容斥原理)
The Boss on Mars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu4059 The Boss on Mars
The Boss on Mars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- hdu4059 The Boss on Mars 容斥原理
On Mars, there is a huge company called ACM (A huge Company on Mars), and it’s owned by a younger bo ...
- 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 Problem's Link Mean: 给定一个整数n,求1~n中所有与n互质的数的四次方的和.(1<=n<=1e8) analyse: 看似简单,倘若 ...
- 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-4509-The Boss on Mars
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4059 题目大意: 给一个n,求1~n中与n互质的数的4次方的总和. 解题思路: 容斥原理.逆元.公式 ...
随机推荐
- 从零开始编写自己的C#框架(22)——添加普通列表页面
普通列表页面指的是上一章那种有层次感列表以外的正常列表页面,由于上一章已讲解了正常添加页面的相关操作了,所以部分相关的操作本章节就不再罗嗦重复一次了.大家可以试试先用本章内容中的一些简单介绍,自己使用 ...
- RXJava by Example--转
原文地址:https://www.infoq.com/articles/rxjava-by-example Key takeaways Reactive programming is a specif ...
- 【Data Cluster】真机环境下MySQL数据库集群搭建
真机环境下MySQL-Cluster搭建文档 摘要:本年伊始阶段,由于实验室对不同数据库性能测试需求,才出现MySQL集群搭建.购置主机,交换机,双绞线等一系列准备工作就绪,也就开始集群搭建.起初笔 ...
- 『.NET Core CLI工具文档』(十二)dotnet-pack
说明:本文是个人翻译文章,由于个人水平有限,有不对的地方请大家帮忙更正. 原文:dotnet-pack 翻译:dotnet-pack 名称 dotnet-pack - 将代码打包成 NuGet 包 概 ...
- html+ccs3太阳系行星运转动画
做一个太阳系八大行星的运转动画,不包括行星的卫星,所有行星围绕太阳公转,行星采用纯色,暂时没有自转. 效果静态图: 动画中包括:太阳及各行星,运行轨道,行星公转动画. 先画好草图,设计好大小和位置,根 ...
- C#中怎样实现序列化和反序列化
我们想要将数据进行持久化的操作的话,也就是将数据写入到文件中,我们在C#中可以通过IO流来操作,同时也可以通过序列化来操作,本人是比较推荐使用序列化操作的 因为我们如果想要将一个对象持久化到文件中 如 ...
- 《连载 | 物联网框架ServerSuperIO教程》- 12.服务接口的开发,以及与云端双向交互
1.C#跨平台物联网通讯框架ServerSuperIO(SSIO)介绍 <连载 | 物联网框架ServerSuperIO教程>1.4种通讯模式机制. <连载 | 物联网框架Serve ...
- Access提示“操作必须使用一个可更新的查询”的解决办法
问题:软件工程师开发了一个asp.net+access网站,本地调试增.删.改和查都没有异常.部署到服务器windows2008 R2的IIS上运行后,查询没有异常.可是在修改操作提交时,产生异常:提 ...
- datagrid与webAPI的数据交互(ef mvc )
datagride自带分页工具,当使用分页工具的时候,初始化datagride或者带数据提交到API里面时,会以Json对象的形式将数据传递到API控制器里面,当没有过滤条件或者请求参数.和提交参数的 ...
- 浅谈Hybrid技术的设计与实现第三弹——落地篇
前言 接上文:(阅读本文前,建议阅读前两篇文章先) 浅谈Hybrid技术的设计与实现 浅谈Hybrid技术的设计与实现第二弹 根据之前的介绍,大家对前端与Native的交互应该有一些简单的认识了,很多 ...