zoj 2313 Chinese Girls' Amusement(2-A)

题目地址:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1313


这题的理论就是当且仅当(a,m)=1  时  a*k+b (1<=k<=m)  遍历mod m的完系  


一开始的感觉就是暴力  从n/2 开始减 ,直到互素为止,于是直接用java大整数写了:

import java.math.*;
import java.util.*;
import java.io.*;
public class Main {
	
	public static void main(String[] args) throws Exception {
		
		
	    Scanner cin=new Scanner(System.in);
	    
	    int size=cin.nextInt();
	    for(int l=0;l<size;l++)
	    {	
	    BigInteger n=cin.nextBigInteger();
	    BigInteger k=n.divide(new BigInteger("2"));
	    
	    while(n.gcd(k).compareTo(BigInteger.ONE)!=0)
	    {
	      k=k.subtract(BigInteger.ONE);
	    }
         
	    
	    System.out.println(k);
	    if(l<size-1)   System.out.println();
	    }
	  } 
	   
}


竟然ac了 ,为啥不会超时?

理论解释是,当n=2*k+1 时 ,结果就是k,  当n=4*k时 结果就是2*k-1,  当n=4*k+2 时,结果就是2*k-2,  所以答案都离n/2距离很近,当然就不会超时了。

有了理论的分析 ,用c++也比较简单

但是还是java代码比较简洁~

#include<iostream>
#include<cstring>
using namespace std;

void jminus(int a [],int n,int b)
{
  a[n-1]-=b;
  for(int i=n-1;i>0;i--)
  {
     if(a[i]<0)
     {
        a[i]+=10;
        a[i-1]-=1;
     }
  }
}
void divide(int a [],int n )
{


  for(int i=0;i<n-1;i++)
   {
      if(a[i]%2==0)  a[i]=a[i]/2;
      else
      {
         a[i]=a[i]/2;
         a[i+1]+=10;
      }


   }
    a[n-1]/=2;
}


int main()
{

  int size=0;
  cin>>size;
  for(int l=0;l<size;l++)
  {

  char p[2010];
  cin>>p;
  int n=strlen(p);
  int a[n];

  for(int i=0;i<n;i++)
   a[i]=p[i]-'0';

  if(a[n-1]%2==1)
  {
      divide(a,n);
    int start=0;
    while(a[start]==0)  start++;

    for(int i=start;i<n;i++)
    cout<<a[i];
    cout<<endl;
   }
   else
   {

     if(n==1)
     {
        
        int temp=a[0];
        if(temp%4==2)
        {
          divide(a,n);
          jminus(a,n,2);

        }

        else
        {
            divide(a,n);
            jminus(a,n,1);
        }
     }

     else
     {
        int temp=a[n-1]+10*a[n-2];
        if(temp%4==2)
        {
          divide(a,n);
          jminus(a,n,2);
        }

        else
        {
             divide(a,n);
             jminus(a,n,1);
        }


     }

     int start=0;
    while(a[start]==0)  start++;

    for(int i=start;i<n;i++)
    cout<<a[i];
    cout<<endl;

   }

  if(l<size-1) cout<<endl;
  }


}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值