Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2577 Accepted Submission(s): 1102
If two trees and Sherlock are in one line, Farmer Sherlock can only see the tree nearest to him.
#include <stdio.h> #include <string.h> using namespace std; typedef long long LL; int e[50]; void devide(int n,int &id){ for(int i=2;i*i<=n;i++){ if(n%i==0){ e[id++] = i; while(n%i==0) n/=i; } } if(n>1) e[id++] = n; } int main() { int n,m; int tcase; scanf("%d",&tcase); while(tcase--){ scanf("%d%d",&n,&m); LL ans = m; for(int i=2;i<=n;i++){ int id = 0; devide(i,id); int sum = 0; for(int j=1;j<(1<<id);j++){ int cnt = 0,l=1; for(int k=0;k<id;k++){ if((j>>k)&1){ cnt++; l*=e[k]; } } if(cnt&1){ sum+=m/l; }else{ sum-=m/l; } } ans+=(LL)(m-sum); } printf("%lld\n",ans); } }