题意:

求小于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();
}
}
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 

hdu4059 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. hdu4059 The Boss on Mars

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

  3. 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 ...

  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/Oth ...

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

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

  7. The Boss on Mars

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

  8. hdu 4059 The Boss on Mars

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

  9. 数论(容斥原理)hdu-4509-The Boss on Mars

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4059 题目大意: 给一个n,求1~n中与n互质的数的4次方的总和. 解题思路: 容斥原理.逆元.公式 ...

随机推荐

  1. ASP.NET MVC5+EF6+EasyUI 后台管理系统(7)-MVC与EasyUI DataGrid

    系列目录 本节知识点 为了符合后面更新后的重构系统,文章于2016-11-1日重写 EasyUI读取MVC后台Json数据 开始实现 我们的系统似乎越来越有趣了 首先从前端入手,开打View下面的Sh ...

  2. spring整合mybatis使用<context:property-placeholder>时的坑

    背景 最近项目要上线,需要开发一个数据迁移程序.程序的主要功能就是将一个数据库里的数据,查询出来经过一系列处理后导入另一个数据库.考虑到开发的方便快捷.自然想到用spring和mybatis整合一下. ...

  3. 数据库进阶之路(五) - MySQL行锁深入研究

    由于业务逻辑的需要,必须对数据表的一行或多行加入行锁,举个最简单的例子,图书借阅系统:假设id=1的这本书库存为1,但是有2个人同时来借这本书,此处的逻辑为: ; --如果restnum大于0,执行u ...

  4. 学习Redis你必须了解的数据结构——JS实现集合和ECMA6集合

    集合类似于数组,但是集合中的元素是唯一的,没有重复值的.就像你学高中数学的概念一样,集合还可以做很多比如,并集,交集,差集的计算.在ECMA6之前,JavaScript没有提供原生的Set类,所以只能 ...

  5. 设计模式(十四)模板方法模式(Template Pattern)

    一.引言 提到模板,大家肯定不免想到生活中的“简历模板”.“论文模板”.“Word中模版文件”等,在现实生活中,模板的概念就是——有一个规定的格式,然后每个人都可以根据自己的需求或情况去更新它,例如简 ...

  6. Web Service概念梳理

    计算机技术难理解的很多,Web Service 对我来说就是一个很难理解的概念:为了弄清它到底是什么,我花费了两周的时间,总算有了一些收获,参考了不少网上的资料,但有些概念说法不一.我以w3c和 一些 ...

  7. MySQL设置服务器方法

    grant select,insert,update,delete on *.* to root@"%" Identified by "root";

  8. python学习笔记(基础四:模块初识、pyc和PyCodeObject是什么)

    一.模块初识(一) 模块,也叫库.库有标准库第三方库. 注意事项:文件名不能和导入的模块名相同 1. sys模块 import sys print(sys.path) #打印环境变量 print(sy ...

  9. Eclipse Meaven Spring SpringMVC Mybaits整合

    本示例是在:Ubuntu15上实现的:Windows上安装Maven将不太相同. Maven Install Run command sudo apt-get install maven, to in ...

  10. WCF服务启用与配置端口共享

    在 Windows Communication Foundation (WCF) 应用程序中使用 net.tcp:// 端口共享的最简单方式是使用 NetTcpBinding 公开一个服务. 此绑定提 ...