『ZOJ 3547』The Boss on Mars (容斥原理)

传送门戳这里qwq

题目描述

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

题目翻译(XJH版本)

又若干年之后,XJH成为了一个上市企业的老板,然而是一个抠门的老板(XJH:???)。

XJH的公司里面有n个员工,编号是1~n。每个人的工资是自己编号的四次方,例如,编号为5的员工,工资就是54=625。 (员工1:???)

然而,XJH觉得工资发的太多了,需要裁员。于是,决定把所有编号和自己互质的员工都裁掉(XJH的编号显然是n,肥水不流外人田)。 现在,问:XJH裁员之后能省多少钱?

解题思路

我们直接一上来就能看出一种O(n)的做法,那就是一个数一个数的枚举,如果这个数没有被访问过并且和n是互质的,那么这个数的次方都与n互质,计算的过程中顺便把vis标记一下,标准的O(n),标准的TLE。。。。

我们不妨转换一下思路:我们不妨求所有于所有与n不互质的数的四次方之和,再用 ∑n^4 把它减掉,也能得到所有的结果。并且,1e8以内的数的质因子个数很少,所以这样应该不会超时,但是还有一个难点——如何推出四次方求和公式?

我们退而求其次,不妨先求出更低次的求和公式:

这样,我们就可以推出二次求和公式,以此类推,我们就可以得到4次求和公式:

既然有除,又有膜,所以我们就要用到逆元了。

比如n与3不互质,那和6,9,12,15也就不互质,那么我们将这个数列除以3,就得到差为1的等差数列,这样我们就很容易求了。运用容斥原理,二进制枚举就可以了。

代码

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<algorithm>
 4 #include<cstring>
 5 #define ll long long
 6 using namespace std;
 7 const int maxn=10050;
 8 const ll MOD=1e9+7;
 9 const ll ni=233333335;
10 bool pri[maxn];
11 ll p[maxn],num[maxn];
12 ll T,n,cnt=0,tot=0,ans;
13 inline void ini(){
14     pri[1]=0;
15     for(register int i=2;i<maxn;i++){
16         if(pri[i]){
17             p[++cnt]=i;
18             for(register int j=2;j*i<maxn;j++)pri[i*j]=0;
19         }
20     }
21 }
22 inline ll ksm(ll a,ll b){
23     ll c=1;
24     while(b){
25         if(b&1)c=c*a%MOD;
26         a=a*a%MOD,b>>=1;
27     }
28     return c%MOD;
29 }
30 inline ll calc(ll x){
31     ll y=x%MOD;
32     y=y*(x+1)%MOD;
33     y=y*(2*x+1)%MOD;
34     y=y*((3*x*x+3*x-1)%MOD)%MOD;
35     y=y*ni%MOD;
36     return y;
37 }
38 inline void resolve(ll x){
39     for(register int i=1;i<=cnt;i++){
40         if(p[i]>x)break;
41         if(x/p[i]*p[i]==x){
42             num[++tot]=p[i];
43             while(x/p[i]*p[i]==x)x/=p[i];
44         }
45     }
46     if(x>1)num[++tot]=x;
47 }
48 int main(){
49     memset(pri,1,sizeof(pri));
50     ini();
51     scanf("%lld",&T);
52     while(T--){
53         memset(num,0,sizeof(num));
54         tot=ans=0;
55         scanf("%lld",&n);
56         ans=calc(n);
57         resolve(n);
58         for(register ll i=1;i<(1<<tot);i++){
59             ll tmp=1,cnt1=0;
60             for(register ll j=0;j<tot;j++){
61                 if(i&(1<<j)){
62                     cnt1++;
63                     tmp=tmp*num[j+1]%MOD;
64                 }
65             }
66             //
67             if(cnt1&1){
68                 ans=(ans-(calc(n/tmp)*ksm(tmp,4))%MOD+MOD)%MOD;
69             }
70             else {
71                 ans=(ans+(calc(n/tmp)*ksm(tmp,4))%MOD+MOD)%MOD;
72             }
73         }
74         printf("%lld\n",(ans+MOD)%MOD);
75     }
76 }

圣诞快乐!!

原文地址:https://www.cnblogs.com/Fang-Hao/p/9495228.html

时间: 2024-07-30 12:40:50

『ZOJ 3547』The Boss on Mars (容斥原理)的相关文章

hdu4059---The Boss on Mars(容斥原理+前n项的4次方和)

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

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)为b的逆元 //由费马小定理a^(p-1) = 1(modp) a , p 互质可得 //30的逆元为30^(mod-2) //由容斥原理很容易得到与n不互质的数之和为 //对于所有的n的素数因子 //一个素数因子的所有数的四次方之和-有两个素数因子的所有数的四次方之和+有三个.... //然后用

HDU 4059 The Boss on Mars ( 容斥原理)

题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4059 题意: 给定一个数n求小于n的与n互斥的数的四次方的和. 分析: 我们可以求出从1~n的所有数的四次方的和sum1,然后容斥求出1~n所有与n不互斥的数的四次方的和sum2: ans =sum1 - sum2; 设f(n)表示从1~n的所有数的四次方的和 f(n)=1/30*n*(n+1)(2n+1)(3n^2+3n-1); 推倒如下: (n+1)^5-n^5=5n^4+10n^3+10n^

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

The Boss on Mars Problem's Link Mean: 给定一个整数n,求1~n中所有与n互质的数的四次方的和.(1<=n<=1e8) analyse: 看似简单,倘若自己手动推公式的话,还是需要一定的数学基础. 总的思路:先求出sum1=(1^4)+(2^4)+...(n^4),再求出sum2=(1~n中与n不互质的数的四次方的和),answer=sum1-sum2. 如何求sum1呢? 有两种方法: 1.数列差分.由于A={Sn}={a1^4+a2^4+...an^4}

『昼颜』读后感

『昼颜』读后感       <--故事梗概-->---------------------------------------------------------------------------------------------------   纱和和北野居然恋爱了,仔细想想, 一个是超市的收银员,一个是高中老师,都有充足的时间, 但是,他们都有自己的家庭.   基于自己的最优选择,还是宽容, 后来各自的家庭必须要拆散他们,所以也就用不再见了, 最后,纱和开始了自己独立的生活...  

[TYVJ1827]『Citric II』一道防AK好题

时间: 1000ms / 空间: 131072KiB / Java类名: Main 背景 第二届『Citric杯』NOIP提高组模拟赛第一题 描述 Lemon认为在第一届『Citric』杯模拟赛中出的题目太简单了,于是他决定,这次要给参赛选手们一个下马威! ^_^ Lemon手上有一个长度为n的数列,第i个数为xi.他现在想知道,对于给定的a,b,c,他要找到一个i,使得a*(i+1)*xi^2+(b+1)*i*xi+(c+i)=0成立.如果有多个i满足,Lemon想要最小的那个i.Lemon有

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): 2327    Accepted Submission(s): 718 Problem Description On Mars, there is a huge company called ACM (A huge Company on Mars), and

Github 恶搞教程(一起『玩坏』自己的 Github 吧)

最近在伯乐在线读到一篇趣文,<如何在 Github『正确』做贡献>,里面各种能人恶搞 Github 的『Public contributions』,下面截取几个小伙伴的战绩: 顺藤摸瓜,发现原来有人已经做出『玩坏』Github 的工具啦,名叫 gitfiti.主要对应预先定义的模板,进行相应日期的 commit 操作,push 至 Github 后在贡献栏中生成相应像素点,并且利用 Github 贡献数不同颜色深度不同的机制,就可以在自己的贡献栏里面看见像素画了.怎么样,是不是心动啦,那么下面

『安全工具』注入神器SQLMAP

原文:『安全工具』注入神器SQLMAP Pic by Baidu 0x 00 前言 正是SQLMAP这种神器的存在,SQL注入简直Easy到根本停不下来.... PS:国内类似软件也有阿D,明小子,挖掘机,当你用过他们之后你才会发现SQLMap才是绝对的注入神器 0x 01 注入原理 *****************************************开始分割线***************************************** 时间原因,这段内容就先不写了 就是因为