标签:style blog http color os io strong for
Problem A Play with Floor and Ceil http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=18&page=show_problem&problem=1614
扩展gcd求解2元不定式 ax+by=c d=gcd(a,b)=ax+by; x=x*c/d y=y*c/d
1 #include<cstdio> 2 typedef long long LL; 3 LL ext_gcd(LL a,LL b,LL &x,LL &y) { //扩展gcd d=gcd(a,b)=a*x+b*y; return d,x,y; 4 LL t,ret; 5 if(!b) { 6 x=1; 7 y=0; 8 return a; 9 } 10 ret=ext_gcd(b,a%b,x,y); 11 t=x; 12 x=y; 13 y=t-a/b*y; 14 return ret; 15 } 16 int main(){ 17 int t,x,k; 18 while(~scanf("%d",&t)){ 19 while(t--){ 20 scanf("%d%d",&x,&k); 21 LL a=x/k; 22 LL b=a; 23 if(x%k) b++; 24 LL tx,ty; 25 LL d=ext_gcd(a,b,tx,ty); 26 printf("%lld %lld\n",tx*x/d,ty*x/d); 27 } 28 } 29 return 0; 30 }
Alternate Task http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=117&page=show_problem&problem=2828
求因子的和
1 #include<cstdio> 2 int ds_func(int n){//求n所有除数的和 3 int ret=1,m=n,t; 4 for(int i=2;i*i<=n;i+=(i==2)?1:2){ 5 if(!(n%i)){ 6 t=i*i; 7 n/=i; 8 while(!(n%i)){ 9 t*=i; 10 n/=i; 11 } 12 ret*=(t-1)/(i-1); 13 } 14 } 15 return n>1?ret*(n+1):ret; 16 } 17 int main(){ 18 int s,cas=1; 19 while(~scanf("%d",&s),s){ 20 int ans=-1; 21 for(int i=s;i>=1;i--){ 22 if(ds_func(i)==s){ 23 ans=i; 24 break; 25 } 26 } 27 printf("Case %d: %d\n",cas++,ans); 28 } 29 return 0; 30 }
找规律,发现3*k+7是素数值为1,否则为0,素数筛法。
1 #include<cstdio> 2 #include<cstring> 3 #define mt(a,b) memset(a,b,sizeof(a)) 4 const int M=3000010; 5 int pri[M],mark[M],pricnt;//mark[i]存i的最小因子,素数时mark[i]==i 6 void sieve_primes() { //筛素数 7 pricnt=0; 8 mt(mark,0); 9 mark[0]=mark[1]=1; 10 for(int i=2; i<M; i++) { 11 if(!mark[i]) pri[pricnt++]=mark[i]=i; 12 for(int j=0; pri[j]*i<M; j++) { 13 mark[i*pri[j]]=pri[j]; 14 if(!(i%pri[j])) break; 15 } 16 } 17 } 18 int ans[M]; 19 int main(){ 20 sieve_primes(); 21 ans[0]=0; 22 for(int i=1;i<=1000000;i++){ 23 int j=3*i+7; 24 ans[i]=ans[i-1]; 25 if(mark[j]==j) ans[i]++; 26 } 27 int t,n; 28 while(~scanf("%d",&t)){ 29 while(t--){ 30 scanf("%d",&n); 31 printf("%d\n",ans[n]); 32 } 33 } 34 return 0; 35 }
1 #include<cstdio> 2 typedef long long LL; 3 LL mulmod(LL a,LL b,LL c) { //ret=(a*b)%c 4 LL ret=0; 5 for(; b; a=(a<<1)%c,b>>=1) { 6 if(b&1) { 7 ret=(ret+a)%c; 8 } 9 } 10 return ret; 11 } 12 LL powmod(LL a,LL b,LL c) { //ret=(a^b)%mod 13 LL ret=1%c; 14 for(; b; a=mulmod(a,a,c),b>>=1) { 15 if(b&1) { 16 ret=mulmod(ret,a,c); 17 } 18 } 19 return ret; 20 } 21 bool suspect(LL a,int s,LL d,LL n) { 22 LL x=powmod(a,d,n); 23 if(x==1) return true; 24 while(s--) { 25 if(x==n-1) return true; 26 x=mulmod(x,x,n); 27 } 28 return false; 29 } 30 const int test[]= {2,3,5,7,11,13,17,19,23,29,-1}; // for n < 10^16 31 bool isprime(LL n) { //Miller-Rabin 大素数测试 32 if(n<=1||(n>2&&(!(n&1)))) return false; 33 LL d=n-1; 34 int s=0; 35 while(!(d&1)) { 36 s++; 37 d>>=1; 38 } 39 for(int i=0; test[i]<n&&~test[i]; i++) { 40 if(!suspect(test[i],s,d,n)) return false; 41 } 42 return true; 43 } 44 int main(){ 45 int t,a,b; 46 while(~scanf("%d",&t)){ 47 while(t--){ 48 scanf("%d%d",&a,&b); 49 if(a){ 50 LL t=a*a+2*b*b; 51 if(isprime(t)){ 52 puts("Yes"); 53 continue; 54 } 55 } 56 puts("No"); 57 } 58 } 59 return 0; 60 }
end
标签:style blog http color os io strong for
原文地址:http://www.cnblogs.com/gaolzzxin/p/3911912.html