E - The Boss on Mars

Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

Description

On Mars, there is a huge company called ACM (A huge Company on Mars), and it’s owned by a younger boss.

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

The first line contains an integer T indicating the number of test cases. (1 ≤ T ≤ 1000) Each test case, there is only one integer n, indicating the number of employees in ACM. (1 ≤ n ≤ 10^8) 
 

Output

For each test case, output an integer indicating the money the boss can save. Because the answer is so large, please module the answer with 1,000,000,007. 
 

Sample Input

2
4
5
 

Sample Output

82
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 容斥原理+快速幂+逆元的更多相关文章

  1. HDU 5685 Problem A | 快速幂+逆元

    Problem A Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  2. HDU 5793 A Boring Question (找规律 : 快速幂+逆元)

    A Boring Question 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5793 Description Input The first l ...

  3. HDU 5868 Different Circle Permutation Burnside引理+矩阵快速幂+逆元

    题意:有N个座位,人可以选座位,但选的座位不能相邻,且旋转不同构的坐法有几种.如4个座位有3种做法.\( 1≤N≤1000000000 (10^9) \). 题解:首先考虑座位不相邻的选法问题,如果不 ...

  4. Open judge C16H:Magical Balls 快速幂+逆元

    C16H:Magical Balls 总时间限制:  1000ms 内存限制:  262144kB 描述 Wenwen has a magical ball. When put on an infin ...

  5. HDU 2855 (矩阵快速幂)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2855 题目大意:求$S(n)=\sum_{k=0}^{n}C_{n}^{k}Fibonacci(k)$ ...

  6. HDU 4471 矩阵快速幂 Homework

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4471 解题思路,矩阵快速幂····特殊点特殊处理····· 令h为计算某个数最多须知前h个数,于是写 ...

  7. HDU - 1575——矩阵快速幂问题

    HDU - 1575 题目: A为一个方阵,则Tr A表示A的迹(就是主对角线上各项的和),现要求Tr(A^k)%9973.  Input数据的第一行是一个T,表示有T组数据. 每组数据的第一行有n( ...

  8. hdu 1757 (矩阵快速幂) 一个简单的问题 一个简单的开始

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1757 题意不难理解,当x小于10的时候,数列f(x)=x,当x大于等于10的时候f(x) = a0 * ...

  9. 随手练——HDU 5015 矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5015 看到这个限时,我就知道这题不简单~~矩阵快速幂,找递推关系 我们假设第一列为: 23 a1 a2 ...

随机推荐

  1. BZOJ solve 100 纪念

    按照xiaoyimi立下的flag是不是该去表白啦--可惜并没有妹子

  2. Codeforce#331 (Div. 2) A. Wilbur and Swimming Pool(谨以此题来纪念我的愚蠢)

    C time limit per test 1 second memory limit per test 256 megabytes input standard input output stand ...

  3. JavaScript的apply和call方法及其区别

    参考资料: http://blog.csdn.net/myhahaxiao/article/details/6952321 apply和call能“劫持”其他对象的方法来执行,其形参如下: apply ...

  4. 锋利的jQuery-1--jQuery对象和DOM对象以及相互转化

    DOM对象: document object model,文档对象模型,每一份dom都可以表示成一棵树. 如下图所示,代码省略 在这颗dom树种,h3, p, ul以及ul的3个li子节点都是dom元 ...

  5. Data conversion error converting

    词错如果出现在sql语句中,那么多半是类型转换的问题

  6. JavaScript的Eval与JSON.parse的区别

    JavaScript的Eval与JSON.parse的区别 json的定义以及用法: JSON(JavaScript Object Notation)是一种轻量级的数据格式,采用完全独立于语言的文本格 ...

  7. dedecms升级后报错

    DedeCMS Error: (PHP 5.3 and above) Please set 'request_order' ini value to include C,G and P (recomm ...

  8. Charm Bracelet

    Charm Bracelet Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Subm ...

  9. 在spring中获取代理对象代理的目标对象工具类

    昨天晚上一哥们需要获取代理对象的目标对象,查找了文档发现没有相应的工具类,因此自己写了一个分享给大家.能获取JDK动态代理/CGLIB代理对象代理的目标对象. 问题描述:: 我现在遇到个棘手的问题,要 ...

  10. Vmware虚拟机

    1.在虚拟机安装完系统后找到相对应的保存路径如:D:\VMware\VOS\Win7x64OS 2.该目录下面会有很多文件和文件夹,其中配置文件Windows 7 x64.vmx和硬盘文件Window ...