小易经常沉迷于网络游戏.有一次,他在玩一个打怪升级的游戏,他的角色的初始能力值为 a.在接下来的一段时间内,他将会依次遇见n个怪物,每个怪物的防御力为b1,b2,b3...bn. 如果遇到的怪物防御力bi小于等于小易的当前能力值c,那么他就能轻松打败怪物,并 且使得自己的能力值增加bi;如果bi大于c,那他也能打败怪物,但他的能力值只能增加bi 与c的最大公约数.那么问题来了,在一系列的锻炼后,小易的最终能力值为多少?
对于每组数据,第一行是两个整数n(1≤n<100000)表示怪物的数量和a表示小易的初始能力值. 然后输入n行,每行整数,b1,b2...bn(1≤bi≤n)表示每个怪物的防御力
对于每组数据,输出一行.每行仅包含一个整数,表示小易的最终能力值
3 50 50 105 200 5 20 30 20 15 40 100
110 205
import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.IOException; public class Main { public static void main(String[] args) throws IOException { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); String line; while((line = br.readLine()) != null){ String[] params = line.split(" "); int n = Integer.parseInt(params[0]); int a = Integer.parseInt(params[1]); for(int i = 0; i < n; i++){ int b = Integer.parseInt(br.readLine()); if(a >= b){ a += b; }else{ a += gcd(b, a); } } System.out.println(a); } } private static int gcd(int n, int m) { if(m == 0){ return n; } return gcd(m, n % m); } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNextInt()){ int n = sc.nextInt(); int a = sc.nextInt(); int[] nums = new int[n]; for(int i = 0;i < n;i++){ nums[i] = sc.nextInt(); if(nums[i] <= a) a += nums[i]; else a += method(a,nums[i]); } System.out.println(a); } } private static int method(int a ,int b){ //辗转相除法 while(b != 0){ int r = a % b; a = b; b = r; } return a; } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); int a = sc.nextInt(); for(int i = 0;i<n;i++){ int bi = sc.nextInt(); if(bi < a){ a += bi; }else{ a += getDivisor(a,bi); } } System.out.println(a); } } public static int getDivisor(int c,int b){ //这时候的c一定是小于bi的 long temp; if(b == 0) return c; else return getDivisor(b,c%b); } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int num = sc.nextInt(); int pow = sc.nextInt(); for(int i=0;i<num;i++){ int number = sc.nextInt(); if(pow > number) pow += number; else pow += gcd(pow,number); } System.out.println(pow); } } static int gcd(int n1,int n2){ if(n1 % n2 == 0) return n2; return gcd(n2,n1%n2); } }
import java.util.*; import java.io.*; public class Main{ public static void main(String[] args) throws IOException{ int n,a; BufferedReader bf = new BufferedReader(new InputStreamReader(System.in)); String line = null; while((line=bf.readLine())!=null){ String[] str1 = line.split(" "); n=Integer.parseInt(str1[0]); a=Integer.parseInt(str1[1]); int[] b=new int[n]; String[] str2 = bf.readLine().split(" "); for(int i=0;i<n;i++){ b[i]=Integer.parseInt(str2[i]); } for(int i=0;i<n;i++){ if(a>=b[i]) a+=b[i]; else{ int p=a; int q=b[i]; while(p%q!=0){ int temp=p %q; p=q; q=temp; } a+=q; } } System.out.println(a); } } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner sc = new Scanner(System.in); while(sc.hasNext()){ int n = sc.nextInt(); int a = sc.nextInt(); for(int i = 0; i < n; i++){ int cur = sc.nextInt(); if(a >= cur) a += cur; else a += maxGCD(a, cur); } System.out.println(a); } sc.close(); } private static int maxGCD(int a, int cur){ if(cur == 0) return a; return maxGCD(cur, a % cur); } }
import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class SmallYiUp { public static void main(String args[]){ Scanner sc =new Scanner(System.in); List<Integer> list = new ArrayList<>(); while (sc.hasNext()) { int monsterNumber = sc.nextInt(); int power = sc.nextInt(); int[] moster = new int[monsterNumber]; for (int i = 0; i < monsterNumber; i++) { moster[i] = sc.nextInt(); } for (int i = 0; i < moster.length; i++) { if (power > moster[i]) { power = power + moster[i]; } else { power = power + isPublicYueNum(power, moster[i]); } } list.add(power); } for (int i = 0;i<list.size();i++){ System.out.println(list.get(i)); } } public static int isPublicYueNum(int xiaoYi,int monster){ int num = 0; for (int i = xiaoYi;i>0;i--){ if(xiaoYi%i==0&&monster%i==0){ num = i; break; } } return num; } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); int a = sc.nextInt(); int[] b = new int[n]; for (int i = 0; i < n; i++) { b[i] = sc.nextInt(); if (b[i] <= a) { a += b[i]; } else { a += gcd(a, b[i]); } } System.out.println(""+a); } sc.close(); } private static int gcd(int a, int b) { if (b % a == 0) { return a; } else { return gcd(b % a, a); } } }
import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNextInt()){ int n = sc.nextInt(); int ambition = sc.nextInt(); for (int i = 0; i < n; i++) { int temp = sc.nextInt(); if(ambition > temp) ambition += temp; else ambition += gcd(ambition,temp); } System.out.println(ambition); } } public static int gcd(int x,int y){//求最大公约数 while (y != 0){ int temp = y; y = x % y; x = temp; } return x; } }
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); while (sc.hasNext()) { int n = sc.nextInt(); int[] arr = new int[n + 1]; arr[0] = sc.nextInt(); for (int i = 1; i < n + 1; i ++ ) { arr[i] = sc.nextInt(); } for (int i = 1; i < arr.length; i ++ ) { if(arr[i] <= arr[i - 1]) arr[i] += arr[i - 1]; else arr[i] = arr[i - 1] + gongyue(arr[i], arr[i - 1]); } System.out.println(arr[n]); } } public static int gongyue(int x, int y) { int min = x > y ? y : x; int max = x == min ? y : x; while (max % min != 0) { int temp = max % min; max = min; min = temp; } return min; } }
import java.util.*; public class Main{ public static void main(String[] args){ Scanner in = new Scanner(System.in); while(in.hasNext()){ int n = in.nextInt(); int a = in.nextInt(); int[] b = new int[n]; for(int i = 0; i < n; i++){ b[i] = in.nextInt(); } for(int j = 0; j < n; j++){ if(b[j] <= a){ a += b[j]; }else{ a += GCD(a,b[j]); } } System.out.println(a); } } public static int GCD(int a, int b){ int gcd = 1; for(int i = 2; i <= Math.min(a,b); i++){ if((a%i == 0) && (b%i == 0)) gcd = i; } return gcd; } }
import java.util.*; public class Main { public static void main(String[] args) { //先构造对应的输入输出格式 Scanner scanner=new Scanner(System.in); //获取输入判断 while(scanner.hasNext()) { //初始化 int n=scanner.nextInt(); int init=scanner.nextInt(); int[] monster=new int [n]; for(int i=0;i<n;i++) { //获取对应的数值 monster[i]=scanner.nextInt(); if(init>monster[i]) { init+=monster[i]; } else { //这时候一定monster大 init+=GetGYS(init,monster[i]); } } System.out.println(""+init); } //关闭scanner scanner.close(); } //获取对应最大公约数 public static int GetGYS(int n,int m) { if(m%n==0) return n; else { return GetGYS(m%n,n); } } }