8.26 T2 日记和欧拉函数(欧拉函数)

http://cplusoj.com/d/senior/p/NOD2301B

发现 x ≤ B x\le B xB 时答案是 x x x

x > B + 500 x>B+500 x>B+500 左右答案是1

我们预处理中间的就行

预处理直接暴力做,求 max ⁡ ϕ \max \phi maxϕ 的话相当于求小于它的质数

#include<bits/stdc++.h>
using namespace std;
#ifdef LOCAL
 #define debug(...) fprintf(stdout, ##__VA_ARGS__)
 #define debag(...) fprintf(stderr, ##__VA_ARGS__)
#else
 #define debug(...) void(0)
 #define debag(...) void(0)
#endif
#define int long long
inline int read(){int x=0,f=1;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;
ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+
(x<<3)+(ch^48);ch=getchar();}return x*f;}
#define Z(x) (x)*(x)
#define pb push_back
#define fi first
#define se second
//#define M
//#define mo
#define N 510
int n, m, i, j, k, T, B;
int mx, phi[N], f[N], sm[N], l, r;
int ans;  
map<int, int>mp; 

int qry(int x) {
	if(mp[x]) return mp[x]; 
	int ans = 1, i, j; 
//	debug("phi[%lld] = ", x); 
	for(i = 2; i * i <= x; ++i) 
		if(x % i == 0) {
			j = 1; 
			while(x % i == 0) 
				x /= i, j *= i; 
			j /= i; j *= (i - 1); 
			ans *= j; 
		}
	if(x > 1) ans *= (x - 1); 
//	debug("%lld\n", ans); 
	return mp[x] = ans; 
}

signed main()
{
	#ifdef LOCAL
	  freopen("in.txt", "r", stdin);
	  freopen("out.txt", "w", stdout);
	#endif
//	srand(time(NULL));
//	T=read();
//	while(T--) {
//
//	}
	T = read(); B = read(); 
	for(j = 1 + B; ; --j) if(j == 1 || qry(j) == j - 1) break; 
//	debug("Out\n"); 
	for(i = 1; i <= 400; ++i) {
		k = i + B; phi[i] = qry(k); 
		if(phi[i] == k - 1) j = k; 
		mx = j - 1 - B; f[i] = k; 
		if(i <= 6) debug("[%lld]%lld %lld | %lld\n", k, mx, phi[i], j); 
		if(mx <= 0) continue; 
		while(mx-- && f[i] != 1) f[i] = qry(f[i]); 
		if(i <= 6) debug("[%lld] %lld\n", k, f[i]); 
	}
	for(i = 1; i <= 400; ++i) sm[i] = sm[i - 1] + f[i]; 
	while(T--) {
		l = read(); r = read(); ans = 0; 
		if(r <= B) { printf("%lld\n", (l + r) * (r - l + 1) / 2); continue; }
		if(l <= B) ans += (l + B) * (B - l + 1) / 2, l = B + 1; 
		if(l >= B + 400) { printf("%lld\n", r - l + 1); continue; }
		if(r >= B + 400) ans += (r - (B + 400) + 1), r = B + 400 - 1; 
		debug("[%lld %lld] %lld\n", l, r, ans); 
		l -= B; r -= B; ans += sm[r] - sm[l - 1]; 
		debug("[%lld %lld]\n", l, r); 
		printf("%lld\n", ans); 
	}
	return 0;
}



  • 4
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值