結果

問題 No.1006 Share an Integer
ユーザー amaridekinaiamaridekinai
提出日時 2020-03-06 23:06:56
言語 C++14
(gcc 13.2.0 + boost 1.83.0)
結果
AC  
実行時間 23 ms / 2,000 ms
コード長 1,935 bytes
コンパイル時間 1,928 ms
コンパイル使用メモリ 172,928 KB
実行使用メモリ 5,760 KB
最終ジャッジ日時 2024-04-22 10:22:27
合計ジャッジ時間 3,133 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 16 ms
5,504 KB
testcase_01 AC 16 ms
5,376 KB
testcase_02 AC 17 ms
5,376 KB
testcase_03 AC 17 ms
5,632 KB
testcase_04 AC 16 ms
5,376 KB
testcase_05 AC 16 ms
5,376 KB
testcase_06 AC 17 ms
5,632 KB
testcase_07 AC 16 ms
5,504 KB
testcase_08 AC 16 ms
5,504 KB
testcase_09 AC 16 ms
5,760 KB
testcase_10 AC 16 ms
5,632 KB
testcase_11 AC 21 ms
5,632 KB
testcase_12 AC 23 ms
5,504 KB
testcase_13 AC 23 ms
5,504 KB
testcase_14 AC 22 ms
5,504 KB
testcase_15 AC 22 ms
5,632 KB
testcase_16 AC 20 ms
5,504 KB
testcase_17 AC 21 ms
5,504 KB
testcase_18 AC 22 ms
5,504 KB
testcase_19 AC 21 ms
5,632 KB
testcase_20 AC 22 ms
5,760 KB
testcase_21 AC 22 ms
5,504 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include<bits/stdc++.h>
using namespace std;
#define int long long 

const int MAX_N = 2e6+10;

const int inf = 1e9;

bool arr[MAX_N];
int division[MAX_N];

set<int> se;

void eratosthenes(){
  
  for(int i = 0; i < MAX_N; i++){ arr[i] = true;}
  
  arr[0] = false; arr[1] = false;
  
  for(int i = 2; i < MAX_N; i++){  //素因数列挙
    if(arr[i] ){ 
     for(int j = 2; i*j < MAX_N; j++){
       
       arr[i*j] = false;
     }
    }
  }
  
  for(int i = 0; i < (int)sqrt(MAX_N); i++){ 
    if( arr[i] ){ 
      se.insert(i);
    }
  }
  
  return ;
}

signed main(void){
  
  int N; cin >> N;
  
  set<pair<int,int>> ans;
  eratosthenes();
  
 
  division[1] = 1;
  
  int left  = max(N/2 - (int)sqrt(N) - 10,1LL);
  int right = min(N/2 + (int)sqrt(N) + 10,N  );
  
  
  for(int i = left; i <= right; i++){ 
    // iの約数の個数を計算しておく
    // N/2の√N周りらへんを計算しておけば十分そう(予想)
    
    
    int res = i;
    
    int s = 1LL;
    
    if( arr[res] ){ division[i] = 2; continue;} 
    // p と 1
      
      for(auto d : se){ 
        
        if(res % d == 0 ){ 
          
          int cnt = 1LL;
          
          while( res % d == 0  ){ 
            res /= d;
            cnt++;
          }
          
          s *= cnt;
          if( res == 1 ){ break;} //for-脱出
        }// if res%d
      }//for-d
    
    if( arr[res] ){ s *= 2;}
     division[i] = s;

  }
  
  
  int rem = inf;
  
  for(int A = left; A <= right; A++){ 
    
    
    int B = N-A;
    if( A <= 0 || B <= 0 ){ continue;}
    
    
    int FA = A-division[A];
    int FB = B-division[B];
    
    if( rem > abs(FA-FB) ){ 
      rem = abs(FA-FB); 
      ans.clear();
      ans.insert({A,B});
    }
    else if( rem == abs(FA-FB) ){ 
      ans.insert({A,B});
    }
    
  }
  
  for(auto ne : ans ){ cout << ne.first << " " << ne.second << endl;}

    return 0;
  
                     
}
0