http://acm.hdu.edu.cn/showproblem.php?pid=4059

定义S = 1^4 + 2^4 + 3^4+.....+n^4。如今减去与n互质的数的4次方。问共降低了多少。

容斥原理。能够先把与n不互质的数的4次方求出来。那就先对n进行质因子分解,对质因子的组合运用容斥原理。质因子个数为奇数就加,偶数就减。事实上与求[1,n]内与n互质的数的个数类似,该题重点是计算,防止乘法溢出。

对于求解1^4 + 2^4 + 3^4+.....+n^4,能够先类比1^2+2^2+...+n^2的求法,那么求4次方,

首先(n+1)^5= n^5 + 5*n^4 + 10*n^3 + 10*n^2 + 5*n^1 + 1.

那么2^5 = (1+1)^5 = 1^5 + 5*1^4 + 10*1^3 + 10*1^2 + 5*1^1 + 1.

3^5 = (2+1)^5 = 2^5 + 5*2^4 + 10*2^3 + 10*2^2 + 5*2^1 + 1.

........

........

(n+1)^5 = n^5 + 5*n^4 + 10*n^3 + 10*n^2 + 5*n^1 + 1.

将上述全部等式相加。两边抵消同样项,得到(n+1)^5 = 5*(1^4+2^4+……n^4)+10*(1^3+2^3+……+n^3)+10*(1^2+2^2+……+n^2)+5*(1+2+……+n)+n+1,

将1^3+2^3+……+n^3 = (n+1)^2*n^2/4和1^2+2^2+……+n^2
= (n*(n+1)*(2*n+1))/6带入上式,化简得到:

1^4+2^4+……n^4
= (n*(n+1)*(2n+1)*(3*n*n+3*n-1))/30。

由于要取余,要求30对1000000007的逆元,用扩展欧几里得就可以。

#include <stdio.h>
#include <iostream>
#include <map>
#include <set>
#include <bitset>
#include <list>
#include <stack>
#include <vector>
#include <math.h>
#include <string.h>
#include <queue>
#include <string>
#include <stdlib.h>
#include <algorithm>
#define LL __int64
//#define LL long long
#define eps 1e-9
#define PI acos(-1.0)
using namespace std;
const int maxn = 10010;
const LL mod = 1000000007;
LL n;
int fac[maxn];
int facCnt;
int prime[maxn];
LL ni,nii; //求30对mod的逆元。
LL extend_gcd(LL a, LL b, LL &x, LL &y)
{
if(b == 0)
{
x = 1;
y = 0;
return a;
}
LL d = extend_gcd(b,a%b,x,y);
LL t = x;
x = y;
y = t-a/b*y;
return d;
} LL pow_4(LL t)
{
LL anw =( ((t*(t+1))%mod*(2*t+1)%mod) * (((3*t*t)%mod+(3*t)%mod-1+mod)%mod )%mod*ni)%mod;
return anw;
} LL cal(LL m)
{
LL t = n/m;
LL anw1 = m;
anw1 = (anw1*m)%mod;
anw1 = (anw1*m)%mod;
anw1 = (anw1*m)%mod;
LL anw2 = pow_4(t);
LL anw = (anw1*anw2)%mod;
return anw;
} void getPrime()
{
bool flag[maxn];
memset(flag,false,sizeof(flag));
prime[0] = 0;
for(int i = 2; i < maxn; i++)
{
if(flag[i] == false)
{
prime[++prime[0]] = i;
for(int j = 1; j <= prime[0]&&prime[j]*i < maxn; j++)
{
flag[prime[j]*i] = true;
if(i%prime[j] == 0)
break;
}
}
}
} void getFac()
{
facCnt = 0;
LL tmp = n;
for(int i = 1; i <= prime[0] && prime[i]*prime[i] <= tmp; i++)
{
if(tmp % prime[i] == 0)
{
fac[facCnt++] = prime[i];
while(tmp % prime[i] == 0)
tmp /= prime[i];
}
if(tmp == 1) break;
}
if(tmp > 1)
fac[facCnt++] = tmp;
} int main()
{
int test;
scanf("%d",&test);
getPrime();
extend_gcd(30,mod,ni,nii);
while(test--)
{
scanf("%I64d",&n);
getFac();
LL ans = 0; for(int i = 1; i < (1<<facCnt); i++)
{
LL mul = 1;
int cnt = 0;
for(int j = 0; j < facCnt; j++)
{
if(i&(1<<j))
{
cnt++;
mul *= fac[j];
}
}
if(cnt&1)
ans = (ans + cal(mul) )%mod;
else
ans = (ans - cal(mul) )%mod;
}
ans = ((pow_4(n) - ans)%mod+mod)%mod; //减法时取余
printf("%I64d\n",ans);
}
return 0;
}

hdu 4059 The Boss on Mars(纳入和排除)的更多相关文章

  1. HDU 4059 The Boss on Mars 容斥原理

    The Boss on Mars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  2. HDU 4059 The Boss on Mars(容斥原理 + 四次方求和)

    传送门 The Boss on Mars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Oth ...

  3. 数论 + 容斥 - HDU 4059 The Boss on Mars

    The Boss on Mars Problem's Link Mean: 给定一个整数n,求1~n中所有与n互质的数的四次方的和.(1<=n<=1e8) analyse: 看似简单,倘若 ...

  4. HDU 4059 The Boss on Mars(容斥原理)

    The Boss on Mars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  5. hdu 4059 The Boss on Mars

    The Boss on Mars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

  6. hdu 4059 The Boss on Mars 容斥

    题目链接 求出ai^4+a2^4+......an^4的值, ai为小于n并与n互质的数. 用容斥做, 先求出1^4+2^4+n^4的和的通项公式, 显然是一个5次方程, 然后6个方程6个未知数, 我 ...

  7. hdu4059 The Boss on Mars(差分+容斥原理)

    题意: 求小于n (1 ≤ n ≤ 10^8)的数中,与n互质的数的四次方和. 知识点: 差分: 一阶差分: 设  则    为一阶差分. 二阶差分: n阶差分:     且可推出    性质: 1. ...

  8. HDU 4059 容斥原理+快速幂+逆元

    E - The Boss on Mars Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64 ...

  9. The Boss on Mars

    The Boss on Mars Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) ...

随机推荐

  1. 委托、Lambda和事件

    委托 委托相当于C语言当中的函数指针,不过委托是类型安全的类,它定义了返回类型和参数的类型. 声明委托 在C#中使用一个类,分为两个阶段.首先,需要定义这个类,告诉编译器这个类由什么字段和方法组成,然 ...

  2. Groovy常见语法汇总

    一.groovy是什么 简单地说,Groovy 是下一代的java语言,跟java一样,它也运行在 JVM 中. 作为跑在JVM中的另一种语言,groovy语法与 Java 语言的语法很相似.同时,G ...

  3. CodeIgniter Apacheserver htaccess SEO重写写法

    1)支持重写 2)兼容全部APACHE server 3)假设不是文件,请求的结尾不包含反斜杠,自己主动跳转到反斜杠 4)文件名称 .htaccess 5)这个放到nginxserver怎么都不支持重 ...

  4. 批处理程序:自动上传FTP,再登陆linux服务器执行更新

    ----转载请注明出处:博客园-邦邦酱好 最近在学批处理程序,一直没找到能够结合工作来使用它的地方,前几天测试服务端的时候突然想到可以这样做: 写一个批处理程序,自动通过FTP上传软件包到指定的地方, ...

  5. Kafka学习(一)配置及简单命令使用

    一. Kafka中的相关概念的介绍 Kafka是一个scala实现的分布式消息中间件,当中涉及到的相关概念例如以下: Kafka中传递的内容称为message(消息),message 是通过topic ...

  6. Source not found for StandardEngine(ContainerBase).initInternal() line: 1078

    总是这样 在复制完一个项目,并重新起了个名字后. 再打开网页就怎么也打开不了.   第一反应是tomcat出问题了. 于是有了这样的问题: Source not found for StandardE ...

  7. Linux内核的同步机制---自旋锁

    自旋锁的思考:http://bbs.chinaunix.net/thread-2333160-1-1.html 近期在看宋宝华的<设备驱动开发具体解释>第二版.看到自旋锁的部分,有些疑惑. ...

  8. session校验是否登录

    由于一个网站要有好多页面,如果每个页面都写上检验session是否为空,太麻烦了,所以写个工具类,就方便了. 1首先创建一个类库Common 2,然后在这个类库添加引用 3在Common继承 :Sys ...

  9. android LinearLayout等view如何获取button效果

    我们可以给LinearLayout以及一切继承自View的控件,设置View.onClickListener监听,例如LInearLayout. 但是我们发现LinearLayout可以执行监听方法体 ...

  10. Web中的性能优化

    优化Web中的性能 简介 web的优化就是一场阻止http请求最终访问到数据库的战争.优化的方式就是加缓存,在各个节点加缓存. web请求的流程及节点 熟悉流程及节点,才能定位性能的问题.而且优化的顺 ...