-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path56.cpp
More file actions
57 lines (56 loc) · 928 Bytes
/
56.cpp
File metadata and controls
57 lines (56 loc) · 928 Bytes
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
#include<bits/stdc++.h>
using namespace std;
typedef long long int lli;
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cin.tie(NULL);
#ifndef ONLINE_JUDGE
freopen("input.txt","r",stdin);
freopen("output.txt","w",stdout);
#endif
int t;
cin>>t;
while(t--)
{
lli n,k,last;
lli sum=0,sum_k=0 ,temp=0;
cin>>n>>k;
sum = (n*(n+1))/2;
for(lli i=n;i>=1;i--)
{
if(i%k==0)
{
last = i;
break;
}
}
last = last/k;
sum_k = k*(last*(last+1))/2;
for (lli i=k;i<=n;i=i+k)
{
lli p = i;
while(p!=1)
{
p = p/k;
if(p%k==0)
{
continue;
}
else
{
// cout<<p<<"\n";
temp = temp + p;
break;
}
}
// cout<<i<<" temp = "<<temp<<"\n";
}
cout<<"sum = "<<sum<<"\n";
cout<<"sum_k = "<<sum_k<<"\n";
cout<<"temp = "<<temp<<"\n";
sum = sum-sum_k+temp;
cout<<sum<<"\n";
}
}