UESTC 618 无平方因子数 (容斥 + 莫比乌斯反演)
2015-08-04 13:31
375 查看
无平方因子数
Time Limit: 4000/2000MS (Java/Others)
Memory Limit: 65535/65535KB (Java/Others)
Submit Status
无平方因子数即对于任意一个素数p,p2都不会整除那个数,如1 , 5=5 , 15=3×5都是无平方因子数,而20=22×5不是。现在给定一个n (1≤n<1012) ,求区间[1,n]中无平方因子数的个数。
Input
第一行有个整数T,代表数据组数(T≤10)
接下来有T行,每行有个整数n (1≤n<1012)
Output
输出T行,每行输出一个整数代表区间[1,n]内的无平方因子数的个数。
Sample Input
3
1
10
30
Sample Output
1
7
19
Source
UESTC Training for Math
题目链接:http://acm.uestc.edu.cn/#/problem/show/618
题目分析:又是无平方因子数,比BZOJ那题简单很多,直接算就行了,参照BZOJ 2440
Time Limit: 4000/2000MS (Java/Others)
Memory Limit: 65535/65535KB (Java/Others)
Submit Status
无平方因子数即对于任意一个素数p,p2都不会整除那个数,如1 , 5=5 , 15=3×5都是无平方因子数,而20=22×5不是。现在给定一个n (1≤n<1012) ,求区间[1,n]中无平方因子数的个数。
Input
第一行有个整数T,代表数据组数(T≤10)
接下来有T行,每行有个整数n (1≤n<1012)
Output
输出T行,每行输出一个整数代表区间[1,n]内的无平方因子数的个数。
Sample Input
3
1
10
30
Sample Output
1
7
19
Source
UESTC Training for Math
题目链接:http://acm.uestc.edu.cn/#/problem/show/618
题目分析:又是无平方因子数,比BZOJ那题简单很多,直接算就行了,参照BZOJ 2440
#include <cstdio> #include <cstring> #include <algorithm> #define ll long long using namespace std; int const MAX = 1e6 + 5; int mob[MAX], p[MAX]; bool prime[MAX]; void Mobius() { int pnum = 0; memset(prime, true, sizeof(prime)); mob[1] = 1; for(int i = 2; i < MAX; i++) { if(prime[i]) { p[pnum ++] = i; mob[i] = -1; } for(int j = 0; j < pnum && i * p[j] < MAX; j++) { prime[i * p[j]] = false; if(i % p[j] == 0) { mob[i * p[j]] = 0; break; } mob[i * p[j]] = -mob[i]; } } } ll cal(ll n) { ll cnt = 0; for(ll i = 1; i * i <= n; i++) cnt += (ll) mob[i] * (n / (i * i)); return cnt; } int main() { Mobius(); int T; scanf("%d", &T); while(T --) { ll n; scanf("%lld", &n); printf("%lld\n", cal(n)); } }
相关文章推荐
- Swing-JDialog示例代码-用户登陆UI
- easyUi中的一段漂亮代码之将list转换成tree.
- iOS UIScrollView里面添加UIView,有时候y相差20
- UIView,UILabel,UIButton,
- iOS UI 之 简单概述
- (七十四)Android showDialog时报错requestFeature() must be called before adding content
- 玲珑杯Uinty开发心得——安卓平台发布
- android5.0 必学的UI设计源码实例
- ADF Generator for Dynamic ADF BC and ADF UI
- UI_手势
- 产品经理之UI设计——UI/UX设计趋势
- iOS开发 代码创建UITabBarController
- IOS之UIStepper控件详解
- IOS第七天(3:UiTableView 模型和数据的分组的显示)
- UI03_UIViewController
- IOS第七天(2:UiTableView 加上数据分离)
- IOS第七天(1:UiTableView 的基本用法)
- Longest Consecutive Sequence
- iOS-UIkit复习和代理的使用实现文本框限制输入字数控制
- no value specified for 'date' 的解决方法