-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCHEFDIV.cpp
134 lines (132 loc) · 3.13 KB
/
CHEFDIV.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include<bits/stdc++.h>
using namespace std;
int main()
{
long long int a[1000500]={0},i,j,x,y,temp,z;
vector<long long int>b;
a[1]=1;
for(i=2;i*i<=1000050;i++)
{
if(a[i]==0)
{
for(j=i*2;j<=1000050;j+=i)
{
if(a[j]==0)
a[j]=i;
}
}
}
for(i=2;i<=1000050;i++)
{
if(a[i]==0)
b.push_back(i);
}
cin>>x>>y;
for(i=0;i<b.size();i++)
{
if(b[i]<=sqrt(y))
temp=b[i];
else
break;
}
vector<long long int>c[y-x+1];
z=y-x+1;
vector<pair<long long int,long long int> >e;
for(i=x;i<=y;i++)
{
e.push_back(make_pair(i%z,i));
}
sort(e.begin(),e.end());
for(i=0;i<b.size();i++)
{
if(b[i]<=temp)
{
if(x%b[i]!=0)
j=x+(b[i]-x%b[i]);
else
j=x;
for(;j<=y;j+=b[i])
{
c[j%z].push_back(b[i]);
}
}
else
break;
}
long long int sum=0;
for(i=0;i<e.size();i++)
{
set<pair<long long int,long long int> >d;
long long int m,n,p,count=0,prod=1,flag=0;
m=e[i].second;
p=m;
for(j=0;j<c[i].size();j++)
{
n=c[i][j];
z=0;
while(m%n==0)
{
m=m/n;
count++;
}
m=p;
d.insert(make_pair(count,n));
count=0;
z++;
}
m=p;
if(d.size()==0)
{
flag=1;
sum+=2;
}
if(flag==0)
{
set<pair<long long int,long long int> >::iterator it;
for(it=d.begin();it!=d.end();it++)
{
n=it->first;
p=it->second;
while(n>0)
{
m=m/p;
n--;
}
}
if(m>1)
d.insert(make_pair(1,m));
count=0;
while(!d.empty())
{
prod=1;
for(it=d.begin();it!=d.end();it++)
{
m=it->first;
prod=prod*(m+1);
}
if(count==1)
sum+=prod-1;
else
sum+=prod;
prod=1;
count++;
it=d.end();
it--;
m=it->first;
n=it->second;
if((m-1)>0)
{
m--;
d.erase(it);
d.insert(make_pair(m,n));
}
else
{
d.erase(it);
}
}
sum+=1;
}
}
cout<<sum<<endl;
}