HDU 4059 The Boss on Mars(容斥原理)
The Boss on Mars
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2494 Accepted Submission(s): 775
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.
2
4
5
82
354这道题目是求小于n和n互质的数的四次方的和利用容斥原理,可以用总和减去不和n互质的和也可以直接求互质的数的和这里还涉及到了sum{1^4,2^4,3^4....n^4}求和公式n*(n+1)*(2*n+1)*((3*n*n+3*n-1)/30求和公式里有/ 那么在用同余定理的时候就要用逆元b/c(mod m)=b(mod m)* c^(m-2)(mod m)证明略#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <stdlib.h> using namespace std;
typedef long long int LL;
#define MAX 1000000
LL prime[MAX+5];
LL sprime[MAX+5];
bool check[MAX+5];
const LL mod=1e9+7;
LL INF;
LL cnt;
LL quick(LL x,LL a)
{
LL sum=1;
for(x;x>0;x>>=1)
{
if(x&1)
sum=(sum*a)%mod;
a=(a*a)%mod;
}
return sum;
}
LL sum1(LL n)
{
INF=quick(mod-2,30);
return n*(n+1)%mod*(2*n+1)%mod*((3*n*n%mod+3*n-1)%mod)%mod*INF%mod;
}
LL sum2(LL n)
{
return (((n*n)%mod*n)%mod*n)%mod;
}
void eular()
{
memset(check,false,sizeof(check));
int tot=0;
for(LL i=2;i<MAX+5;i++)
{
if(!check[i])
prime[tot++]=i;
for(int j=0;j<tot;j++)
{
if(i*prime[j]>MAX+5) break;
check[i*prime[j]]=true;
if(i%prime[j]==0) break;
}
}
}
void Divide(LL n)
{
cnt=0;
LL t=(LL)sqrt(n*1.0);
for(LL i=0;prime[i]<=t;i++)
{
if(n%prime[i]==0)
{
sprime[cnt++]=prime[i];
while(n%prime[i]==0)
n/=prime[i];
}
}
if(n>1)
sprime[cnt++]=n;
}
int main()
{
eular();
int t;
scanf("%d",&t);
while(t--)
{
LL n;
scanf("%lld",&n);
Divide(n); LL ans=0;
LL res=sum1(n);
for(int i=1;i<((LL)(1<<(cnt)));i++)
{
int num=0;
LL tmp=1;
for(int j=0;j<cnt;j++)
{
if(i&(1<<j))
{
num++;
tmp*=sprime[j];
}
}
if(num&1)
ans=(ans+(sum1(n/tmp)*sum2(tmp))%mod)%mod;
else
ans=((ans-(sum1(n/tmp)*sum2(tmp))%mod)%mod+mod)%mod;
}
printf("%lld\n",(res-ans+mod)%mod);
}
return 0;
}
HDU 4059 The Boss on Mars(容斥原理)的更多相关文章
- HDU 4059 The Boss on Mars 容斥原理
The Boss on Mars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- HDU 4059 The Boss on Mars(容斥原理 + 四次方求和)
传送门 The Boss on Mars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Oth ...
- hdu 4059 The Boss on Mars
The Boss on Mars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 数论 + 容斥 - HDU 4059 The Boss on Mars
The Boss on Mars Problem's Link Mean: 给定一个整数n,求1~n中所有与n互质的数的四次方的和.(1<=n<=1e8) analyse: 看似简单,倘若 ...
- 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次方.问共降低了多 ...
- hdu 4059 The Boss on Mars 容斥
题目链接 求出ai^4+a2^4+......an^4的值, ai为小于n并与n互质的数. 用容斥做, 先求出1^4+2^4+n^4的和的通项公式, 显然是一个5次方程, 然后6个方程6个未知数, 我 ...
- 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 ...
- hdu 4059 数论+高次方求和+容斥原理
http://acm.hdu.edu.cn/showproblem.php? pid=4059 现场赛中通过率挺高的一道题 可是容斥原理不怎么会.. 參考了http://blog.csdn.net/a ...
- hdu4059The Boss on Mars 容斥原理
//求1到n之间与n互质的数的四次方的和 //segma(n^4) = (6n^5+15n^4+10n^3-n)/30 //对于(a/b)%mod能够转化为(a*inv(b))%mod //inv(b ...
随机推荐
- BAT特殊字符(转)
BAT特殊字符1. 点 与echo连用,作用是换行 示例1 [输出空行] echo. 2 > 定向符[输出] 将命令的输出进行重定向 [一般用于将结果写入文件] 注意 nul 为空设备 > ...
- css 禁用移动端部分特性
禁止复制黏贴功能 .oncopy{ -webkit-user-select: none; } 禁止点击之后修改背景颜色 -webkit-tap-highlight-color: rgba(0,0,0, ...
- SCUT个人整理的常见问题
1.刚打开redis和服务端,测试时发现明显延时 刚开始会同步数据比较卡,需要进行一次操作才会同步.这个问题暂时没去研究 2.Server failed to start error:The mode ...
- WatiN自动化测试
简介 WatiN - Watir的.NET版: http://watin.sourceforge.net/ Welcome at the WatiN (pronounced as What-in) w ...
- ubuntu 安装python3.5
wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tar.xz xz -d Python-.tar.xz .tar cd Python ...
- apue编程之getopt ,getopt_long使用方法以及实例
1.getopt 1.1 函数定义 int getopt(int argc, char * const argv[], const char *optstring);#include <unis ...
- Selenium操作之滚动条
在用Selenium做UI自动化时,经常会遇到有些元素找不到之类的问题,但是自己的代码并没有错,元素就是找不到,这是为什么呢?原因很简单,由于页面内容较多,有些内容需要下拉滚动条才会显示,这里介绍一种 ...
- win7系统如何配置JAVA环境变量
1.在“计算机”右击选择“属性” 2.点击“高级系统设置” 3.点击“环境变量” 4.点击系统变量下面的“新建” (1)新建->变量名:JAVA_HOME变量值 C:\Program Files ...
- PHP学习笔记(10)mysql常用命令
1. 连接mysql数据库: 输入: mysql> mysql -uroot -p123(注意后面没有分号!!) 输出: mysql: [Warning] Using a password on ...
- YARN机制
YARN是资源管理调度的机制,之前一直以来和MapReduce机制合在一起,之后才分开.正是因为YARN机制单独独立出来,才使得Hadoop框架更加具有普适性.MapReduce可以处理海量离线数据, ...