ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#213024 | #3824. 最大公约数 | shiruiheng | 100 | 1259ms | 1200kb | C++11 | 1.3kb | 2024-11-03 10:23:22 | 2024-11-03 13:01:52 |
answer
#include<bits/stdc++.h>
using namespace std;
using ll = long long;
//using LL = __int128
#define pi pair<ll, ll>
#define fi first
#define se second
//#define fi(x) (x->first)
//#define se(x) (x->second)
ll n, t, a[111], pos;
#define push(x) (a[++pos] = (x))
void solve(){
unsigned long long ans = 0, tmp = -1;
for(int i = 0 ; i <= n ; i++){
ans += (tmp *= -1) * __gcd(n, 1ll * i);
}
cout << ans << "\n";
}
void pme(ll sz){
pos = 0;
for(ll i = 2 ; i <= sz / i ; i++)
if(sz % i == 0){
push(i);
while(sz % i == 0)
sz /= i;
}
if(sz > 1)
push(sz);
}
ll phi(ll x){
ll ans = x;
for(int i = 1 ; i <= pos ; i++)
if(x % a[i] == 0)
ans = ans / a[i] * (a[i] - 1);
return ans;
}
ll calc(ll g){
if(g % 2 == 0)
return g * phi(n / g);
if((n / g) % 2 == 0)
return -g * phi(n / g);
return -g * (n / g == 1);
}
int main(){
cin >> t;
while(t--){
cin >> n;
if(n <= 100000){
solve();
continue;
}
if(n % 2 == 1){
cout << 0 << "\n";
continue;
}
unsigned long long ans = n;
pme(n);
ans += calc(1);
if(n != 1)
ans += calc(n);
for(ll i = 2 ; i <= n / i ; i++)
if(n % i == 0){
ans += calc(i);
if(i * i != n)
ans += calc(n / i);
}
cout << ans << " \n";
}
return 0;
}
/*
freopen(".in", "r", stdin);
freopen(".out", "w", stdout);
*/
详细
小提示:点击横条可展开更详细的信息
Test #1:
score: 10
Accepted
time: 109ms
memory: 1192kb
input:
10 54926 90288 81440 67894 64722 61170 95256 94752 59904 98368
output:
162827 2104272 813680 202699 453747 244635 1775844 2125872 1269504 1247488
result:
ok 10 lines
Test #2:
score: 10
Accepted
time: 106ms
memory: 1192kb
input:
10 64000 76328 56400 75540 89808 72566 71682 57048 89460 99316
output:
1043200 903284 1023600 528600 688368 212091 301307 342228 1475208 468152
result:
ok 10 lines
Test #3:
score: 10
Accepted
time: 94ms
memory: 1192kb
input:
10 80310 74048 68070 50202 75380 59528 84544 84640 55440 61056
output:
321195 923648 272235 167319 346712 391028 591616 1194160 1706544 1048896
result:
ok 10 lines
Test #4:
score: 10
Accepted
time: 0ms
memory: 1200kb
input:
10 449813415219 238127986797 141941948583 570931177507 599551449147 107803513771 877967309187 390005...
output:
0 0 0 0 0 0 0 0 0 0
result:
ok 10 lines
Test #5:
score: 10
Accepted
time: 0ms
memory: 1196kb
input:
10 10384276147 991130530443 376705130467 176660198029 71835888101 243351181691 106856013219 60580750...
output:
0 0 0 0 0 0 0 0 0 0
result:
ok 10 lines
Test #6:
score: 10
Accepted
time: 225ms
memory: 1192kb
input:
10 873655567624 887745378432 579208100112 627322249728 674458211840 831871918080 671762267568 957053...
output:
19876945147444 39898371166272 105598519728048 26281498777344 155583031642880 489902085550080 1...
result:
ok 10 lines
Test #7:
score: 10
Accepted
time: 216ms
memory: 1192kb
input:
10 588091392000 526120252000 785151270000 960505337304 890742975972 536233040000 611974995856 541092...
output:
255078254592000 24802807358000 31563079290000 22008428480628 12641618150472 217959799400000 17...
result:
ok 10 lines
Test #8:
score: 10
Accepted
time: 173ms
memory: 1196kb
input:
10 884417667840 852117801984 871881667200 551138972160 644606815160 907614111040 557989228800 917485...
output:
496472754435840 123788083728384 346773935764800 28186820371200 25118009909300 166849427530240 ...
result:
ok 10 lines
Test #9:
score: 10
Accepted
time: 164ms
memory: 1192kb
input:
10 830832396800 727006448672 696814829568 608595958080 765299682306 822293683200 990128124000 833599...
output:
105081605000960 20185119355632 258275379068928 282896416256256 11726240868963 359261498880000 ...
result:
ok 10 lines
Test #10:
score: 10
Accepted
time: 172ms
memory: 1196kb
input:
10 792907836240 851587632744 993304166400 507384285600 873921498624 866248782720 697501279180 813957...
output:
36894114384240 9366377004084 685633484390400 22196917839600 34956857518848 105821718077760 571...
result:
ok 10 lines
Extra Test:
score: 0
Extra Test Passed