标签:
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2974 Accepted Submission(s): 843
1 #include<stdio.h> 2 #include<algorithm> 3 #include<iostream> 4 #include<stdlib.h> 5 #include<string.h> 6 #include<queue> 7 #include<map> 8 typedef long long LL; 9 using namespace std; 10 bool prime[400005]; 11 int ans[400005]; 12 queue<int>que; 13 int cm[400005]; 14 int ma[400005]; 15 int flag[2000]; 16 int gcd(int n,int m); 17 map<int,int>my; 18 int main(void) 19 { 20 int i,j,k; 21 int p,q; 22 int n,m; 23 int x,y; 24 int t; 25 for(i=2; i<=2000; i++) 26 { 27 if(!prime[i]) 28 { 29 for(j=i; i*j<=400000; j++) 30 { 31 prime[i*j]=true; 32 } 33 } 34 } 35 int cnt=0; 36 for(i=2; i<=400000; i++) 37 { 38 if(!prime[i]) 39 { 40 ans[cnt++]=i; 41 } 42 } 43 scanf("%d",&k); 44 while(k--) 45 { my.clear(); 46 scanf("%d %d",&p,&q); 47 int bt=1; 48 for(i=0; i<=p; i++) 49 ma[i]=i; 50 while(q--) 51 { 52 scanf("%d",&n); 53 if(n==1) 54 { 55 scanf("%d %d %d",&x,&y,&t); 56 int r=0; 57 int yy=0; 58 int xy=t; 59 if(x>y)swap(x,y); 60 while(t>1) 61 { 62 if(r==0&&t%ans[yy]==0) 63 { 64 r=1; 65 que.push(ans[yy]); 66 t/=ans[yy]; 67 } 68 else if(r==1&&t%ans[yy]==0) 69 { 70 t/=ans[yy]; 71 } 72 else 73 { 74 r=0; 75 yy++; 76 } 77 } 78 x-=1; 79 int ak=0; 80 while(!que.empty()) 81 { 82 cm[ak++]=que.front(); 83 que.pop(); 84 } 85 LL sum1=0; 86 LL sum2=0; 87 for(i=1; i<=(1<<ak)-1; i++) 88 { 89 int nn=0; 90 LL dp=1; 91 for(j=0; j<ak; j++) 92 { 93 if(i&(1<<j)) 94 { 95 nn++; 96 dp*=(LL)cm[j]; 97 } 98 } 99 if(nn%2) 100 { 101 LL ct=y/dp; 102 LL qt=x/dp; 103 LL ct1=ct+1; 104 LL qt1=qt+1; 105 sum1+=dp*ct1*ct/2; 106 sum2+=dp*qt1*qt/2; 107 } 108 else 109 { 110 LL ct=y/dp; 111 LL qt=x/dp; 112 LL ct1=ct+1; 113 LL qt1=qt+1; 114 sum1-=dp*ct1*ct/2; 115 sum2-=dp*qt1*qt/2; 116 } 117 } LL xc=1+x;LL yc=1+y; 118 LL ap1=(long long )(xc)*(long long)x/2; 119 LL ap2=(long long)(yc)*(long long)y/2; 120 ap2-=sum1; 121 ap1-=sum2; 122 ap2-=ap1; 123 for(j=1; j<bt; j++) 124 { 125 if(flag[j]>=x+1&&flag[j]<=y) 126 { 127 int cp=gcd(xy,ma[flag[j]]); 128 int cq=gcd(xy,flag[j]); 129 if(cq==1) 130 ap2-=flag[j]; 131 if(cp==1) 132 ap2+=ma[flag[j]]; 133 } 134 } 135 printf("%lld\n",ap2); 136 } 137 else 138 { 139 scanf("%d %d",&x,&t); 140 if(my[x]==0) 141 {my[x]=1;flag[bt]=x;bt++;} 142 ma[x]=t; 143 } 144 } 145 } 146 return 0; 147 } 148 int gcd(int n,int m) 149 { 150 if(m==0) 151 return n; 152 else if(n%m==0) 153 return m; 154 else return gcd(m,n%m); 155 }
标签:
原文地址:http://www.cnblogs.com/zzuli2sjy/p/5459853.html