HDU Integer's Power(容斥原理)

题意

求[l,r]的最大指数和(1<=l,r<=10^18)

最大指数和(如64=8^2=4^3=2^6,所以64的最大指数和是6)

题解

很明显我们可以先求出[1,n]的最大指数和,然后再作差。

我们可以先求出num[i]代表[1,n]中最大指数为i的数有多少个。

然后枚举全部的i,然后让答案加上i*num[i];

那么怎么求num[i]呢

我们可以求出[1,n]中指数为x的数有多少个作为num[x]的初步值。这个用n1/x就可以求出(不过要注意精度问题,及其恶心,看代码吧)

然后这个num却对是不对的。我们发现16=4^2=2^4,num[2]中却有16,所以我们用num[2]-num[4]就行了。

我们发现对于每一对i>j且i%j==0num[j]都需要减num[i],不过我们要倒序枚举j。

具体看代码吧。

 1 #include<iostream>
 2 #include<cmath>
 3 #include<cstdio>
 4 #include<algorithm>
 5 #include<cstring>
 6 using namespace std;
 7 const long long INF=1e18+1;
 8 const long long inf=(long long)1<<31;
 9 long long dp[100],a,b,n;
10 long long ksm(long long x,long long b){
11     long long tmp=1;
12     while(b){
13         if(b&1){
14             double num=1.0*INF/tmp;
15             if(x>num)return -1;
16             tmp=tmp*x;
17         }
18         b>>=1;
19         if(x>inf&&b>0)return -1;
20         x=x*x;
21     }
22     return tmp;
23 }
24 long long calc(long long n,long long x){
25     long long a=(long long)(pow((double)(n),1.0/x)+1e-9);
26     long long b=ksm(a,x);
27     if(b==n)return a;
28     if(b==-1||b>n)return a-1;
29     long long c=ksm(a+1,x);
30     if(c!=-1&&c<=n)return a+1;
31     else return a;
32 }
33 long long solve(long long x){
34     if(x==0)return 0;
35     if(x==1)return 1;
36     long long k;
37     dp[1]=x;
38     for(long long i=2;i<=62;i++){
39         dp[i]=calc(x,i)-1;
40         if(dp[i]==0){
41             k=i-1;
42             break;
43         }
44     }
45     for(long long i=k;i>=1;i--)
46         for(long long j=1;j<i;j++){
47             if(i%j==0)dp[j]-=dp[i];
48         }
49     long long ans=dp[1];
50     for(long long i=2;i<=k;i++){
51         ans+=dp[i]*i;
52     }
53     return ans;
54 }
55 int main(){
56     while(scanf("%lld%lld",&a,&b)!=EOF){
57         if(a==0&&b==0)break;
58         printf("%lld\n",solve(b)-solve(a-1));
59     }
60     return 0;
61 }

HDU Integer's Power(容斥原理)

原文地址:https://www.cnblogs.com/Xu-daxia/p/9715416.html

时间: 2024-10-01 04:38:12

HDU Integer's Power(容斥原理)的相关文章

hdu 6034 Balala Power!

题目链接:hdu 6034 Balala Power! 题意: 给你n个字符串,都是包含小写字母,现在让你给a~z赋值0~25,使得这些字符串变成的26进制的数的总和最大. 不能有前导0的情况,他们保证至少有一个字母不出现在第一位. 题解: 每个字符对答案的贡献都可以看作一个 26 进制的数字,问题相当于要给这些贡献加一个 0 到 25 的权重使得答案最大.最大的数匹配 25,次大的数匹配 24,依次类推.排序后这样依次贪心即可,唯一注意的是不能出现前导 0. 前导0的具体处理看代码. 1 #i

hdu 4790 Just Random 容斥原理+数学

Just Random Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1723    Accepted Submission(s): 483 Problem Description Coach Pang and Uncle Yang both love numbers. Every morning they play a game w

hdu (欧拉函数+容斥原理) GCD

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=1695 看了别人的方法才会做 参考博客http://blog.csdn.net/shiren_Bod/article/details/5787722 题意 a,b,c,d,k五个数,a与c可看做恒为1,求在a到b中选一个数x,c到d中选一个数y,使得gcd(x,y)等于k,求x和y有多少对. 首先可以想到选取的必是k的倍数,假设是x和y倍,则x和y一定是互质的在,那么就变成了求1到b/k和1到d/k的之

HDU 6034 Balala Power!(贪心+排序)

Balala Power! Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others) Total Submission(s): 1411    Accepted Submission(s): 239 Problem Description Talented Mr.Tang has n strings consisting of only lower case characters.

hdu 4390 Number Sequence (容斥原理)

Number Sequence Time Limit: 10000/3000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submission(s): 790    Accepted Submission(s): 331 Problem Description Given a number sequence b1,b2-bn. Please count how many number sequences

2017 Multi-University Training Contest - Team 1 1002&amp;&amp;HDU 6034 Balala Power!【字符串,贪心+排序】

Balala Power! Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Total Submission(s): 2668    Accepted Submission(s): 562 Problem Description Sample Input 1 a 2 aa bb 3 a ba abc Sample Output Case #1: 25 Case #2: 132

HDU 4135 Co-prime(容斥原理 + 基础数论)

传送门 Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3695    Accepted Submission(s): 1461 Problem Description Given a number N, you are asked to count the number of integers between A an

hdu 4135 Co-prime【容斥原理】

Co-prime Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 1668    Accepted Submission(s): 636 Problem Description Given a number N, you are asked to count the number of integers between A and B

hdu 2841 Visible Trees 容斥原理

Visible Trees Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description There are many trees forming a m * n grid, the grid starts from (1,1). Farmer Sherlock is standing at (0,0) point. He wonders how ma