- /****************##### بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم #####******************
- __________________________________________________________________________
- ###################### Ya-Seen Arafat(ACWizard) #########################
- ###################### UAP-CSE-33B #########################
- *************************************************************************/
- #include <bits/stdc++.h>
- #define S 5000003
- using namespace std;
- bool prime[S];
- __int64 primeD[S];
- void seive(){
- int last;
- for(int i = 2; i <= S; i++){
- if(prime[i]){
- primeD[i] = i-1;
- for(int j = i+i; j <= S; j+=i){
- prime[j] = false;
- if(!primeD[j])primeD[j] = (__int64)j - (j/i);
- else primeD[j] -= (primeD[j]/i);
- }
- }
- }
- for(int i = 2; i <= S; i++)primeD[i] *= primeD[i];
- }
- void cumulative_sum(){
- for(int i = 3; i <= S; i++){
- primeD[i] = primeD[i-1]+primeD[i];
- }
- }
- int main(){
- memset(prime, true, sizeof(prime));
- seive();
- cumulative_sum();
- int t, a, b, cs = 0;
- scanf("%d", &t);
- while(t--){
- scanf("%d %d", &a, &b);
- printf("Case %d: %llu\n", ++cs, primeD[b]-primeD[a-1]);
- }
- return 0;
- }
Friday, February 26, 2016
LightOJ - 1007 - Mathematically Hard
Thursday, February 25, 2016
UVa - 524 - Prime Ring Problem
- /****************##### بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم #####******************
- __________________________________________________________________________
- ###################### Ya-Seen Arafat(ACWizard) #########################
- ###################### UAP-CSE-33B #########################
- *************************************************************************/
- #include <bits/stdc++.h>
- #define S 100
- using namespace std;
- int n;
- bool check[23];
- int all[23];
- bool prime[S];
- void primeCheck(){
- int lim = sqrt(S);
- for(int i = 2; i <= lim; i++){
- if(prime[i]){
- for(int j = i*2; j <= S; j+=i){
- prime[j] = false;
- }
- }
- }
- }
- bool sum(int a, int b){
- int c = a+b;
- return prime[c];
- }
- void PrimeRing(int ind){
- int p = all[ind-1]+all[ind-2];
- if(p > 1 && !prime[p])return;
- if(n == ind){
- if(sum(all[0], all[n-1])){
- for(int i = 0; i < n; i++){
- printf("%d", all[i]);
- if(i != n-1)printf(" ");
- }
- puts("");
- }
- return;
- }
- for(int i = 1; i < n; i++){
- if(!check[i]){
- all[ind] = i+1;
- check[i] = true;
- PrimeRing(ind+1);
- check[i] = false;
- }
- }
- }
- int main(){
- int cs = 0;
- memset(prime, true, sizeof(prime));
- primeCheck();
- while(scanf("%d", &n) == true){
- if(cs)puts("");
- memset(check, false, sizeof(check));
- memset(all, 0, sizeof(all));
- printf("Case %d:\n", ++cs);
- all[0] = 1;
- PrimeRing(1);
- }
- return 0;
- }
LightOJ - 1023 - Discovering Permutations
- /****************##### بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم #####******************
- __________________________________________________________________________
- ###################### Ya-Seen Arafat(ACWizard) #########################
- ###################### UAP-CSE-33B #########################
- *************************************************************************/
- #include <bits/stdc++.h>
- using namespace std;
- int n, m, cn;
- bool check[30];
- char all[30];
- void DisPer(int ind){
- if(n == ind){
- for(int i = 0; i < n; i++){
- printf("%c", all[i]);
- }
- puts("");
- cn++;
- return;
- }
- if(cn >= m)return;
- for(int i = 0; i < n; i++){
- if(!check[i]){
- all[ind] = i+'A';
- check[i] = true;
- DisPer(ind+1);
- check[i] = false;
- }
- }
- }
- int main(){
- int t, cs = 0;
- scanf("%d", &t);
- while(t--){
- cn = 0;
- scanf("%d %d", &n, &m);
- memset(check, false, sizeof(check));
- memset(all, 0, sizeof(all));
- printf("Case %d:\n", ++cs);
- DisPer(0);
- }
- return 0;
- }
Saturday, January 30, 2016
LightOJ - 1004 - Monkey Banana Problem
- /****************##### بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم #####******************
- __________________________________________________________________________
- ###################### Ya-Seen Arafat(ACWizard) #########################
- ###################### UAP-CSE-33B #########################
- *************************************************************************/
- #include <bits/stdc++.h>
- #define sc(n) scanf("%d", &n)
- #define scD(n) scanf("%lf", &n)
- #define scS(n) scanf("%s", &n)
- #define S 103
- using namespace std;
- int n, ar[S*3][S];
- int dp[S*3][S], sz[S*3];
- int MB(int row, int ind, int jump){
- if(row > (n+n-1) || ind < 0 || ind > sz[jump])return 0;
- if(dp[jump][ind] != -1)return dp[jump][ind];
- if(row < n)dp[jump][ind] = (ar[jump][ind] + max(MB(row+1, ind, jump+1), MB(row+1, ind+1, jump+1)));
- else dp[jump][ind] = (ar[jump][ind] + max(MB(row+1, ind-1, jump+1), MB(row+1, ind, jump+1)));
- return dp[jump][ind];
- }
- int main(){
- int t, cs = 0;
- scanf("%d", &t);
- while(t--){
- memset(dp, -1, sizeof(dp));
- memset(ar, 0, sizeof(ar));
- scanf("%d", &n);
- int z;
- for(int i = 0; i < n; i++){
- for(int j = 0; j <= i; j++){
- scanf("%d", &ar[i][j]);
- }
- sz[i] = i;
- z = i;
- }
- int jj = n-1;
- for(int i = z+1; i < (n+n-1); i++){
- for(int j = 0; j < jj; j++){
- scanf("%d", &ar[i][j]);
- }
- sz[i] = jj-1;
- jj--;
- }
- int ans = MB(1, 0, 0);
- cout << "Case " << ++cs << ": " << ans << endl;
- }
- return 0;
- }
Saturday, January 9, 2016
LightOJ - 1009 - Back to Underworld --- using BFS
- /****************##### بِسْمِ اللَّهِ الرَّحْمَنِ الرَّحِيم #####******************
- __________________________________________________________________________
- ###################### Ya-Seen Arafat(ACWizard) #########################
- ###################### UAP-CSE-33B #########################
- *************************************************************************/
- #include <bits/stdc++.h>
- #define sc(n) scanf("%d", &n)
- #define scD(n) scanf("%lf", &n)
- #define scS(n) scanf("%s", &n)
- #define S 20003
- #define SS 100003
- using namespace std;
- typedef long long LL;
- vector <int> race[S], edge;
- int ans[S], check[S];
- int BFS(int src){
- int anss = 0;
- queue <int> Q;
- Q.push(src);
- int sz = edge.size();
- ans[src] = 1;
- while(!Q.empty()){
- int x, y;
- x = Q.front();
- check[x]++;
- Q.pop();
- y = race[x].size();
- for(int i = 0; i < y; i++){
- if(!ans[race[x][i]]){
- Q.push(race[x][i]);
- if(ans[x] == 1)ans[race[x][i]] = 2;
- if(ans[x] == 2)ans[race[x][i]] = 1;
- }
- }
- if(Q.empty()){
- int vamp = 0, lyk = 0;
- for(int i = 0; i < S; i++){
- if(ans[i] == 1)vamp++;
- if(ans[i] == 2)lyk++;
- ans[i] = 0;
- }
- anss += max(vamp, lyk);
- for(int i = 0; i < sz; i++){
- if(check[edge[i]] == 1){
- Q.push(edge[i]);
- ans[edge[i]] = 1;
- break;
- }
- }
- }
- }
- return anss;
- }
- void Do(){
- int t, n, u, v, cs = 0;
- sc(t);
- while(t--){
- sc(n);
- memset(ans, 0, sizeof(ans));
- memset(check, 0, sizeof(check));
- for(int i = 0; i < S; i++)race[i].clear();
- edge.clear();
- while(n--){
- sc(u);
- sc(v);
- if(!check[u])check[u] = 1, edge.push_back(u);
- if(!check[v])check[v] = 1, edge.push_back(v);
- race[u].push_back(v);
- race[v].push_back(u);
- }
- int members = BFS(u);
- printf("Case %d: %d\n", ++cs, members);
- }
- }
- int main(){
- ios_base::sync_with_stdio(0); cin.tie(0);
- #ifndef ONLINE_JUDGE
- ///freopen("contest.txt","w",stdout);
- #endif
- Do();
- return 0;
- }
Subscribe to:
Posts (Atom)