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. POJ 1012:Joseph

    Joseph Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 50068   Accepted: 19020 Descript ...

  2. Android开机自动运行APP——BroadcastReceiver

    前言: 有些时候,应用需要在开机时就自动运行,例如某个自动从网上更新内容的后台service.怎样实现开机自动运行的应用?在撰写本文时,联想到高焕堂先生以“Don't call me, I'll ca ...

  3. docker安装及问题处理

    1.在Ubuntu的命令行中输入 sudo apt-get install docker.io 2.如果切换到了root用户下 apt-get install docker.io 3.对于新安装的Ub ...

  4. 基于element ui的图片预览插件

    写插件很简单,满足两个条件即可,一.基本的逻辑思路,二.熟悉插件语法要求.本次Vue插件也比较简单,点击“查看图片”用轮播的方式限制用户上传的图片,如图: 项目采用的是vue-element-admi ...

  5. HDU 1251统计难题 字典树

    字典树的应用. 数据结构第一次课的作业竟然就需要用到树了!!!这不科学啊.赶紧来熟悉一下字典树. 空间开销太大T T #include<cstdio> #include<cstrin ...

  6. 【例题5-8 UVA - 400】Unix ls

    [链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 设n个字符串中出现的最长的为len; 最后一列能容纳len个字符,然后前面的列能容纳len+2个字符. 每行最多60个字符. 按照这 ...

  7. 【BZOJ 3172】单词

    [链接]h在这里写链接 [题意]     给你n个单词;     这n个单词组成了一篇文章;     问你每个单词在这篇文章中出现了多少次.     其中每个单词之间用一个逗号隔开->组成一篇文 ...

  8. 更改jdk所用内存空间

    在做项目是有时候会遇到内存jvm内存不够用的情况,在myeclipse是这样设置的. -Xms128m -Xmx512m -XX:PermSize=128m -XX:MaxPermSize=128m

  9. keil出错总结

    错误一: ..\APP\app.c(51): error:  #268: declaration may not appear after executable statement in block ...

  10. [译]基于Vue.js的10个最佳UI框架,用于构建移动应用程序

    原文查看10 Best Vue.js based UI Frameworks for Building Mobile Apps 如果您期待使用Vue.js构建移动应用程序,那么您可以选择许多可用的UI ...