The Boss on Mars

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1934    Accepted Submission(s): 580

Problem 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
 
Author
ZHANG, Chao
 
Source
 

题解及代码:

这道题目的综合性还是非常强的。首先说一下题目,就是求小于n而且与n互素的数的四次方的和。

说一下思路吧:首先我们求出1---n-1的全部的数的四次方的和,之后将n进行素因子分解。求出n的全部因子,然后减去包括这些因子的数的四次方就能够了。

大体上的思路有了,来处理一下细节:1.首先我们要求出四次方和的公式   2.素数打表   3.求逆元(由于四次方和公式有一个分母,取余时要乘上逆元)

4.素因子分解    5.容斥原理

搞定这5步,我们这道题就能做了,所以说综合性很强。

详细见代码吧:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
using namespace std;
const long long mod=1000000007,q=233333335;//p为逆元,用费马小定理求出
bool prime[10010];
int p[1400];
int k=0; //四次方和计算公式
long long cal(long long n)
{
if(n==0) return 0;
return (n*(n+1)%mod*(2*n+1)%mod)%mod*((3*n*n+3*n-1)%mod*q%mod)%mod;
} //容斥原理
void dfs(int base,int num_p,long long n,long long m,long long nt,long long mu,long long &sum,long long tab_p[])
{
if(nt==m)
{
long long b=n/mu;
if(m%2==0)
{
sum=(sum-mu*mu%mod*mu%mod*mu%mod*cal(b)%mod+mod)%mod;
}
else
{
sum=(sum+mu*mu%mod*mu%mod*mu%mod*cal(b)%mod)%mod;
}
return;
}
for(long long i=base; i<num_p; i++)
{
dfs(i+1,num_p,n,m,nt+1,mu*tab_p[i],sum,tab_p);
}
} //素数打表
void isprime()
{
long long i,j;
memset(prime,true,sizeof(prime));
prime[0]=prime[1]=false;
for(i=2; i<10010; i++)
{
if(prime[i])
{
p[k++]=i;
for(j=i*i; j<10010; j+=i)
prime[j]=false;
}
}
} int main()
{
isprime();
long long n,ans,tab_p[1400];
int cas;
scanf("%d",&cas);
while(cas--)
{
scanf("%I64d",&n);
n=n-1;
ans=cal(n);
long long m=n,t=n+1;
int num_p=0;
for(int i=0; i<k&&p[i]*p[i]<=t; i++) //素因子分解
if(t%p[i]==0)
{
tab_p[num_p++]=p[i];
while(t%p[i]==0)
{
t/=p[i];
}
}
if(t>1) tab_p[num_p++]=t; /*//输出測试
for(int i=0;i<num_p;i++)
{
printf("%d ",tab_p[i]);
}
puts("");
//測试结束
*/ long long sum=0;
for(int i=0; i<num_p; i++) //将不互素的部分减去
{
n=m/tab_p[i];
sum=(sum+tab_p[i]*tab_p[i]%mod*tab_p[i]%mod*tab_p[i]%mod*cal(n))%mod;
} for(long long i=2; i<=num_p; i++) //容斥部分求解
dfs(0,num_p,m,i,0LL,1LL,sum,tab_p); printf("%I64d\n",(ans-sum+mod)%mod);
}
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(纳入和排除)

    http://acm.hdu.edu.cn/showproblem.php?pid=4059 定义S = 1^4 + 2^4 + 3^4+.....+n^4.如今减去与n互质的数的4次方.问共降低了多 ...

  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. WEB安全实战(二)带你认识 XSS 攻击

    前言 上一篇文章写了关于 WEB 安全方面的实战,主要是解决 SQL 盲注的安全漏洞.这篇文章本来是要写一篇关于怎样防治 XSS 攻击的,可是想来想去,还是决定先从理论上认识一下 XSS 吧.下一篇文 ...

  2. Visual Studio Team Services持续集成到Github仓库

    Devops如何用VSTS持续集成到Github仓库!   工欲善其事,必先利其器.在开始正式的教程之前我们先来聊聊准备工作. 管理工具会VSTS. 代码管理会用GITHUB. 服务器会用Azure. ...

  3. 19. Spring Boot Shiro 权限管理

    转自:https://blog.csdn.net/catoop/article/details/50520958

  4. zeromq and jzmq

    install c test install jzmq java test Storm UI Cluster Summary Version Nimbus uptime Supervisors Use ...

  5. JSP学习 —— 开篇:JSP,servlet容器,Tomcat,servlet容器之间的关系

    JSP(JAVA SERVER PAGE)的缩写,其本身就是servlet的简化,是一种动态网页标准,其特点是在HTML代码中嵌入JAVA代码,JSP标签或用户标签来生成网页.至于它为什么会出现,主要 ...

  6. [Angular] Progress HTTP Events with 'HttpRequest'

    New use case that is supported by the HTTP client is Progress events. To receive these events, we cr ...

  7. Project Euler 516 5-smooth totients (数论)

    题目链接: https://projecteuler.net/problem=516 题目: \(5\)-smooth numbers are numbers whose largest prime ...

  8. 微信支付v2开发(6) 发货通知

    本文介绍微信支付中发货通知功能的实现. 一.发货通知 为了更好地跟踪订单的情况,需要第三方在收到最终支付通知之后,调用发货通知API告知微信后台该订单的发货状态. 发货时间限制:虚拟.服务类24小时内 ...

  9. python 的spyder用法

    ctrl+tab可以进行跳转 https://blog.csdn.net/luckygirl0809/article/details/79929491

  10. mahout測试朴素贝叶斯分类样例

    对于这个測试建议大家先理解原理,这里我画了例如以下的示意图 接下来就依照例如以下的细节来输入指令測试: 首先前提是Hadoop安装并启动,mahout已经安装了. <strong>< ...