first commit

This commit is contained in:
Nyagami
2022-12-15 23:51:04 +07:00
commit 87d51fb251
292 changed files with 12623 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
public class Main{
public static void main(String[] args){
System.out.println("Hello PTIT.");
}
}
+18
View File
@@ -0,0 +1,18 @@
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;
public class Main{
public static void main(String[] args){
File file = new File("Hello.txt");
try{
Scanner scaner = new Scanner(file);
while(scaner.hasNextLine()){
System.out.println(scaner.nextLine());
}
scaner.close();
}catch(FileNotFoundException e){
}
}
}
+62
View File
@@ -0,0 +1,62 @@
package nyagami;
import java.io.*;
import java.util.*;
import view.InvoiceView;
import vn.edu.ptit.*;
/**
*
* @author hoang
*/
public class Nyagami {
static class PaymentController{
private Invoice invoice;
public PaymentController(){
Scanner sc = new Scanner(System.in);
Student st = new Student(sc.nextLine(), sc.nextLine());
int n_subject = Integer.parseInt(sc.nextLine());
ArrayList<Subject> AlSubject = new ArrayList<>();
for(int i=0; i<n_subject;i++){
String code = sc.nextLine();
String name = sc.nextLine();
AlSubject.add(new Subject(name, code, Integer.parseInt(sc.nextLine())));
}
Rule rule = new Rule(sc.nextLine(), sc.nextLine(), Double.parseDouble(sc.nextLine()));
invoice = new Invoice(rule);
invoice.setSt(st);
invoice.setAlSubject(AlSubject);
double amount = 0;
for(Subject sj: AlSubject){
amount += sj.getCredit()*rule.getCreditPrice();
}
invoice.setAmount(amount);
}
public Invoice getInvoice(){
return invoice;
}
}
public static void main(String[] args) {
PaymentController pc = new PaymentController();
//Output for test
Invoice invoice = pc.getInvoice();
InvoiceView.show(invoice);
}
public static void main5399814(String[] args) {
PaymentController pc = new PaymentController();
//Output for test
Invoice invoice = pc.getInvoice();
InvoiceView.show(invoice);
}
}
+16
View File
@@ -0,0 +1,16 @@
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int a = reader.nextInt(), b = reader.nextInt();
if(a<=0 || b<=0){
reader.close();
System.out.print(0);
return;
}
String ans = String.format("%d %d",2*(a+b),a*b);
System.out.print(ans);
reader.close();
}
}
+14
View File
@@ -0,0 +1,14 @@
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int t = reader.nextInt();
while(t>0){
t--;
Long n = reader.nextLong();
System.out.println(n*(n+1)/2);
}
reader.close();
}
}
@@ -0,0 +1,15 @@
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
float a = reader.nextFloat(), b = reader.nextFloat();
if(a==0){
if(b!=0) System.out.println("VN");
else System.out.println("VSN");
}else{
System.out.printf("%.2f", -b/a);
}
reader.close();
}
}
+25
View File
@@ -0,0 +1,25 @@
import java.util.Scanner;
public class Main {
static boolean nt(int n){
if(n<2) return false;
if(n==2) return true;
if(n%2==0) return false;
for(int i=3;i<=Math.sqrt(n);i+=2){
if(n%i==0) return false;
}
return true;
}
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
int t = reader.nextInt();
while(t>0){
t--;
int n = reader.nextInt();
if(nt(n)) System.out.println("YES");
else System.out.println("NO");
}
reader.close();
}
}
+19
View File
@@ -0,0 +1,19 @@
import java.util.Scanner;
public class Main {
public static void main(String[] args){
Scanner reader = new Scanner(System.in);
int t = reader.nextInt();
while(t>0){
t--;
double n = reader.nextDouble(), h = reader.nextDouble();
for(double i = 1; i < n; i++){
double p = i/n;
double curh = h*(Math.sqrt(p));
System.out.printf("%.6f ",curh);
}
System.out.println("");
}
reader.close();
}
}
+16
View File
@@ -0,0 +1,16 @@
import java.util.Scanner;
public class Main {
public static void main(String[] args){
long[] f = new long[94];
f[0]=0; f[1]=1;
for(int i=2;i<=92;i++) f[i]=f[i-1]+f[i-2];
Scanner reader = new Scanner(System.in);
int t = reader.nextInt();
while(t>0){
t--;
System.out.println(f[reader.nextInt()]);
}
reader.close();
}
}
+26
View File
@@ -0,0 +1,26 @@
import java.util.Scanner;
public class Main {
public static void main(String[] args){
long[] f = new long[94];
f[0]=0; f[1]=1;
for(int i=2;i<=92;i++) f[i]=f[i-1]+f[i-2];
Scanner reader = new Scanner(System.in);
int t = reader.nextInt();
while(t>0){
t--;
long n = reader.nextLong();
if(isfi(n,f)) System.out.println("YES");
else System.out.println("NO");
}
reader.close();
}
static boolean isfi(long n,long[] f){
for(int i=0;i<=92;i++){
if(n==f[i]) return true;
}
return false;
}
}
@@ -0,0 +1,29 @@
package nyagami;
import java.util.Scanner;
public class test{
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int i=1; i<=t; i++){
int n = sc.nextInt();
System.out.printf("Test %d: ",i);
int can = (int) Math.sqrt((double)n);
for(int j=2; j<=can; j++){
int cnt=0;
while(n%j==0){
n/=j;
cnt++;
}
if(cnt>0){
System.out.printf("%d(%d) ", j, cnt);
}
}
if(n>1) System.out.printf("%d(1)", n);
System.out.println("");
}
sc.close();
}
}
+30
View File
@@ -0,0 +1,30 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package test;
import java.util.Scanner;
/**
*
* @author hoang
*/
public class Test {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
long n = reader.nextLong();
long ans = 0;
long[] f = new long[22];
f[0]=1;
for(int i=1; i<=n ; i++){
f[i]=f[i-1]*i;
ans+=f[i];
}
System.out.print(ans);
reader.close();
}
}
+37
View File
@@ -0,0 +1,37 @@
import java.util.Scanner;
public class Main {
static char convert(char x) {
if (x == '0')
return '0';
if (x == '1')
return '1';
if (x == '8')
return '0';
if (x == '9')
return '0';
return '2';
}
static String ans(char[] a) {
for (int i = 0; i < a.length; i++) {
a[i] = convert(a[i]);
if (a[i] == '2')
return "INVALID";
}
String s = new String(a);
s=s.replaceFirst("^0+", "");
if ("".equals(s)) return "INVALID";
return s;
}
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int t = Integer.parseInt(reader.nextLine());
for (int i = 0; i < t; i++) {
String s = reader.nextLine();
System.out.println(ans(s.toCharArray()));
}
reader.close();
}
}
@@ -0,0 +1,23 @@
import java.util.Scanner;
public class Main {
static long gcd(long a, long b){
if(b==0) return a;
return gcd(b,a%b);
}
static long lcm(long a,long b){
return a*b/gcd(a,b);
}
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int t = reader.nextInt();
for (int i = 0; i < t; i++) {
int a=reader.nextInt(), b=reader.nextInt();
System.out.printf("%d %d\n",lcm(a,b),gcd(a,b));
}
reader.close();
}
}
@@ -0,0 +1,28 @@
import java.util.Scanner;
public class Main {
static int count(int n){
if(n%2==1) return 0;
int can=(int) Math.sqrt((double) n);
int c=0;
for(int i=1;i<=can;i++){
if(n%i==0){
if(i%2==0) c++;
if((n/i)%2==0) c++;
}
}
if(can*can==n && can%2==0) c--;
return c;
}
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int t = reader.nextInt();
for (int i = 0; i < t; i++) {
int a=reader.nextInt();
System.out.println(count(a));
}
reader.close();
}
}
+30
View File
@@ -0,0 +1,30 @@
import java.util.Scanner;
public class Main {
static long[] f = new long[2000001];
public static void main(String[] args) {
for(int i=0;i<2000001;i++){
f[i]=0;
Boolean oke=true;
for(int j=2;j<=(int)Math.sqrt(i);j++){
if(i%j==0){
f[i]=f[j]+f[i/j];
oke=false;
break;
}
}
if(oke) f[i]=i;
}
Scanner reader = new Scanner(System.in);
long t = reader.nextLong();
long s=0;
for (long i = 0; i < t; i++) {
int a=reader.nextInt();
s+=f[a];
}
System.out.print(s);
reader.close();
}
}
@@ -0,0 +1,26 @@
import java.util.Scanner;
public class Main {
static long[] f = new long[2000001];
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
long t = reader.nextLong();
for (long i = 0; i < t; i++) {
long ans=1;
long a=reader.nextLong();
for(long j=2;j<=(long)Math.sqrt(a);j++){
if(a%j==0){
ans=j;
while(a%j==0){
a/=j;
}
}
}
if(a>1) ans=a;
System.out.println(ans);
}
reader.close();
}
}
@@ -0,0 +1,17 @@
import java.util.Scanner;
public class Main {
static long[] f = new long[2000001];
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
String s = reader.next();
int c=0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='4' || s.charAt(i)=='7') c++;
}
System.out.print((c==4 || c==7)?"YES":"NO");
reader.close();
}
}
+22
View File
@@ -0,0 +1,22 @@
import java.util.Scanner;
public class Main {
static String check(String s){
for(int i=1;i<s.length();i++){
int x=s.charAt(i)-s.charAt(i-1);
if(x!=1 && x!=-1) return "NO";
}
return "YES";
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++){
String s=sc.next();
System.out.println(check(s));
}
sc.close();
}
}
+25
View File
@@ -0,0 +1,25 @@
import java.util.Scanner;
public class Main {
static String check(String s){
int sum=s.charAt(0)-'0';
for(int i=1;i<s.length();i++){
sum+=s.charAt(i)-'0';
int x=s.charAt(i)-s.charAt(i-1);
if(x!=2 && x!=-2) return "NO";
}
if(sum%10!=0) return "NO";
return "YES";
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++){
String s=sc.next();
System.out.println(check(s));
}
sc.close();
}
}
@@ -0,0 +1,30 @@
import java.util.*;
public class non {
static long ans = (1<<10) - 1;
static void solve(long n){
if(n==0){
System.out.println("Impossible");
return;
}
long r = 0;
for(long i=1;i<1000000;++i){
long x=i*n;
while(x>0){
r|=(1<<(x%10));
x/=10;
}
if(r==ans){
System.out.println(i*n);
return;
}
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while(T-->0) solve(sc.nextLong());
}
}
+23
View File
@@ -0,0 +1,23 @@
import java.util.Scanner;
public class Main {
static long mod=1000000007;
static long mod(long a,long b){
if(b==0) return 1;
if(b%2==1) return mod(a,b-1)*a%mod;
long p=mod(a,b/2);
return p*p%mod;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(true){
long a=sc.nextLong(), b=sc.nextLong();
if(a==0 && b==0) break;
System.out.println(mod(a,b));
}
sc.close();
}
}
+31
View File
@@ -0,0 +1,31 @@
package nyagami;
import java.util.Scanner;
public class test{
static long[] f = new long[94];
static int Try(int n, long k){
if(n==1) return 0;
if(n==2) return 1;
if(k<=f[n-2]) return Try(n-2,k);
return Try(n-1,k-f[n-2]);
}
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
f[0]=0;
f[1]=1;
for(int i=2;i<94;i++){
f[i]=f[i-1]+f[i-2];
}
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
long k = sc.nextLong();
System.out.println(Try(n, k));
}
sc.close();
}
}
+63
View File
@@ -0,0 +1,63 @@
import java.util.*;
public class non {
static ArrayList<String> genn(String s){
ArrayList<String> a = new ArrayList<>();
if(s.charAt(0) == '?'){
for(char c = '1'; c <= '9'; c++) a.add(c + "" + s.charAt(1));
}else a.add(s);
if(s.charAt(1) == '?'){
ArrayList<String> ans = new ArrayList<>();
for(String i : a){
for(char c = '0'; c <= '9'; c++) ans.add(i.charAt(0) + "" + c);
}
return ans;
}
return a;
}
static String geno(String s){
if(s.charAt(0) == '?') return "+-";
return s;
}
static int cal(int a, int b, char op){
if(op == '+') return a+b;
return a-b;
}
static boolean check(String a, char op, String b, String c){
if("*/".indexOf(op) > -1) return false;
int x = Integer.parseInt(a);
int y = Integer.parseInt(b);
int z = Integer.parseInt(c);
return x>9 && y>9 && z>9 && cal(x, y, op) == z;
}
static void solve(String a, String op, String b, String equal, String c){
ArrayList<String> arr1 = genn(a);
ArrayList<String> arr2 = genn(b);
ArrayList<String> arr3 = genn(c);
op = geno(op);
for(String i:arr1){
for(String j:arr2){
for(String k:arr3){
for(int u=0;u<op.length();u++){
if(check(i, op.charAt(u), j, k)){
System.out.println(String.format("%s %c %s = %s", i, op.charAt(u), j, k));
return;
}
}
}
}
}
System.out.println("WRONG PROBLEM!");
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int T = sc.nextInt();
while(T-->0) solve(sc.next(), sc.next(), sc.next(), sc.next(), sc.next());
}
}
+21
View File
@@ -0,0 +1,21 @@
import java.util.Scanner;
public class Main {
static String check(String s){
for(int i=0;i<s.length();i++){
if("012".indexOf(s.charAt(i))==-1) return "NO";
}
return "YES";
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++){
String s=sc.next();
System.out.println(check(s));
}
sc.close();
}
}
+22
View File
@@ -0,0 +1,22 @@
package nyagami;
import java.util.Scanner;
public class test{
static int min4(int a,int b,int c,int d){
return Math.min(Math.min(Math.min(a, b),c),d);
}
static int max4(int a,int b,int c,int d){
return Math.max(Math.max(Math.max(a, b),c),d);
}
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int a1x = sc.nextInt(), a1y = sc.nextInt(), a2x = sc.nextInt(), a2y = sc.nextInt();
int b1x = sc.nextInt(), b1y = sc.nextInt(), b2x = sc.nextInt(), b2y = sc.nextInt();
int minx = min4(a1x,a2x,b1x,b2x), maxx = max4(a1x,a2x,b1x,b2x);
int miny = min4(a1y,a2y,b1y,b2y), maxy = max4(a1y,a2y,b1y,b2y);
int c = Math.max(maxx-minx, maxy-miny);
System.out.print(c*c);
sc.close();
}
}
+19
View File
@@ -0,0 +1,19 @@
import java.util.Scanner;
public class Main {
static String cp(int n){
int x=(int)Math.sqrt((double)n);
return x*x==n?"YES":"NO";
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++){
int s=sc.nextInt();
System.out.println(cp(s));
}
sc.close();
}
}
+23
View File
@@ -0,0 +1,23 @@
import java.util.Scanner;
public class Main {
static String check(int[] a, int n){
for(int i=0;i<n;i++){
if(a[i]!=a[n-i-1]) return "NO";
}
return "YES";
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int j=0;j<t;j++){
int n = sc.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++){
a[i] = sc.nextInt();
}
System.out.println(check(a,n));
}
sc.close();
}
}
+35
View File
@@ -0,0 +1,35 @@
package nyagami;
import java.util.Scanner;
import java.util.TreeSet;
public class test{
static boolean has(int[] a, int n, int x){
for(int i=0;i<n;i++) if(a[i]==x) return true;
return false;
}
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
TreeSet<Integer> h = new TreeSet<>();
int n = sc.nextInt(), m = sc.nextInt();
int[] a = new int[n];
int[] b = new int[m];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
for(int i=0;i<m;i++){
b[i]=sc.nextInt();
}
for(int i=0;i<n;i++){
if(has(b,m,a[i])){
h.add(a[i]);
}
}
h.forEach((k)->{
System.out.printf("%d ", k);
});
sc.close();
}
}
+33
View File
@@ -0,0 +1,33 @@
package nyagami;
import java.util.Scanner;
import java.util.TreeSet;
import java.util.HashSet;
public class test{
static boolean has(int[] a, int n, int x){
for(int i=0;i<n;i++) if(a[i]==x) return true;
return false;
}
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
TreeSet<Integer> h = new TreeSet<>();
HashSet<Integer> a = new HashSet<>();
HashSet<Integer> b = new HashSet<>();
int n = sc.nextInt(), m = sc.nextInt();
for(int i=0;i<n;i++){
a.add(sc.nextInt());
}
for(int i=0;i<m;i++){
b.add(sc.nextInt());
}
a.forEach(k -> h.add(k));
b.forEach(k -> h.add(k));
h.forEach((k)->{
System.out.printf("%d ", k);
});
sc.close();
}
}
@@ -0,0 +1,31 @@
import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int j=1;j<=t;j++){
System.out.printf("Test %d:\n",j);
int n=sc.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++){
a[i]=sc.nextInt();
}
Map<Integer,Integer> m = new HashMap<Integer,Integer>();
for(int i=0;i<n;i++){
if(m.get(a[i]) == null){
m.put(a[i], 1);
}else m.put(a[i], m.get(a[i])+1);
}
for(int i=0;i<n;i++){
if(m.get(a[i])>0){
System.out.printf("%d xuat hien %d lan\n",a[i],m.get(a[i]));
m.put(a[i], 0);
}
}
}
sc.close();
}
}
@@ -0,0 +1,24 @@
import java.util.Scanner;
public class Main {
static long gcd(long a,long b){
if(b==0) return a;
return gcd(b,a%b);
}
static long lcm(long a,long b){
return a*b/gcd(a,b);
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
long n=sc.nextLong();
long s=1;
for(int i=1;i<=n;i++){
s=lcm(s,i);
}
System.out.println(s);
}
sc.close();
}
}
+28
View File
@@ -0,0 +1,28 @@
import java.util.*;
import java.io.*;
public class Main {
static class pair{
public int first, second;
public pair(int a, int b){
first = a;
second = b;
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
// Scanner sc = new Scanner(new File("ONLINE.in"));
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<pair> a = new ArrayList<>();
for(int i=0;i<n;i++) a.add(new pair(sc.nextInt(), sc.nextInt()));
a.sort((pair x, pair b) -> {
return x.first - b.first;
});
int now = 0;
for(int i=0;i<n;i++){
if(a.get(i).first>=now) now=a.get(i).first+a.get(i).second;
else now+=a.get(i).second;
}
System.out.println(now);
}
}
@@ -0,0 +1,34 @@
package nyagami;
import java.util.Scanner;
public class test{
static void out(int[] a, int n, int step){
System.out.printf("Buoc %d: ",step);
for(int i=0;i<n;i++){
System.out.printf("%d ", a[i]);
}
System.out.println("");
}
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++){
a[i] = sc.nextInt();
}
for(int i=0;i<n-1;i++){
for(int j=i+1;j<n;j++){
if (a[i]>a[j]) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
out(a,n,i+1);
}
sc.close();
}
}
+36
View File
@@ -0,0 +1,36 @@
package nyagami;
import java.util.Scanner;
public class test{
static void out(int[] a, int n, int step){
System.out.printf("Buoc %d: ",step);
for(int i=0;i<n;i++){
System.out.printf("%d ", a[i]);
}
System.out.println("");
}
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++){
a[i] = sc.nextInt();
}
for(int i=0;i<n-1;i++){
int candindex = i;
for(int j=i+1;j<n;j++){
if (a[candindex]>a[j]) {
candindex=j;
}
}
int tmp = a[i];
a[i] = a[candindex];
a[candindex] = tmp;
out(a,n,i+1);
}
sc.close();
}
}
+27
View File
@@ -0,0 +1,27 @@
package nyagami;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
public class test{
static void out(ArrayList<Integer> a, int step){
System.out.printf("Buoc %d: ", step);
a.forEach(k -> System.out.printf("%d ", k));
System.out.println("");
}
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
ArrayList<Integer> a = new ArrayList<>();
for(int i=0;i<n;i++){
a.add(sc.nextInt());
Collections.sort(a);
out(a,i);
}
sc.close();
}
}
+44
View File
@@ -0,0 +1,44 @@
package nyagami;
import java.util.Scanner;
import java.util.ArrayList;
import java.util.Collections;
public class test{
static void out(int[] a, int n, int step){
System.out.printf("Buoc %d: ", step);
for(int i=0;i<n;i++){
System.out.printf("%d ", a[i]);
}
System.out.println("");
}
static boolean isSorted(int[] a, int n){
for(int i=0;i<n-1;i++){
if(a[i]>a[i+1]) return false;
}
return true;
}
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++){
a[i] = sc.nextInt();
}
for(int i=0;i<n-1;i++){
for(int j=0;j<n-1;j++){
if(a[j]>a[j+1]){
int tmp = a[j];
a[j] = a[j+1];
a[j+1] = tmp;
}
}
out(a,n,i+1);
if(isSorted(a,n)) break;
}
sc.close();
}
}
+29
View File
@@ -0,0 +1,29 @@
package nyagami;
import java.util.Scanner;
public class test{
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int[] a = new int[n+1];
for(int i=1;i<n+1;i++){
a[i] = sc.nextInt();
a[i]+=a[i-1];
}
boolean oke = true;
for(int i=2;i<n;i++){
if(a[i-1]==(a[n]-a[i])){
System.out.println(i);
oke = false;
break;
}
}
if(oke) System.out.println(-1);
}
sc.close();
}
}
+33
View File
@@ -0,0 +1,33 @@
import java.util.*;
import java.io.*;
public class Main {
static boolean check(long[] a){
for(int i=a.length-1;i>=2;i--){
int j=0, k=i-1;
while(j<k){
long x = a[j] + a[k];
if (x==a[i]) return true;
if(x>a[i]) k--;
else j++;
}
}
return false;
}
static long sq(Long x){
return x*x;
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
// Scanner sc = new Scanner(new File("ONLINE.in"));
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
long[] a = new long[n];
for (int i = 0; i < n; i++)
a[i] = sq(sc.nextLong());
if(check(a)) System.out.println("YES");
else System.out.println("NO");
}
}
}
+23
View File
@@ -0,0 +1,23 @@
package nyagami;
import java.util.Scanner;
import java.util.Stack;
public class test{
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
Stack<Integer> a = new Stack<>();
for(int i=0; i<n; i++){
int x = sc.nextInt();
if(a.empty()) a.push(x);
else{
if((a.lastElement()+x)%2==0) a.pop();
else a.push(x);
}
}
System.out.print(a.size());
sc.close();
}
}
+40
View File
@@ -0,0 +1,40 @@
import java.util.Scanner;
public class non {
static int N = 1000000;
static int[] U = new int[N+2];
static int pow(int a, int b){
if(b==0) return 1;
if(b%2==1) return pow(a,b-1)*a;
int p = pow(a, b>>1);
return p*p;
}
static void set(){
for(int i=2;i<=(int)Math.sqrt(N);++i){
if(U[i]==0){
U[i]=i;
for(int j=0;j<=N/i;++j) U[i*j] = i;
}
}
for(int i=2;i<=N;++i){
if(U[i]==0) U[i]=i+1;
else{
int n = i, x = 0;
while(n%U[i]==0){
n/=U[i];
x++;
}
if(n==1) U[i] = (pow(U[i], x+1) - 1)/(U[i]-1);
else U[i] = U[pow(U[i], x)] * U[n];
}
}
}
public static void main(String[] args) {
set();
Scanner sc = new Scanner(System.in);
int a = sc.nextInt(), b = sc.nextInt(), cnt = 0;
for(int i=a;i<=b;++i) if(U[i] - i > i) ++cnt;
System.out.println(cnt);
}
}
+30
View File
@@ -0,0 +1,30 @@
package nyagami;
import java.util.Scanner;
import java.util.Stack;
public class test{
static int n, k, cnt=0;
static void Try(int i, int start, int[] res){
if(i==k){
cnt++;
for(int j=0; j<k; j++){
System.out.printf("%d ",res[j]);
}
System.out.println("");
return;
}
for(int u=start; u<=n-k+i+1 ; u++){
res[i]=u;
Try(i+1,u+1,res);
}
}
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
n = sc.nextInt(); k = sc.nextInt();
int[] res = new int[k];
Try(0,1,res);
System.out.printf("Tong cong co %d to hop", cnt);
sc.close();
}
}
+30
View File
@@ -0,0 +1,30 @@
package nyagami;
import java.util.Scanner;
import java.util.Stack;
public class test{
static int n, k, cnt=0;
static void Try(int i, int start, int[] res){
if(i==k){
cnt++;
for(int j=0; j<k; j++){
System.out.print(res[j]);
}
System.out.print(" ");
return;
}
for(int u=start; u<=n-k+i+1 ; u++){
res[i]=u;
Try(i+1,u+1,res);
}
}
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
n = sc.nextInt(); k = sc.nextInt();
int[] res = new int[k];
Try(0,1,res);
System.out.printf("\nTong cong co %d to hop", cnt);
sc.close();
}
}
+36
View File
@@ -0,0 +1,36 @@
package nyagami;
import java.util.Scanner;
public class test{
static int n;
static void Try(int i,int[] a, boolean[] vs){
if(i==n){
for(int j=0; j<n; j++){
System.out.print(a[j]);
}
System.out.println("");
return;
}
for(int j=1; j<=n; j++){
if(!vs[j] && (i==0 || (i>0 && Math.abs(j-a[i-1])!=1))){
vs[j]=true;
a[i]=j;
Try(i+1,a,vs);
vs[j]=false;
}
}
}
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
n = sc.nextInt();
boolean[] visited = new boolean[n+1];
int[] a = new int[n];
Try(0,a,visited);
System.out.println("");
}
sc.close();
}
}
+29
View File
@@ -0,0 +1,29 @@
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), s = sc.nextInt(), n9 = 0;
String Max = "", Min = "";
if(n*9 < s || s == 0){
System.out.println("-1 -1");
return;
}
while(s>=9){
Max += "9";
n9++;
s-=9;
}
if(s!=0) Max += Integer.toString(s);
while(Max.length()<n) Max+="0";
if(n9 != n && s == 0){
n9--;
s=9;
}
if(s!=0 && n9+1 != n) System.out.print("1");
for(int i=0;i<n-n9-(s>0?2:0);i++) System.out.print("0");
if(s>0) System.out.print(n9+1!=n?s-1:s);
while(n9-->0) System.out.print("9");
System.out.printf(" %s",Max);
}
}
+41
View File
@@ -0,0 +1,41 @@
import java.util.*;
import java.io.*;
public class Main {
static ArrayList<String> ans;
static void Try(int end, int[] a, int sum, String s) {
if (sum % 2 == 1) {
System.out.println(s);
ans.add(s);
}
for (int i = a.length - 1; i > end; i--) {
Try(i, a, sum + a[i], s + a[i] + ' ');
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
// Scanner sc = new Scanner(new File("ONLINE.in"));
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int[] a = new int[n];
ans = new ArrayList<>();
for (int i = 0; i < n; i++)
a[i] = sc.nextInt();
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] < a[j]) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
Try(-1, a, 0, "");
Collections.sort(ans);
// ans.forEach(e -> System.out.println(e));
}
}
}
@@ -0,0 +1,47 @@
import java.util.*;
import java.io.*;
public class Main {
static boolean primr(int x) {
if (x < 2)
return false;
if (x == 2)
return true;
if (x % 2 == 0)
return false;
for (int i = 3; i <= (int) Math.sqrt(x); i += 2) {
if (x % i == 0)
return false;
}
return true;
}
static void Try(int end, int[] a, int sum, String s) {
if (primr(sum)) System.out.println(s);
for (int i = a.length - 1; i > end; i--) {
Try(i, a, sum + a[i], s + a[i] + ' ');
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
// Scanner sc = new Scanner(new File("ONLINE.in"));
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int[] a = new int[n];
for (int i = 0; i < n; i++)
a[i] = sc.nextInt();
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
if (a[i] < a[j]) {
int tmp = a[i];
a[i] = a[j];
a[j] = tmp;
}
}
}
Try(-1, a, 0, "");
}
}
}
@@ -0,0 +1,26 @@
import java.util.*;
import java.io.*;
public class Main {
static void Try(int start, int cnt, int k, int[] a, String s){
if(cnt==k){
System.out.println(s);
return;
}
for(int i=start; i<a.length;i++){
Try(i+1, cnt+1, k, a, s+a[i]+' ');
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
// Scanner sc = new Scanner(new File("ONLINE.in"));
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt(), k = sc.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++) a[i]=sc.nextInt();
Arrays.sort(a);
Try(0,0,k,a,"");
}
}
}
+28
View File
@@ -0,0 +1,28 @@
import java.util.*;
import java.io.*;
public class Main {
static int uppper_bound(int l, int r, int[] a, int x){
if(l>r) return l;
int mid = (l+r)/2;
if(a[mid] > x) return uppper_bound(l, mid - 1 , a, x);
return uppper_bound(mid + 1, r, a, x);
}
public static void main(String[] args) throws IOException, ClassNotFoundException {
// Scanner sc = new Scanner(new File("ONLINE.in"));
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt(), k = sc.nextInt();
int[] a = new int[n];
for(int i=0;i<n;i++) a[i]=sc.nextInt();
Arrays.sort(a);
long sum = 0;
for(int i=1;i<n;i++){
int index = uppper_bound(0, i-1, a, a[i]-k);
sum += (long)(i - index);
}
System.out.println(sum);
}
}
}
@@ -0,0 +1,35 @@
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// Scanner sc = new Scanner(new File("ONLINE.in"));
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
long k = sc.nextLong();
long[] a = new long[n];
for(int i=0;i<n;i++) a[i] = sc.nextLong();
int l = 0, r = 0;
long sum = a[l];
boolean check = false;
while(l<n){
if(sum == k){
check = true;
break;
}
if(l==r || sum < k){
r++;
if(r==n) break;
sum+=a[r];
}else if(sum > k){
sum-=a[l];
l++;
}
}
if(check) System.out.println("YES");
else System.out.println("NO");
}
}
}
+28
View File
@@ -0,0 +1,28 @@
import java.io.*;
import java.util.*;
public class non {
public static void main(String[] args) throws IOException {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
long k = sc.nextLong();
long[] a = new long[n];
for(int i=0;i<n;i++) a[i] = sc.nextLong();
Arrays.sort(a);
int cntn = 0;
for(long i:a) if(i<0) cntn++;
long sum = 0;
if(k<=cntn){
for(int i=0;i<k;i++) a[i] = -a[i];
}else{
for(int i=0;i<cntn;i++) a[i] = -a[i];
Arrays.sort(a);
k-=cntn;
if(k%2==1) a[0] = -a[0];
}
for(long i:a) sum+=i;
System.out.println(sum);
}
}
+27
View File
@@ -0,0 +1,27 @@
package nyagami;
import java.util.Scanner;
public class test{
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int last = 0;
int cnt = 0;
for(int i=0; i<n; i++){
int x = sc.nextInt();
if(x==last+1){
last=x;
continue;
}else{
cnt++;
for(int j=last+1; j<x; j++){
System.out.println(j);
}
last=x;
}
}
if(cnt==0) System.out.println("Excellent!");
sc.close();
}
}
+23
View File
@@ -0,0 +1,23 @@
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// Scanner sc = new Scanner(new File("ONLINE.in"));
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int[] a = new int[n];
int[] b = new int[n];
for(int i=0;i<n;i++) b[i] = a[i] = sc.nextInt();
Arrays.sort(a);
for(int i=0;i<n;i++){
if(b[i] == a[0]){
System.out.println(i);
break;
}
}
}
}
}
+29
View File
@@ -0,0 +1,29 @@
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt(), s = sc.nextInt(), n9 = 0;
String Max = "";
if(n*9 < s || s == 0){
System.out.println("-1 -1");
return;
}
while(s>=9){
Max += "9";
n9++;
s-=9;
}
if(s!=0) Max += Integer.toString(s);
while(Max.length()<n) Max+="0";
if(n9 != n && s == 0){
n9--;
s=9;
}
if(s!=0 && n9+1 != n) System.out.print("1");
for(int i=0;i<n-n9-(s>0?2:0);i++) System.out.print("0");
if(s>0) System.out.print(n9+1!=n?s-1:s);
while(n9-->0) System.out.print("9");
System.out.printf(" %s",Max);
}
}
@@ -0,0 +1,33 @@
import java.util.*;
public class Main {
static int gcd(int a, int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}
static int lcm(int a, int b) {
return a * b / gcd(a, b);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
int n = sc.nextInt();
int[] a = new int[n];
int[] b = new int[n + 1];
for (int i = 0; i < n; i++)
a[i] = sc.nextInt();
b[0] = a[0];
for (int i = 1; i < n; i++) {
b[i] = lcm(a[i], a[i - 1]);
}
b[n] = a[n - 1];
for (int i = 0; i < n + 1; i++) {
System.out.printf("%d ", b[i]);
}
System.out.println();
}
}
}
+22
View File
@@ -0,0 +1,22 @@
import java.util.*;
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException, ClassNotFoundException {
// Scanner sc = new Scanner(new File("ONLINE.in"));
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
sc.nextLine();
while (t-- > 0) {
String[] s = sc.nextLine().trim().split("\\s+");
int cnt = 0;
for (int i = 0; i < s.length; i++) {
if (Integer.parseInt(s[i]) % 2 == 0)
cnt++;
}
if (s.length % 2 == 0 && cnt > s.length - cnt || s.length % 2 == 1 && s.length - cnt > cnt)
System.out.println("YES");
else System.out.println("NO");
}
}
}
+29
View File
@@ -0,0 +1,29 @@
package nyagami;
import java.util.Scanner;
public class test{
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
int n = sc.nextInt();
int[][] a = new int[n][n];
for(int i=0; i<n; i++){
for(int j=0; j<n; j++) a[i][j] = sc.nextInt();
}
int j = 0;
for(int i=0; i<n; i++){
if(j==0){
for(; j<n; j++) System.out.printf("%d ", a[i][j]);
j=n-1;
}else{
for(; j>=0; j--) System.out.printf("%d ", a[i][j]);
j=0;
}
}
System.out.println("");
}
sc.close();
}
}
@@ -0,0 +1,50 @@
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int[][] a = new int[n][n];
int[] b = new int[n * n];
for (int i = 0; i < n * n; i++)
b[i] = sc.nextInt();
Arrays.sort(b);
int x = 0, y = 0, i = 0;
while (i < n * n) {
while (y < n && a[x][y] == 0) {
a[x][y] = b[i];
y++;
i++;
}
x++;
y--;
while (x < n && a[x][y] == 0) {
a[x][y] = b[i];
x++;
i++;
}
y--;
x--;
while (y >= 0 && a[x][y] == 0) {
a[x][y] = b[i];
y--;
i++;
}
x--;
y++;
while (x >= 0 && a[x][y] == 0) {
a[x][y] = b[i];
x--;
i++;
}
y++;
x++;
}
for (i = 0; i < n; i++) {
for (int j = 0; j < n; j++) {
System.out.printf("%d ", a[i][j]);
}
System.out.println("");
}
}
}
@@ -0,0 +1,45 @@
package nyagami;
import java.util.Scanner;
public class test{
static int[][] trans(int[][] a, int n, int m){
int[][] b = new int[m][n];
for(int i=0; i<m; i++){
for(int j=0; j<n; j++) b[i][j] = a[j][i];
}
return b;
}
static int[][] mul(int[][] a, int[][]b, int n, int m){
int res[][] = new int[n][n];
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
int sum = 0;
for(int k=0; k<m; k++) sum+=a[i][k]*b[k][j];
res[i][j] = sum;
}
}
return res;
}
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
for(int ts=1; ts<=t; ts++){
int n = sc.nextInt(), m = sc.nextInt();
int[][] a = new int[n][m];
for(int i=0; i<n; i++){
for(int j=0; j<m; j++) a[i][j] = sc.nextInt();
}
int[][] b = trans(a, n, m);
int[][] res = mul(a, b, n, m);
System.out.printf("Test %d:\n", ts);
for(int i=0; i<n; i++){
for(int j=0; j<n; j++){
System.out.printf("%d ", res[i][j]);
}
System.out.println("");
}
}
sc.close();
}
}
+21
View File
@@ -0,0 +1,21 @@
package nyagami;
import java.util.Scanner;
public class test{
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=1; i<=n; i++){
for(int j=1; j<=n; j++){
int x = sc.nextInt();
if(x==1 && i<j){
System.out.printf("(%d,%d)\n", i, j);
}
}
}
sc.close();
}
}
+21
View File
@@ -0,0 +1,21 @@
package nyagami;
import java.util.Scanner;
public class test{
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
for(int i=1; i<=n; i++){
System.out.printf("List(%d) = ", i);
for(int j=1; j<=n; j++){
int x = sc.nextInt();
if(x==1) System.out.printf("%d ", j);
}
System.out.println("");
}
sc.close();
}
}
+19
View File
@@ -0,0 +1,19 @@
package nyagami;
import java.util.Scanner;
public class test{
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int res = 0;
for(int i=1; i<=n; i++){
int cnt = 0;
for(int j=1; j<=3; j++) cnt+=sc.nextInt();
if(cnt > 3-cnt) res++;
}
System.out.print(res);
sc.close();
}
}
@@ -0,0 +1,33 @@
package pkg1.chuan_hoa_xau_ho_ten1;
import java.util.Scanner;
public class Chuan_hoa_xau_ho_ten1 {
static String edit(String s){
String s1 = s.substring(0, 1);
String s2 = s.substring(1, s.length());
s1 = s1.toUpperCase();
return s1+s2;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
sc.nextLine();
while(t>0){
t-=1;
String s = sc.nextLine();
s = s.toLowerCase();
s = s.replaceAll("\\s+", " ");
s = s.trim();
String word[] = s.split("\\s");
for( String i : word){
System.out.print(edit(i)+" ");
}
System.out.println("");
}
}
}
@@ -0,0 +1,35 @@
import java.util.Scanner;
import java.util.ArrayDeque;
import java.util.Queue;
public class Main{
static char up(char x){
if('a'<=x && x<='z') return (char)((int)x-32);
return x;
}
static Queue<String> re(String name){
String[] a = name.split("[' ']+");
Queue<String> r = new ArrayDeque<>();
for(int i=0; i<a.length; i++){
if(a[i].length() == 0) continue;
String s = Character.toString(up(a[i].charAt(0))) + a[i].substring(1);
r.add(s);
}
return r;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
while(sc.hasNextLine()){
String name = sc.nextLine();
if(name.length() == 0) continue;
Queue<String> q = re(name.toLowerCase());
String[] a = new String[q.size()];
q.toArray(a);
a[a.length-1] += ",";
for(int i=1; i<a.length; i++) System.out.printf("%s ",a[i]);
System.out.println(a[0].toUpperCase());
}
}
}
+22
View File
@@ -0,0 +1,22 @@
import java.util.Scanner;
public class Main {
static String check(String s){
int n=s.length();
for(int i=0;i<n;i++){
char x=s.charAt(i),y=s.charAt(n-i-1);
if(x!=y) return "NO";
if((x-'0')%2==1) return "NO";
}
return "YES";
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
String s=sc.next();
System.out.println(check(s));
}
sc.close();
}
}
+23
View File
@@ -0,0 +1,23 @@
import java.util.Scanner;
public class Main {
static String check(String s){
int n=s.length();
if(s.charAt(n-1)!='8') return "NO";
int sum=0;
for(int i=0;i<n;i++){
sum+=s.charAt(i)-'0';
}
if(sum%10!=0) return "NO";
return "YES";
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
String s=sc.next();
System.out.println(check(s));
}
sc.close();
}
}
+22
View File
@@ -0,0 +1,22 @@
import java.util.Scanner;
public class Main {
static String nt="2357";
static String check(String s){
int n=s.length();
for(int i=0;i<n;i++){
if(nt.indexOf(s.charAt(i))==-1) return "NO";
if(s.charAt(i)!=s.charAt(n-1-i)) return "NO";
}
return "YES";
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
String s=sc.next();
System.out.println(check(s));
}
sc.close();
}
}
@@ -0,0 +1,37 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package pkg6.tap_tu_rieng_cua_2_xau;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
/**
*
* @author GIGABYTE
*/
public class Tap_tu_rieng_cua_2_xau {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
sc.nextLine();
while(t>0){
t-=1;
Set<String> st = new HashSet<>();
String s1[] = sc.nextLine().split("\\s");
String s2 = sc.nextLine();
for(String word: s1){
if(!s2.contains(word)){
st.add(word);
}
}
for(String i: st){
System.out.print(i + " ");
}
System.out.println("");
}
}
}
+31
View File
@@ -0,0 +1,31 @@
import java.util.Scanner;
import java.util.HashMap;
public class Main{
static String mail(String s){
String[] a = s.split("[' ']+");
String r = a[a.length-1];
for(int i=0; i<a.length-1; i++){
if(a[i].length() != 0) r += Character.toString(a[i].charAt(0));
}
return r;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
sc.next();
HashMap<String, Integer> a = new HashMap<>();
while(sc.hasNextLine()){
String name = sc.nextLine();
if(name.length() == 0) continue;
String user = mail(name.toLowerCase());
if(a.get(user) == null){
a.put(user, 2);
}else{
int th = a.get(user);
a.put(user, th+1);
user += Integer.toString(th);
}
System.out.println(user + "@ptit.edu.vn");
}
}
}
@@ -0,0 +1,18 @@
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
static BigInteger gcd(BigInteger a, BigInteger b){
if(b.equals(BigInteger.ZERO)) return a;
return gcd(b,a.mod(b));
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
while(t-->0){
BigInteger a=sc.nextBigInteger(), b=sc.nextBigInteger();
System.out.println(gcd(a,b));
}
sc.close();
}
}
@@ -0,0 +1,13 @@
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
BigInteger a = new BigInteger(sc.next());
BigInteger b = new BigInteger(sc.next());
System.out.println(a.add(b));
}
}
}
@@ -0,0 +1,17 @@
import java.util.Scanner;
import java.math.BigInteger;
public class Main{
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
BigInteger a = new BigInteger(sc.next());
BigInteger b = new BigInteger(sc.next());
int max = Math.max(a.toString().length(), b.toString().length());
String ans = a.subtract(b).abs().toString();
while(ans.length()<max) ans = "0" + ans;
System.out.println(ans);
}
}
}
@@ -0,0 +1,13 @@
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = 1;
while(t-->0){
BigInteger a = new BigInteger(sc.next());
BigInteger b = new BigInteger(sc.next());
System.out.println(a.add(b));
}
}
}
@@ -0,0 +1,14 @@
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
static BigInteger gcd(BigInteger a, BigInteger b){
if(b.equals(BigInteger.ZERO)) return a;
return gcd(b,a.mod(b));
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
BigInteger a=sc.nextBigInteger(),b=sc.nextBigInteger();
System.out.println(a.subtract(b));
}
}
+14
View File
@@ -0,0 +1,14 @@
import java.util.*;
import java.math.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
BigInteger a = new BigInteger(sc.next());
if (a.mod(new BigInteger("11")).equals(BigInteger.ZERO))
System.out.println("1");
else System.out.println("0");
}
}
}
+33
View File
@@ -0,0 +1,33 @@
import java.io.*;
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = Integer.parseInt(sc.nextLine());
while(t-->0){
String s = sc.next();
Stack<Integer> st = new Stack<>();
for(int i=0;i<s.length();i++){
if(st.size()<2) st.push(i);
else{
if(s.charAt(i) == '0' && s.charAt(st.lastElement()) == '0' && s.charAt(st.elementAt(st.size() - 2)) == '1'){
st.pop();
st.pop();
}else st.push(i);
}
}
int ans = 0;
for(int i=1;i<st.size();i++){
ans = Math.max(ans, st.elementAt(i) - st.elementAt(i-1) - 1);
}
if(st.empty()) ans = s.length();
else{
ans = Math.max(ans, st.elementAt(0));
ans = Math.max(ans, s.length() - st.lastElement() - 1);
}
System.out.println(ans);
}
}
}
+22
View File
@@ -0,0 +1,22 @@
import java.math.*;
import java.util.*;
public class Main {
static int pow(int a, int b) {
if (b == 0)
return 1;
return a * pow(a, b - 1);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
BigInteger n = new BigInteger(sc.next());
int a = Integer.parseInt(n.mod(BigInteger.valueOf(4)).toString());
int b = Integer.parseInt(n.mod(BigInteger.valueOf(2)).toString());
int s = 1 + pow(2, a) + pow(3, a) + pow(4, b);
System.out.println(s%5);
}
}
}
+31
View File
@@ -0,0 +1,31 @@
import java.util.*;
import java.math.*;
public class Main {
static class pi {
char c;
int index;
public pi(char x, int y) {
c = x;
index = y;
}
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String s = sc.next();
ArrayList<pi> a = new ArrayList<>();
for (int i = 0; i < s.length(); i++)
a.add(new pi(s.charAt(i), i));
a.sort((pi x, pi y) -> {
return (int) (y.c - x.c);
});
int pre = 0;
for (int i = 0; i < a.size(); i++) {
if (a.get(i).index < pre)
continue;
System.out.print(a.get(i).c);
pre = a.get(i).index;
}
}
}
@@ -0,0 +1,61 @@
import java.util.*;
public class Main {
static class word {
String w;
int cnt, stt;
public word(String w, int i) {
this.w = w;
this.cnt = 1;
this.stt = i;
}
@Override
public String toString() {
return w + " " + cnt;
}
}
static boolean tn(String s) {
for (int i = 0; i < s.length() / 2; i++) {
if (s.charAt(i) != s.charAt(s.length() - 1 - i))
return false;
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
ArrayList<word> a = new ArrayList<>();
int index = 0;
while (sc.hasNext()) {
String s = sc.next();
if (!tn(s))
continue;
boolean oke = true;
for (int i = 0; i < a.size(); i++) {
if (a.get(i).w.compareTo(s) == 0) {
oke = false;
a.get(i).cnt += 1;
}
}
if (oke) {
a.add(new word(s, index));
index++;
}
}
a.sort((word x, word y) -> {
if (x.w.length() == y.w.length()) {
return x.stt - y.stt;
}
return y.w.length() - x.w.length();
});
a.forEach(e -> {
if (e.w.length() == a.get(0).w.length())
System.out.println(e);
});
}
}
@@ -0,0 +1,35 @@
import java.util.Scanner;
import java.math.BigInteger;
public class Main{
static char convert(char x){
if("ABC".indexOf(x) > -1) return '2';
if("DEF".indexOf(x) > -1) return '3';
if("GHI".indexOf(x) > -1) return '4';
if("JKL".indexOf(x) > -1) return '5';
if("MNO".indexOf(x) > -1) return '6';
if("PQRS".indexOf(x) > -1) return '7';
if("TUV".indexOf(x) > -1) return '8';
return '9';
}
static boolean dx(char[] a, int n){
for(int i=0;i<=n/2;i++){
if(a[i]!=a[n-i-1]) return false;
}
return true;
}
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
String s = sc.next();
s = s.toUpperCase();
char[] c = new char[s.length()];
for(int i=0; i<s.length(); i++){
c[i] = convert(s.charAt(i));
}
if(dx(c, c.length)) System.out.println("YES");
else System.out.println("NO");
}
}
}
+19
View File
@@ -0,0 +1,19 @@
import java.util.*;
public class Main {
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String s = "";
while (sc.hasNextLine())
s += sc.nextLine();
String[] a = s.trim().toLowerCase().split("[.?!]");
for (String i : a) {
i = i.trim();
i = Character.toString(i.charAt(0)).toUpperCase() + i.substring(1);
for (String j : i.split("\\s+"))
System.out.printf("%s ", j);
System.out.println("");
}
}
}
+30
View File
@@ -0,0 +1,30 @@
import java.util.*;
public class Main {
static HashMap<Character, Integer> a = new HashMap<>();
static String LM = "MDCLXVI";
static int cal(String s) {
for (int i = 0; i < LM.length(); i++) {
if (s.indexOf(LM.charAt(i)) > -1) {
int x = s.indexOf(LM.charAt(i));
return a.get(LM.charAt(i)) - cal(s.substring(0, x)) + cal(s.substring(x + 1));
}
}
return 0;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
a.put('I', 1);
a.put('V', 5);
a.put('X', 10);
a.put('L', 50);
a.put('C', 100);
a.put('D', 500);
a.put('M', 1000);
int t = sc.nextInt();
while (t-- > 0) {
System.out.println(cal(sc.next()));
}
}
}
+29
View File
@@ -0,0 +1,29 @@
import java.math.BigInteger;
import java.util.Scanner;
public class Main {
static boolean is(char x){
if(x>='0' && x<='9') return true;
return false;
}
static String check(String s){
if(s.charAt(0)=='0') return "INVALID";
int n=s.length();
int c=0;
for(int i=0;i<n;i++){
if(!is(s.charAt(i))) return "INVALID";
if((s.charAt(i)-'0')%2==0) c++;
}
if(n%2==0 && c>n-c) return "YES";
if(n%2==1 && c<n-c) return "YES";
return "NO";
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t=sc.nextInt();
for(int i=0;i<t;i++){
System.out.println(check(sc.next()));
}
sc.close();
}
}
+18
View File
@@ -0,0 +1,18 @@
import java.util.Scanner;
public class Main{
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
String S = sc.next();
int cnt = 0;
for(int i=0; i<S.length()/2; i++){
if(S.charAt(i) != S.charAt(S.length()-i-1)) cnt++;
}
if(cnt==1 || cnt==0 && S.length() % 2 == 1) System.out.println("YES");
else System.out.println("NO");
}
sc.close();
}
}
+22
View File
@@ -0,0 +1,22 @@
import java.util.Scanner;
public class Main{
static boolean eq(String a, String b){
if(a.length() != b.length()) return false;
for(int i=0; i<a.length(); i++){
if(a.charAt(i) != b.charAt(i)) return false;
}
return true;
}
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
String a = sc.next();
String b = sc.next();
if(!eq(a,b)) System.out.println(Math.max(a.length(), b.length()));
else System.out.println("-1");
}
sc.close();
}
}
+20
View File
@@ -0,0 +1,20 @@
import java.util.Scanner;
import java.util.Stack;
public class Main{
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
String s = sc.next();
Stack<Character> a = new Stack<>();
for(int i=0; i<s.length(); i++){
if(a.empty()) a.push(s.charAt(i));
else{
if(a.lastElement() == s.charAt(i)) a.pop();
else a.push(s.charAt(i));
}
}
if(a.empty()) System.out.println("Empty String");
else a.forEach(c -> System.out.print(c));
sc.close();
}
}
+37
View File
@@ -0,0 +1,37 @@
import java.util.*;
public class non {
static int value(String s){
int cnt = 0;
for(int i=0; i<s.length(); i++){
cnt += (int)(s.charAt(i)-'A');
}
return cnt;
}
static String turn(String s){
int value = value(s);
String r = "";
for(int i=0;i<s.length();i++){
r += (char)((s.charAt(i) - 'A' + value)%26 + 'A');
}
return r;
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
String s = sc.next();
String a = s.substring(0, s.length()/2);
String b = s.substring(s.length()/2);
a = turn(a);
b = turn(b);
String r = "";
for(int i=0;i<a.length();i++){
r += (char)((a.charAt(i) + b.charAt(i) - 2*'A')%26 + 'A');
}
System.out.println(r);
}
}
}
+36
View File
@@ -0,0 +1,36 @@
import java.util.Scanner;
public class non {
static String deli = ".!?";
static void process(String s){
if(s.length() == 0) return;
if(deli.indexOf(s.charAt(s.length()-1)) == -1){
s += ".";
}
String a[] = s.split("\\s+");
int n = a.length;
if(a[a.length - 1].length() == 1 && deli.contains(a[a.length - 1])){
if(a.length>1){
n-=1;
a[a.length - 2] += a[a.length - 1];
}
}
int i=0;
while(i<n){
a[i] = Character.toString(a[i].charAt(0)).toUpperCase() + a[i].substring(1);
while(deli.indexOf(a[i].charAt(a[i].length()-1)) == -1){
System.out.printf("%s ", a[i]);
i++;
}
System.out.printf("%s ", a[i]);
i++;
System.out.println();
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNextLine()){
process(sc.nextLine().toLowerCase().trim());
}
}
}
+25
View File
@@ -0,0 +1,25 @@
/*
* This Java source file was generated by the Gradle 'init' task.
*/
package nxt;
import java.util.Scanner;
public class App {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String s = sc.next();
int n = s.length();
int[] dpA = new int[n+1];
int[] dpB = new int[n+1];
for(int i=1; i<=n; i++){
if(s.charAt(i-1)=='A'){
dpA[i]=Math.min(dpA[i-1],dpB[i-1]+1);
dpB[i]=Math.min(dpB[i-1]+1, dpA[i-1]+1);
}else{
dpA[i]=Math.min(dpA[i-1]+1,dpB[i-1]+1);
dpB[i]=Math.min(dpB[i-1], dpA[i-1]+1);
}
}
System.out.print(dpA[n]);
sc.close();
}
}
+18
View File
@@ -0,0 +1,18 @@
import java.util.*;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while (t-- > 0) {
String s = sc.next();
int k = sc.nextInt();
HashSet<Character> hs = new HashSet<>();
for (int i = 0; i < s.length(); i++) {
hs.add(s.charAt(i));
}
if (s.length() >= 26 && hs.size() + k >= 26) {
System.out.println("YES");
}else System.out.println("NO");
}
}
}
+22
View File
@@ -0,0 +1,22 @@
import java.util.Scanner;
import java.util.Stack;
public class Main{
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
sc.nextLine();
while(t-->0){
String line = sc.nextLine();
String[] w = line.split("[' ']+");
for(int i=0; i<w.length; i++){
for(int j=0; j<w[i].length(); j++){
System.out.print(w[i].charAt(w[i].length()-j-1));
}
System.out.print(' ');
}
System.out.println("");
}
sc.close();
}
}
@@ -0,0 +1,19 @@
import java.util.Scanner;
import java.math.BigInteger;
public class Main{
static BigInteger gcd(BigInteger a, BigInteger b){
if(b.equals(BigInteger.ZERO)) return a;
return gcd(b, a.mod(b));
}
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
BigInteger a = new BigInteger(sc.next());
BigInteger b = new BigInteger(sc.next());
System.out.println(a.multiply(b).divide(gcd(a,b)));
}
sc.close();
}
}
+46
View File
@@ -0,0 +1,46 @@
import java.io.*;
import java.util.*;
public class Main {
static long pow(long a, long b){
if(b==0) return 1;
return pow(a, b-1) * a;
}
static long q_cnt(String a){
long r=0;
for(int i=0;i<a.length();i++){
if(a.charAt(i) == '?') r++;
}
return r;
}
static long cnt(int i, String a, String b, long rm){
if(i==a.length()) return 0;
if(a.charAt(i)=='?'){
return (long)('9' - b.charAt(i))*pow(10, rm - 1) + cnt(i+1, a, b, rm - 1);
}
else{
if(a.charAt(i) < b.charAt(i)) return 0;
if(a.charAt(i) > b.charAt(i)) return pow(10, rm);
return cnt(i+1, a, b, rm);
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = Integer.parseInt(sc.nextLine());
while(t-->0){
String a = sc.next(), b = sc.next();
long n = q_cnt(a);
if(a.length()<b.length() || n==0) System.out.println(0);
else if(a.length() > b.length()){
if(a.charAt(0) == '?') System.out.println(9*pow(10, n-1));
else System.out.println(pow(10, n));
}else{
System.out.println(cnt(0,a,b,n));
}
}
}
}
+39
View File
@@ -0,0 +1,39 @@
import java.util.*;
public class non {
static int turn(String a, String b){
if(a.length() != b.length()) return -1;
String s = a;
for(int i=0;i<=a.length();i++){
if(s.compareTo(b) == 0) return i;
s = s.substring(1) + s.charAt(0);
}
return -1;
}
static void solve(String[] a){
int ans = 1000000;
for(String i: a){
int cnt = 0;
for(String j:a){
int x = turn(j, i);
if(x==-1){
System.out.println(-1);
return;
}
cnt+=x;
}
ans=Math.min(ans, cnt);
}
System.out.println(ans);
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
String[] a = new String[n];
for(int i=0;i<a.length;i++) a[i] = sc.next();
solve(a);
}
}
+35
View File
@@ -0,0 +1,35 @@
import java.util.*;
public class Main {
static class pair {
int x, y;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.next();
ArrayList<pair> a = new ArrayList<>();
for (char c = 'A'; c <= 'Z'; c++) {
int i = 0;
pair x = new pair();
for (int j = 0; j < s.length(); j++) {
if (s.charAt(j) == c) {
if (i == 0) {
x.x = j;
i++;
} else
x.y = j;
}
}
a.add(x);
}
int cnt = 0;
for (int i = 0; i < a.size() - 1; i++) {
for (int j = i + 1; j < a.size(); j++) {
if (a.get(i).y < a.get(j).x || a.get(j).y < a.get(i).x || (a.get(i).x-a.get(j).x) * (a.get(j).y -a.get(i).y) > 0)
continue;
cnt++;
}
}
System.out.println(cnt);
}
}
+34
View File
@@ -0,0 +1,34 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit this template
*/
package pkg17.danh_dau_chu_cai;
import java.util.HashSet;
import java.util.Scanner;
import java.util.Set;
/**
*
* @author GIGABYTE
*/
public class Danh_dau_chu_cai {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
Set<String> st = new HashSet<>();
for(int i=0;i<s.length();i++){
String temp = s.charAt(i)+" ";
st.add(temp);
}
System.out.println(st.size());
}
}
+16
View File
@@ -0,0 +1,16 @@
import java.util.Scanner;
import java.math.BigInteger;
public class Main{
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
BigInteger a = new BigInteger(sc.next());
BigInteger b = new BigInteger(sc.next());
if(a.mod(b).equals(BigInteger.ZERO) || b.mod(a).equals(BigInteger.ZERO)) System.out.println("YES");
else System.out.println("NO");
}
sc.close();
}
}
+32
View File
@@ -0,0 +1,32 @@
import java.util.Scanner;
import java.math.BigInteger;
public class Main{
static boolean inc(String s){
for(int i=0; i<s.length()-1; i++){
if(s.charAt(i) >= s.charAt(i+1)) return false;
}
return true;
}
static boolean equal(String s){
if(s.charAt(0) == s.charAt(1) && s.charAt(1) == s.charAt(2) && s.charAt(3) == s.charAt(4)) return true;
return false;
}
static boolean se(String s){
for(int i=0; i<s.length(); i++){
if("68".indexOf(s.charAt(i)) == -1) return false;
}
return true;
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-->0){
String card = sc.next().substring(5);
String[] a = card.split("['.']");
card = a[0] + a[1];
if(inc(card) || equal(card) || se(card)) System.out.println("YES");
else System.out.println("NO");
}
}
}
+25
View File
@@ -0,0 +1,25 @@
import java.util.Scanner;
public class Main{
static class point{
double x,y;
public void get(Scanner sc){
x = sc.nextDouble();
y = sc.nextDouble();
}
}
static double dis(point a, point b){
return Math.sqrt(Math.pow(a.x-b.x,2) + Math.pow(a.y - b.y, 2));
}
public static void main(String args[]){
Scanner sc = new Scanner(System.in);
int t = sc.nextInt();
while(t-- > 0){
point a = new point();
point b = new point();
a.get(sc);
b.get(sc);
System.out.printf("%.4f\n",dis(a,b));
}
sc.close();
}
}
@@ -0,0 +1,17 @@
import java.util.Scanner;
import java.math.BigInteger;
public class Main{
static String st(String s){
if(s.length()==1) return "" + Character.toUpperCase(s.charAt(0));
s = s.toLowerCase();
return "" + Character.toUpperCase(s.charAt(0)) + s.substring(1);
}
public static void main(String[] arg){
Scanner sc = new Scanner(System.in);
int a = sc.nextInt(), b = sc.nextInt();
String c = sc.next();
if(a>0 && b>0) System.out.printf("%d %d %s",(a+b)*2, a*b, st(c));
else System.out.println("INVALID");
}
}

Some files were not shown because too many files have changed in this diff Show More