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 ...
随机推荐
- BZOJ2121 字符串游戏
Description BX正在进行一个字符串游戏,他手上有一个字符串L,以及其 他一些字符串的集合S,然后他可以进行以下操作:对于一个在集合S中的字符串p,如果p在L中出现,BX就可以选择是否将其删 ...
- sdk和ndk
让我先来说说android sdk (Android Software Development Kit, 即Android软件开发工具包)可以说只要你使用java去开发Android这个东西就必须用到 ...
- JS 显示时间与倒计时练习
显示时间与倒计时 HTML <!DOCTYPE html> <html> <head> <meta charset="utf-8"> ...
- Linux下使用popen()执行shell命令
转载 http://www.cnblogs.com/caosiyang/archive/2012/06/25/2560976.html 简单说一下popen()函数 函数定义 #include < ...
- 突破XSS字符数量限制执行任意JS代码
一.综述 有些XSS漏洞由于字符数量有限制而没法有效的利用,只能弹出一个对话框来YY,本文主要讨论如何突破字符数量的限制进行有效的利用,这里对有效利用的定义是可以不受限制执行任意JS.对于跨站师们来说 ...
- WPF 检测管理员权限
// 检查是否是管理员身份 private static void CheckAdministrator() { WindowsIdentity wi = null; try { wi = Windo ...
- (三)Linux命令基本格式以及文件处理命令
命令基本格式 (1)命令提示符 如下是命令行的命令提示符,以此为例,讲解含义. 其中: root 当前登录用户名 localhost 主机名 ~ 当前所在的目录(即家目录,用户登录的初始位置) # 超 ...
- POJ 3274 Gold Balanced Lineup
Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10924 Accepted: 3244 ...
- 解决 MySQL Cluster 通过 某一个MySqld节点新建表时,其他 MySqld节点 看不到表内容的问题
问题: 总共有 4 个MySqld节点,通过其中的一个节点新建表时,发现其他 MySqld节点 查不到表内容的问题,即表没有同步过来. 解决方案: 主要是因为新建表时,所选的 表引擎 错误导致的,只能 ...
- MVC中html转义问题(直接输出html的方法)
MVC中如果用@string(string是包含html代码的字符串)形式输出字符串,那么对应的html标签会自动转义,如果想直接输出html可用以下方法: @(new HtmlString( &qu ...