Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
#include <iostream> #include <cstdio> #include <cstdlib> #include <cmath> #include <algorithm> #include <climits> #include <cstring> #include <string> #include <set> #include <bitset> #include <map> #include <queue> #include <stack> #include <vector> #define rep(i,m,n) for(i=m;i<=n;i++) #define mod 1000000007 #define inf 0x3f3f3f3f #define vi vector<int> #define pb push_back #define mp make_pair #define fi first #define se second #define ll long long #define pi acos(-1.0) #define pii pair<int,int> #define sys system("pause") const int maxn=1e5+10; using namespace std; inline ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);} inline ll qpow(ll p,ll q){ll f=1;while(q){if(q&1)f=f*p;p=p*p;q>>=1;}return f;} inline void umax(ll &p,ll q){if(p<q)p=q;} inline void umin(ll &p,ll q){if(p>q)p=q;} inline ll read() { ll x=0;int f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;ch=getchar();} return x*f; } int n,m,k,t,cnt,fac[maxn],cas; int x,y; void init(int x) { cnt=0; if(x%2==0){ fac[++cnt]=2; while(x%2==0)x/=2; } for(int i=3;(ll)i*i<=x;i+=2) { if(x%i==0) { fac[++cnt]=i; while(x%i==0)x/=i; } } if(x>1)fac[++cnt]=x; } ll gao(ll x) { ll ret=0; for(int i=1;i<(1<<cnt);i++) { ll num=0,now=1; for(int j=0;j<cnt;j++) { if(i&(1<<j)) { ++num; now*=fac[j+1]; } } if(num&1)ret+=x/now; else ret-=x/now; } return x-ret; } int main() { int i,j; scanf("%d",&t); while(t--) { scanf("%d%d%d%d%d",&x,&x,&y,&y,&k); if(!k) { printf("Case %d: 0\n",++cas); continue; } x/=k,y/=k; if(x>y)swap(x,y); ll ret=0; rep(i,1,x) { init(i); ret+=gao(y)-gao(i-1); } printf("Case %d: %lld\n",++cas,ret); } return 0; }
原文:http://www.cnblogs.com/dyzll/p/6358259.html