题目地址: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;}}

转载于:https://www.cnblogs.com/jingqi814/p/3581618.html

zoj 2313 Chinese Girls' Amusement(2-A)相关推荐

  1. zoj 2313 Chinese Girls' Amusement

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2313 要想满足要求 k 必须和 n 是互质的 当球第一次回到起点是  球走 ...

  2. ZOJ2313 Chinese Girls' Amusement(大数运算,找规律)

    题目: Chinese Girls' Amusement Time Limit: 2 Seconds       Memory Limit: 65536 KB You must have heard ...

  3. ACdream 1210 Chinese Girls' Amusement

    题目链接:http://115.28.76.232/problem?pid=1210 Problem Description You must have heard that the Chinese ...

  4. ZOJ 3228 Searching the String (AC自动机)

    题意: 给你一个模板串, 和n 个要匹配的串, 匹配串有两种类型, 第一种 可以在模板串中 重叠 出现, 另一种不可以重叠, 问每个串的两种形式 所出现的数量. 思路: 很明显ac自动机. 我们先把所 ...

  5. ZOJ 1423 (Your)((Term)((Project))) (模拟+数据结构)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=423 Sample Input 3 (A-B + C) - (A+(B ...

  6. ZOJ 2747 Paint the Wall(离散化+暴力)题解

    题意:给你一个面,然后涂颜色,问你最后剩多少颜色,每种颜色面积. 思路:第一反应是二维线段树,代码又臭又长,可以做.但是这题暴力+离散化就可以过.可以看到他给的n只有100,也就是说最坏情况下会涂10 ...

  7. ZOJ 1004 Anagrams by Stack(DFS+数据结构)

    Anagrams by Stack 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4 题目大意:输入两个字符串序列,判 ...

  8. ZOJ 3962 Seven Segment Display(数位DP)题解

    题意:给一个16进制8位数,给定每个数字的贡献,问你贡献和. 思路:数位DP,想了很久用什么表示状态,看题解说用和就行,其他的都算是比较正常的数位DP. 代码: #include<iostrea ...

  9. ZOJ 4062 Plants vs. Zombies(二分答案)

    题目链接:Plants vs. Zombies 题意:从1到n每个位置一棵植物,植物每浇水一次,增加ai高度.人的初始位置为0,人每次能往左或往右走一步,走到哪个位置就浇水一次.求m步走完后最低高度的 ...

最新文章

  1. Swing之JTable的详细介绍(转)
  2. Theano2.1.1-基础知识之准备工作
  3. AtCoder AGC001F Wide Swap (线段树、拓扑排序)
  4. linux java 部署 生产环境
  5. 我眼中BA(业务需求分析师)的技能广度和深度
  6. DELL Latitude E5400 装了PC DOS 7.1系统启动不了
  7. win10如何用管理员权限去运行服务器,让Visualstudio在win10下使用管理员方式运行...
  8. 海康ehome协议及应用
  9. 网站扫码登录时怎么一回事?
  10. Linux 硬盘故障修复
  11. Origin无法修改语言为灰色
  12. php获取qq头像地址,获取 QQ 头像地址,并且不暴露 QQ 号
  13. 高通使用/system/bin/r读取msm8916的gpio配置
  14. spread 超链接跳转sheet 不触发 GC.Spread.Sheets.Events.ActiveSheetChanged 事件处理
  15. 新年将至, 程序员如何以代码送出新春祝福
  16. Android dex修复工具,安卓热修复----手动加载dex文件到设备并执行
  17. 详述 MIMIC 数据库26张数据表(一)
  18. Fisco技术文档总结1---搭建第一个区块链网络
  19. TEA XTEA XXTEA
  20. 软件系统维护是一项不吸引人的工作_工作流程管理系统六大特点,助您工作更高效...

热门文章

  1. Android数据储存之SharedPreferences总结
  2. WMI服务故障,VBS脚本无法运行错误
  3. 《Pro ASP.NET MVC 3 Framework》学习笔记之二十四【Controllers和Actions】
  4. Java程序员,上班那点事儿
  5. 版本控制系统(译文) 2 - 基础
  6. ElasticSearch的搜索推荐(typeahead)
  7. 基于Xilinx Spartan-7 FPGA实现SMC接口
  8. (71)FPGA面试题-使用不同的代码实现2-4译码器?使用case语句
  9. 关于装配学校计算机教室报告,计算机室工作计划
  10. springboot static访问不到_Spring Boot 的静态资源处理