HDU 4059 容斥原理+快速幂+逆元
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
题目求1-n中与n互质的数的4次方之和,即S=a1^4+a2^4+……; a1,a2……均小于等于n且与n互质。
先求出1^4+2^4+……n^4然后减去与n不互质的数的4次方。
必然要先要用到4次方的求和公式。接下来简单的证明一下,这里前提是你知道3次方的公式,如果不会照下面的模式可以利用2次公式推出3次公式
(x+1)^5=x^5+5*x^4+10*x^3+10*x^2+5*x+1;
则 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+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^4)=(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,还得算一下30对MOD的逆元,也就是30^(MOD-2),
接下来要减掉与n不互质的数4次方,将n质因子分解后运用容斥原理即可,就是减掉一个因子的倍数的4次方结果,加上两个因子乘积的倍
数的4次方结果,减去……以此类推。也可以通过状态压缩枚举,貌似最多9个质因子吧。
在运算的时候,注意各种相乘溢出就行了,类似计算几何的精度问题,数论的溢出也很纠结。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#define LL long long
#define MOD 1000000007
using namespace std;
LL res; //30对MOD的逆元
int prime[],cnt=,flag[]={};
vector<int>fact;
LL PowMod(LL a,LL b){
LL ret=;
while(b){
if(b&)
ret=(ret*a)%MOD;
a=(a*a)%MOD;
b>>=;
}
return ret;
}
LL Sum(LL n){ //求an=n^4,的前n项和
LL ans=n;
ans=(ans*(n+))%MOD;
ans=(ans*((*n+)%MOD))%MOD;
ans=(ans*(((*n*n)%MOD+(*n)%MOD-+MOD)%MOD))%MOD;
ans=(ans*res)%MOD;
return ans;
}
LL Pow(LL n){ //求n^4
LL ans=n;
ans=(((((ans*n)%MOD)*n)%MOD)*n)%MOD;
return ans;
}
int t;
void Prime(){ //筛选素数,便于后面的分解
for(int i=;i<=;i++){
if(flag[i]) continue;
prime[cnt++]=i;
for(int j=;j*i<=;j++)
flag[i*j]=;
}
}
void Init(){
res=PowMod(,MOD-); //求30对MOD的逆元
Prime();
scanf("%d",&t);
}
LL dfs(int idx,LL n){ //容斥原理
LL ret=,tmp;
for(int i=idx;i<fact.size();i++){
tmp=fact[i];
ret=(ret+(Sum(n/tmp)*Pow(tmp))%MOD)%MOD;
ret=((ret-dfs(i+,n/tmp)*Pow(tmp))%MOD+MOD)%MOD;
}
return ret%MOD;
}
int main(){
LL n;
Init();
while(t--){
scanf("%I64d",&n);
fact.clear();
LL tmp=n;
for(int i=;i<cnt&&prime[i]<=tmp;i++)
if(tmp%prime[i]==){
fact.push_back(prime[i]);
while(tmp%prime[i]==)
tmp/=prime[i];
}
if(tmp!=)
fact.push_back(tmp);
LL sum=((Sum(n)-dfs(,n))%MOD+MOD)%MOD;
printf("%I64d\n",sum);
}
return ;
}
HDU 4059 容斥原理+快速幂+逆元的更多相关文章
- HDU 5685 Problem A | 快速幂+逆元
Problem A Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- HDU 5793 A Boring Question (找规律 : 快速幂+逆元)
A Boring Question 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5793 Description Input The first l ...
- HDU 5868 Different Circle Permutation Burnside引理+矩阵快速幂+逆元
题意:有N个座位,人可以选座位,但选的座位不能相邻,且旋转不同构的坐法有几种.如4个座位有3种做法.\( 1≤N≤1000000000 (10^9) \). 题解:首先考虑座位不相邻的选法问题,如果不 ...
- Open judge C16H:Magical Balls 快速幂+逆元
C16H:Magical Balls 总时间限制: 1000ms 内存限制: 262144kB 描述 Wenwen has a magical ball. When put on an infin ...
- HDU 2855 (矩阵快速幂)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目大意:求$S(n)=\sum_{k=0}^{n}C_{n}^{k}Fibonacci(k)$ ...
- HDU 4471 矩阵快速幂 Homework
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...
- HDU - 1575——矩阵快速幂问题
HDU - 1575 题目: A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973. Input数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n( ...
- hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * ...
- 随手练——HDU 5015 矩阵快速幂
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5015 看到这个限时,我就知道这题不简单~~矩阵快速幂,找递推关系 我们假设第一列为: 23 a1 a2 ...
随机推荐
- BZOJ-1491 社交网络 FLoyd+乱搞
感觉这两天一直在做乱搞的题... 1491: [NOI2007]社交网络 Time Limit: 10 Sec Memory Limit: 64 MB Submit: 1279 Solved: 732 ...
- Android系统中的广播(Broadcast)机制简要介绍和学习计划
在Android系统中,广播(Broadcast)是在组件之间传播数据(Intent)的一种机制:这些组件甚至是可以位于不同的进程中,这样它就像Binder机制一样,起到进程间通信的作用:本文通过一个 ...
- iOS开发的那些坑
最近重新拿起了iOS的开发,使用OC和Swift混编,碰到了一些比较棘手的问题,在这里记录下来,方便自己以后或他人不再入坑.这篇文章的内容包含: UITableViewCell的真实结构在iOS的环境 ...
- P、NP、NPC、NP-Hard问题
转自:http://www.matrix67.com/blog/archives/105 总结 P:能用多项式时间求解的问题NP:能用多项式时间验证的问题(但不知道能不能用多项式时间求解).存在不属于 ...
- tableView左滑删除功能
实现三个代理方法即可 -(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtI ...
- ResultSet
在Java中,获得ResultSet的总行数的方法有以下几种. 第一种:利用ResultSet的getRow方法来获得ResultSet的总行数 Java代码Statement stmt = con. ...
- WPF捕捉Windows关机事件
private const int SC_SCREENSAVE = 0xF140; private const int WM_QUERYENDSESSION = 0x0011; private boo ...
- 统一软件开发过程(rup)理解
不好意思,没有内容,请看百科吧http://baike.baidu.com/link?url=-zXA6oXiHRts5Z1THGzie7seH-YRigfiR7KRppP6TYcnnb7_Bxc_f ...
- BlockingQueue详解
本例介绍一个特殊的队列:BlockingQueue,如果BlockingQueue是空的,从BlockingQueue取东西的操作将会被阻断进入 等待状态,直到BlockingQueue进了东西才会被 ...
- Socket网络编程(1)
TCP/IP 简单介绍 应用层 (Application):应用层是个很广泛的概念,有一些基本相同的系统级 TCP/IP 应用以及应用协议,也有许多的企业商业应用和互联网应用. 传输层 (Transp ...