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次方的总和. 解题思路: 容斥原理.逆元.公式 ...
随机推荐
- Boostrap入门(一)
1.第一步:下载Boostrap有关文件 Boostrap中文网:http://www.bootcss.com/ -->--> 2.第二步: 如下代码:引入相关文件即可. <!DOC ...
- springboot(九):定时任务
在我们的项目开发过程中,经常需要定时任务来帮助我们来做一些内容,springboot默认已经帮我们实行了,只需要添加相应的注解就可以实现 1.pom包配置 pom包里面只需要引入springboot ...
- c#编程基础之字符串函数
c#常用的字符串函数 例一: 获取字符串的大小写函数 ToLower():得到字符串的小写形式 ToUpper():得到字符串的大写形式 注意: 字符串时不可变的,所以这些函数都不会直接改变字符串的内 ...
- HTML光标样式
HTML光标样式 把你的光标放到相应文字上鼠标显示效果 cursor:auto; 自动 cursor:zoom-in; 放大镜 cursor:zoom-out; 缩小镜 curs ...
- 利用KD树进行异常检测
软件安全课程的一次实验,整理之后发出来共享. 什么是KD树 要说KD树,我们得先说一下什么是KNN算法. KNN是k-NearestNeighbor的简称,原理很简单:当你有一堆已经标注好的数据时,你 ...
- IIS 如何设置多个Access-Control-Allow-Origin
1,跨域请求ajax,可以增加请求Header,动态添加 System.Collections.Generic.List<string> lHost = new System.Collec ...
- wpf 列表、菜单 收起与展开,通过Grid DoubleAnimation或者Expander实现
菜单收缩有很多种方法具体如何实现还是看个人想法: 第一种通过后台控制收起与展开: 效果图: 代码 : <Grid> <Grid.ColumnDefinitions> <C ...
- Interview website
https://www.interviewcake.com http://www.leetcode.com
- JavaWeb_day06_Filter过滤器
本文为博主辛苦总结,希望自己以后返回来看的时候理解更深刻,也希望可以起到帮助初学者的作用. 转载请注明 出自 : luogg的博客园 谢谢配合! day06 request 对象常用方法 respon ...
- 运行第一个PHP程序
由于PHP比较简单,所以闲来无事学习一下PHP的程序. 之前安装XAMPP出现各种错误,于是下载了PHPStudy,真的十分简单方便,感谢网站开发者. 可以在网站的软件下载中下载,附上首页链接:htt ...